我有以下控制器…
@Controller @RequestMapping(value = "/userManagement") public class UserManagementController { @RequestMapping(value = "/", method = RequestMethod.GET) public Object home(Locale locale, Model model) { logger.info("User management view controller loaded..."); return "userManagement"; } @RequestMapping(value = "/createUserView", method = RequestMethod.GET) public Object createUser(Locale locale, Model model) { logger.info("create controller loaded..."); return "createUser"; }
我的servlet上下文使用以下值设置…
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean>
现在,如果我转到http://localhost:8080/myapp/userManagement那,我得到的视图就是userManagement.jsp,这正是我想要的…
http://localhost:8080/myapp/userManagement
但是,如果我转到http://localhost:8080/myapp/userManagement/createUserView404错误。
http://localhost:8080/myapp/userManagement/createUserView
NetworkError: 404 Not Found - http://localhost:8080/myapp/userManagement/createUserView"
我看不到的是为什么会发生这种情况,因为我将requestMapping设置为与上面完全相同,并且在/ WEB-INF / views中我有一个createUser.jsp和userManagement.jsp
关于在Spring MVC中提供视图,我做错了什么?
谢谢,
编辑:web.xml添加下面…
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml /WEB-INF/spring/security-app-context.xml /WEB-INF/spring/appServlet/servlet-context.xml</param-value> </context-param> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
编辑2:
另外,如果我直接转到浏览器中的地址而不是使用Ajax(转到myapp / userManagement / createUserView,则会收到错误消息…
HTTP状态404-/myapp/WEB- INF/views/userManagement/createUserView.jsp,因此它似乎在查找目录太高的目录(尽管文件名也错误)。
编辑3。
好的,即使执行以下操作也是如此。
@Controller @RequestMapping(value = "/userManagement") public class UserManagementController { @RequestMapping(value = "/", method = RequestMethod.GET) public Object home(Locale locale, Model model) { logger.info("User management view controller loaded..."); return "createUser"; }
我仍然看到userManagement.jsp页面,因此似乎该返回未正确触发,但是我不知道为什么。记录器详细信息仍然会发送到控制台,因此实际上到达了控制台,这与springmvc返回JSP的方式有些奇怪。
您是否可以检查2件事情:1)将断点添加到控制器或查看服务器日志,检查请求是否进入方法。2)如果第一步正确,请检查正确路径下是否存在jsp或jsp名称正确
编辑
等待,如果您使用Ajax进行请求,则您的控制器应具有以下requestMapping:
@RequestMapping(value = "/url", method = RequestMethod.GET, headers = "X-Requested-With=XMLHttpRequest")
否则,您的ajax请求将不会被映射,您将得到404。
旁注 您可以将方法返回类型从Object更改为String吗?看看是否有影响?