[英]Spring MVC - jsp not rendering
I'm just starting with Spring MVC trying to create a new project, and came accross an issue for which no manual or tutorial seems to help... 我只是从Spring MVC开始尝试创建一个新项目,然后遇到了一个问题,该问题似乎对手册或教程都没有帮助。
I have set up a simple application with no logic, just trying to get Spring configured properly. 我已经建立了一个没有逻辑的简单应用程序,只是试图正确配置Spring。 The controller just returns the name of a view to be displayed, but the view resolver is not rendering the jsp, and returning a 404 error....
控制器仅返回要显示的视图的名称,但视图解析器未呈现jsp,并返回404错误。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
My web.xml is: 我的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>
An my openstats-servlet.xml 我的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>
The controller itself has no logic whatsoever, it's simply: 控制器本身没有任何逻辑,只是:
@Controller
public class ProductController {
@RequestMapping(value = "/products.do", method = RequestMethod.GET)
public ModelAndView listProducts(HttpServletRequest request) {
ModelAndView model = new ModelAndView("index");
return model;
}
}
The controller is reached, the issue is when attempting to render... 到达控制器,问题在于尝试渲染...
I set up log4j in debug, and this is part of what I get: 我在调试中设置了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';
02:08:19,702 DEBUG DispatcherServlet:1094-测试处理程序适配器[org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@397b6074] 02:08:19,803 DEBUG HandlerMethodInvoker:134-调用请求处理程序方法:公共org.springframework。 web.servlet.ModelAndView org.openstats.api.controller.ProductController.listProducts(javax.servlet.http.HttpServletRequest)02:08:19,833 DEBUG DefaultListableBeanFactory:1367-在名称为'index'的bean上调用afterPropertiesSet()02:08: 19,876 DEBUG InternalResourceViewResolver:81-缓存视图[index] 02:08:19,877 DEBUG DispatcherServlet:1181-呈现视图[org.springframework.web.servlet.view.JstlView:名称'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 contex
名称为'openstats'的DispatcherServlet中的URL [/jsp/index.jsp]] 02:08:19,877 DEBUG JstlView:240-渲染名称为'index'且具有模型{}和静态属性{}的视图02:08:19,923 DEBUG JstlView :234-转发到InternalResourceView'索引'中的资源[/jsp/index.jsp] 02:08:19,926调试DispatcherServlet:955-名称为'openstats'的DispatcherServlet为[/ api-server / jsp / index确定最后修改的值.jsp] 02:08:19,927 DEBUG DispatcherServlet:1054-在DispatcherServlet中测试处理程序映射[org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@440c4cee] 02:08:19,928 DEBUG DefaultAnnotationHandlerMapping:179-未找到[/jsp/index.jsp]的处理程序映射02:08:19,929 DEBUG DispatcherServlet:962-在getLastModified 02:08:19,937中未找到处理程序DEBUG DispatcherServlet:781-名称为[/ api的名称为'openstats'的DispatcherServlet处理请求-server / jsp / index.jsp] 02:08:19,938 DEBUG DispatcherServlet:843-绑定请求上下文 t to thread: GET /api-server/products.do HTTP/1.1
t到线程:GET /api-server/products.do HTTP / 1.1
My jsp folder is right within "webapp" and the index.jsp file exists. 我的jsp文件夹就在“ webapp”内,并且存在index.jsp文件。
Thanks in advance. 提前致谢。
I do have the same problem with Spring 3.x. 我确实对Spring 3.x有同样的问题。 Any progress so far?
到目前为止有什么进展吗?
EDIT: I figured it out myself :-) I used the following servletmapping: 编辑:我自己弄清楚了:-)我使用以下servletmapping:
<servlet-mapping>
<servlet-name>spring-frontcontroller</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Editing the url-pattern to eg *.do fixes the problem of not rendering the JSP. 将url模式编辑为* .do可解决不呈现JSP的问题。 But this leaves the question how this is possible with your url-pattern.
但这留下了问题,如何使用您的url模式。
Does your web.xml define index.jsp in the welcome-file-list, if so it may be getting overridden. 您的web.xml是否在welcome-file-list中定义index.jsp,如果这样,它可能会被覆盖。 Try changing the jsp name to products.jsp.
尝试将jsp名称更改为products.jsp。
eg 例如
@Controller
public class ProductController {
@RequestMapping(value = "/products.do", method = RequestMethod.GET)
public String handleRequest() {
return "products";
}
}
doesn't your .jsp and your .do conflict? .jsp和.do不会冲突吗? since a file cannot end with both .jsp and .do .. therefore it will never resolve... so you should get rid of .jsp or change your url pattern to /*
由于文件不能同时以.jsp和.do结尾。因此它将永远无法解析...因此您应该摆脱.jsp或将网址格式更改为/ *
Changing 改变中
import org.springframework.web.servlet.ModelAndView;
by 通过
import org.springframework.web.portlet.ModelAndView;
Works in my case. 就我而言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.