我刚开始使用 Spring MVC 尝试创建一个新项目,遇到了一个似乎没有任何手册或教程可以帮助解决的问题...
我建立了一个没有逻辑的简单应用程序,只是试图正确配置 Spring。 Controller 只返回要显示的 View 的名称,但 View 解析器未呈现 jsp,并返回 404 错误....
非常感谢任何帮助。
我的 web.xml 是:
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>openstats</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>openstats</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<display-name>OpenStats API Server</display-name>
</web-app>
我的 openstats-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<context:component-scan base-package="org.openstats.api.controller"/>
<!-- Enable to request mappings PER METHOD -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- Enable annotated POJO @Controller -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<!-- Define the view resolver to use jsp files within the jsp folder -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
Controller 本身没有任何逻辑,它只是:
@Controller
public class ProductController {
@RequestMapping(value = "/products.do", method = RequestMethod.GET)
public ModelAndView listProducts(HttpServletRequest request) {
ModelAndView model = new ModelAndView("index");
return model;
}
}
到达 Controller ,问题是在尝试渲染时...
我在调试中设置了 log4j,这是我得到的一部分:
02:08:19,702 DEBUG DispatcherServlet:1094 - Testing handler adapter [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@397b6074] 02:08:19,803 DEBUG HandlerMethodInvoker:134 - Invoking request handler method: public org.springframework.web.servlet.ModelAndView org.openstats.api.controller.ProductController.listProducts(javax.servlet.http.HttpServletRequest) 02:08:19,833 DEBUG DefaultListableBeanFactory:1367 - Invoking afterPropertiesSet() on bean with name 'index' 02:08:19,876 DEBUG InternalResourceViewResolver:81 - Cached view [index] 02:08:19,877 DEBUG DispatcherServlet:1181 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/jsp/index.jsp]] in DispatcherServlet with name 'openstats' 02:08:19,877 DEBUG JstlView:240 - Rendering view with name 'index' with model {} and static attributes {} 02:08:19,923 DEBUG JstlView:234 - Forwarding to resource [/jsp/index.jsp] in InternalResourceView 'index' 02:08:19,926 DEBUG DispatcherServlet:955 - DispatcherServlet with name 'openstats' determining Last-Modified value for [/api-server/jsp/index.jsp] 02:08:19,927 DEBUG DispatcherServlet:1054 - Testing handler map [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@440c4cee] in DispatcherServlet with name 'openstats' 02:08:19,928 DEBUG DefaultAnnotationHandlerMapping:179 - No handler mapping found for [/jsp/index.jsp] 02:08:19,929 DEBUG DispatcherServlet:962 - No handler found in getLastModified 02:08:19,937 DEBUG DispatcherServlet:781 - DispatcherServlet with name 'openstats' processing request for [/api-server/jsp/index.jsp] 02:08:19,938 DEBUG DispatcherServlet:843 - Bound request context to thread: GET /api-server/products.do HTTP/1.1
我的 jsp 文件夹就在“webapp”中,并且存在 index.jsp 文件。
提前致谢。
最佳答案
我在使用 Spring 3.x 时也遇到了同样的问题。 目前有什么进展吗?
编辑: 我自己弄明白了 :-) 我使用了以下 servletmapping:
<servlet-mapping>
<servlet-name>spring-frontcontroller</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
将 url-pattern 编辑为例如*.do 修复了不呈现 JSP 的问题。 但这留下了一个问题,即如何使用您的 url 模式实现这一点。
关于java - Spring MVC - jsp 不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1576223/