为什么Spring MVC获得404?

时间:2017-08-12 05:34:55

标签: java spring spring-mvc

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

  

HTTP状态404 - 未找到

     

输入状态报告

     

消息/WEB-INF/users_view.jsp

     

描述源服务器未找到当前表示   对于目标资源或不愿意披露存在的资源。

     

Apache Tomcat / 9.0.0.M26

我的控制器:

sleep

项目结构:

enter image description here

我的@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

3 个答案:

答案 0 :(得分:2)

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

因此,将调度程序servlet内部的更改为/WEB-INF/view/将解决问题

答案 1 :(得分:2)

由于您的jsp位于WEB-INF/view下,InternalResourceViewResolver前缀指向/WEB-INF/,因此您有404错误。要修复此问题,请将前缀更改为/WEB-INF/view/

答案 2 :(得分:1)

Problem with InternalViewResolver

Solution 1: Change Prefix /WEB-INF/ to /WEB-INF/view

Solution 2: While returning String from controller change “users_view” to “/view/users_view”

If you are starting new project using spring mvc try java based configuration refer Spring mvc hello world example