Why won't my spring mvc views load?
我是 spring mvc 的新手。我目前正在使用 netbeans 8.0.2。以及一个spring mvc插件。从我在项目的 \\'libraries\\' 部分下看到的 jar 文件中,我似乎使用的是 spring mvc 4.0.1。
我可以从 tomcat 服务器甚至 glassfish 加载 index.jsp 页面,但如果我应该在目录结构 WEB-INFO>jsp>response.jsp 下创建另一个 .jsp 文件作为示例。我从服务器收到错误 404。这可能是一个可笑的简单修复,但到目前为止没有成功。
我怀疑这与我的控制器和 [springapp]-servlet.xml 文件有关。我在网上看到的大多数示例都使用 spring mvc 3.x 并避免在控制器设置中使用注释方法,乍一看后者对我来说似乎更容易。然后这些示例继续向上述 xml 文件添加几行。但是,我认为通过使用注释控制器设置,spring mvc 会自动检测我的控制器。不推荐使用注解方式吗?
我的配置文件如下:
web.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version="1.0" encoding="UTF-8"?> <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>springapp</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springapp</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> </web-app> |
springapp-servlet.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <?xml version='1.0' encoding='UTF-8' ?> <!-- was: <?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-40.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <!-- Most controllers will use the ControllerClassNameHandlerMapping above, but for the index controller we are using ParameterizableViewController, so we must define an explicit mapping for it. --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <!-- The index controller. --> <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController" p:viewName="index" /> </beans> |
其他相关文件:
控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; //import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; //import org.springframework.web.bind.annotation.RequestParam; /** * * @author macj7 */ @Controller public class HelloController { @RequestMapping( value ="/response", method = RequestMethod.GET) public String index( Model model) { String name ="World"; model.addAttribute("name", name ); return"response"; } } |
response.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <%-- Document : response Created on : Jul 31, 2015, 1:25:19 PM Author : macj7 --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> Spring Web MVC </head> <body> th:text="Hello, ${name}!" </body> </html> |
redirect.jsp:
1 2 3 4 5 6 7 8 9 | <%-- Views should be stored under the WEB-INF folder so that they are not accessible except through controller process. This JSP is here to provide a redirect to the dispatcher servlet but should be the only JSP outside of WEB-INF. --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% response.sendRedirect("index.htm"); %> |
如果有此问题的解决方案或更多信息
需要请告诉我。我也想解释任何答案
在我尝试学习时给出,而不仅仅是复制和粘贴解决方案。
这是我第一次尝试使用 java 创建 Web 应用程序,我必须说仅配置似乎相当令人生畏。
提前感谢您的时间和耐心。
好的,所以我找到的解决方案是将在 springapp-servlet.xml 中处理请求的控制器定义为 bean,并在控制器本身中重新定义该方法,以便它返回一个 ModelAndView (import org. springframework.web.servlet.ModelAndView;)。
我的页面现在呈现。
我应该注意到我已经重写了模型,但没有使用注释形式来编写控制器,但我很快也会以这种形式对其进行测试。现在我只是简单地实现了控制器接口。
感谢所有回复的人。感谢您的意见。
我认为在 web.xml 文件的欢迎文件列表中,您应该提供 Spring 控制器请求映射而不是 jsp 文件名,如下所示。
1 2 3 | <welcome-file-list> <welcome-file>/response</welcome-file> </welcome-file-list> |
根本原因:
注意:
在启动时加载
1 | <load-on-startup>1</load-on-startup> |
解决方案:
更改将检查每个请求的
1 2 3 4 | <servlet-mapping> <servlet-name>springapp</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> |
要访问
阅读更多关于 Web MVC 框架 - DispatcherServlet