Spring MVC - jsp无法渲染

Joh*_*nco 3 java spring-mvc

我刚刚开始尝试使用Spring MVC创建一个新项目,并且遇到了一个问题,没有任何手册或教程似乎有帮助......

我已经设置了一个没有逻辑的简单应用程序,只是尝试正确配置Spring.控制器只返回要显示的视图的名称,但视图解析器不呈现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>
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

控制器本身没有任何逻辑,它只是:

@Controller
public class ProductController {

    @RequestMapping(value = "/products.do", method = RequestMethod.GET)
    public ModelAndView listProducts(HttpServletRequest request) {

        ModelAndView model = new ModelAndView("index");
        return model;
    }
}
Run Code Online (Sandbox Code Playgroud)

到达控制器,问题是在尝试渲染时......

我在调试中设置了log4j,这是我得到的一部分:

02:08:19,702 DEBUG DispatcherServlet:1094 - 测试处理程序适配器[org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@397b6074] 02:08:19,803 DEBUG HandlerMethodInvoker:134 - 调用请求处理程序方法:public org.springframework. web.servlet.ModelAndView org.openstats.api.controller.ProductController.listProducts(javax.servlet.http.HttpServletRequest)02:08:19,833 DEBUG DefaultListableBeanFactory:1367 - 在bean上调用afterPropertiesSet(),名称为'index'02:08: 19,876 DEBUG InternalResourceViewResolver:81 - 缓存视图[索引] 02:08:19,877 DEBUG DispatcherServlet:1181 - 渲染视图[org.springframework.web.servlet.view.JstlView:name'index'; DispatcherServlet中的URL [/jsp/index.jsp]],名称为'openstats'02:08:19,877 DEBUG JstlView:240 - 使用模型{}和静态属性{} 02:08:19,923 DEBUG JstlView呈现名称为'index'的视图:234 - 转发到InternalResourceView'index'中的资源[/jsp/index.jsp] 02:08:19,926 DEBUG DispatcherServlet:955 - 名称为'openstats'的DispatcherServlet确定[/ api-server/jsp/index的Last-Modified值.jsp] 02:08:19,927 DEBUG DispatcherServlet:1054 - 在DispatcherServlet中测试处理程序映射[org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@440c4cee],名称为'openstats'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 - 名称为'openstats'的DispatcherServlet处理请求[/ api -server/jsp/index.jsp] 02:08:19,938 DEBUG DispatcherServlet:843 - 绑定请求上下文 t to thread:GET /api-server/products.do HTTP/1.1

我的jsp文件夹在"webapp"中,并且index.jsp文件存在.

提前致谢.

Bas*_*Bas 5

我对Spring 3.x也有同样的问题.到目前为止有进展吗?

编辑: 我自己想通了:-)我使用了以下servletmapping:

  <servlet-mapping>
    <servlet-name>spring-frontcontroller</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

将url-pattern编辑为例如*.do可以解决不呈现JSP的问题.但这就留下了一个问题,即你的url模式是如何实现的.

  • 我也有这个确切的问题,谢谢你指出来!我想我将不得不接受延期. (2认同)