【问题标题】:Why Spring MVC get 404?为什么 Spring MVC 得到 404?
【发布时间】:2017-08-12 05:34:55
【问题描述】:

我有一个简单的 Spring MVC 应用程序。我得到 HTTP 状态 404 报告。帮我解决这个问题。谢谢。

HTTP 状态 404 - 未找到

类型状态报告

消息 /WEB-INF/users_view.jsp

描述 源服务器没有找到当前的表示 为目标资源或不愿意透露存在。

Apache Tomcat/9.0.0.M26

我的控制器:

@Controller
public class UserController {

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String showUsers(ModelMap model) {
        return "users_view";
    }
}

项目结构:

我的web.xml

<display-name>User</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Process application servlet -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

还有applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <context:component-scan base-package="ru.pravvich" />

</beans>

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    在调度程序 servlet 中您提供了路径 /WEB-INF/,但在应用程序中它是 WEB-INF/view/

    所以在 dispatcher servlet 中将其更改为 /WEB-INF/view/ 即可解决问题

    【讨论】:

      【解决方案2】:

      由于您的jspWEB-INF/view 下,并且InternalResourceViewResolver 前缀指向/WEB-INF/,因此您遇到了404 错误。要解决此问题,请将前缀更改为 /WEB-INF/view/

      【讨论】:

        【解决方案3】:

        InternalViewResolver 的问题

        解决方案 1:将前缀 /WEB-INF/ 更改为 /WEB-INF/view

        解决方案 2:从控制器返回字符串时更改“users_view” 到“/view/users_view”

        如果您正在使用 spring mvc 启动新项目,请尝试基于 java 的配置 参考Spring mvc hello world example

        【讨论】:

          猜你喜欢
          • 2016-07-04
          • 2020-02-26
          • 2011-08-26
          • 2012-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多