【问题标题】:Java - HTTP Status 404 – Not FoundJava - HTTP 状态 404 - 未找到
【发布时间】:2018-05-06 14:29:41
【问题描述】:

我正在学习 spring mvc 并想测试一些非常简单的基于注释的控制器,它在主页上运行良好,但是当我进入 /home/user 时它给了我 HTTP 状态404 – Not Found 错误,我没有找到任何可以修复它的方法。

web.xml

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--####################################-->


    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

调度程序-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <context:component-scan base-package="spring" />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--<bean id="viewResolver"

</beans>

HomeController.java

package spring.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
//@RequestMapping(value = "/home")
public class HomeController {

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String showHomeMessage() {
        return "home";
    }
}

UserController.java

package spring.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/home/user")
public class UserController {

    @RequestMapping(method = RequestMethod.GET)
    public String welcomeMessage(){
        return "user";
    }
}

redirect.jsp 只是使用sendRedirect() 方法并重定向到home.jsp

home.jspuser.jsp 只是打印一个简单的 hello 消息

我哪里做错了?

问候

【问题讨论】:

  • 您是否尝试过将您的UserController 的用户请求映射更改为/user,而不是利用您为HomeController 中的方法定义的/home 的请求映射?我怀疑这就是您的问题所在。
  • @JacobBlanton 是的,同样的错误。我真的无法理解这个简单的代码有什么问题
  • 您在 HomeController 中注释掉了 @RequestMapping(value = "/home") 并将其映射到方法。您是否尝试过将@RequestMapping("/home/user") 移至该方法?实际上,UserController 是从HomeController 模式化的。
  • @jpllosa 是的,我也试过了。同样的错误。
  • 你试过嵌套RequestMapping吗?

标签: java spring-mvc web-applications http-status-code-404


【解决方案1】:

您是否尝试过嵌套RequestMapping?像这样的……

@Controller
@RequestMapping(value = "/home")
public class HomeController {

    @RequestMapping(method = RequestMethod.GET)
    public String showHomeMessage() {
        return "home";
    }

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String welcomeMessage(){
        return "user";
    }
}

我不确定您是否需要为 showHomeMessage 赋值(即 value="/")。

【讨论】:

  • 成功了!!!!但是如何?为什么我的代码不起作用?!我无法在一个控制器中完成所有处理。
【解决方案2】:

查看this 小示例后,我认为您的问题与您没有使用@Controller 注释指定@ResponseBody 的事实有关。然而,如果你使用 @RequestController 注释,仅仅暗示一个 String 类型就足够了,因为它结合了 @Controller@ResponseBody 的使用

@Controller
@RequestMapping("/home/user")
public class UserController {

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody String welcomeMessage(){
        return "user";
    }
}

同一块的替代方案是......

@RestController
@RequestMapping("/home/user")
public class UserController {

    @RequestMapping(method = RequestMethod.GET)
    public String welcomeMessage(){
        return "user";
    }
}

当然这个小例子没有演示MediaType等等,但希望你明白。

【讨论】:

  • 感谢您的回答。我已经阅读了 5-6 个教程和示例,但几乎没有!使用@ResponseBody。所以我什至不知道它是什么,为什么要使用它
猜你喜欢
  • 2021-08-29
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 2019-07-30
  • 2018-05-01
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多