JstlView以递归方式添加路径前缀,从而导致StackOverflowError

时间:2011-08-23 06:05:14

标签: spring-mvc jstl stack-overflow

所有

我正在编写一个演示应用程序来学习

的用法
org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping

类。当我运行应用程序时;我得到一个堆栈溢出错误,日志显示/ WEB-INF / view /以递归方式为URL路径加上前缀。请帮忙解决这个问题。

这是servlet-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->

  <!-- Enables the Spring MVC @Controller programming model -->
  <annotation-driven />

  <beans:bean id="projectDao" class="chomu.dao.ProjectDaoImpl" />
  <beans:bean id="projectService" class="chomu.service.ProjectPersistenceService" />

  <!-- Bind to a naming convention for controllers and views -->
  <!-- p:interceptors-ref="localeChangeInterceptor" -->
  <beans:bean id="classnameControllerMappings"
    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"
    p:order="1" p:caseSensitive="true">
    <beans:property name="defaultHandler">
      <beans:bean
        class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    </beans:property>
  </beans:bean>

  <!-- Enables annotated POJO @Controllers -->
  <beans:bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

  <!-- Enables plain Controllers -->
  <beans:bean
    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

  <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
  <beans:bean
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <beans:property name="viewClass"
      value="org.springframework.web.servlet.view.JstlView" />
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
  </beans:bean>

  <!-- Imports user-defined @Controller beans that process client requests -->
  <beans:import resource="controllers.xml" />

</beans:beans>

controllers.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

  <!-- Scans within the base package of the application for @Components to 
    configure as beans -->
  <context:component-scan base-package="chomu" />

  <tx:annotation-driven proxy-target-class="true" />
  <mvc:annotation-driven />

  <mvc:resources mapping="/resources/**" location="/resources/" />

  <bean id="validator"
    class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</beans>

控制器代码

package chomu.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import chomu.domain.Project;
import chomu.service.ProjectPersistenceService;

/**
 * @author amisr1
 * @dateCreated Aug 18, 2011
 * @version $Revsion:$
 * 
 *          $LastChangedBy:$
 * @since
 */
@Controller
public class ProjectController {

  @Autowired
  private ProjectPersistenceService projectService;

  /**
   * @return the projectService
   */
  public ProjectPersistenceService getProjectService() {
    return projectService;
  }

  /**
   * @param projectService
   *          the projectService to set
   */
  public void setProjectService(ProjectPersistenceService projectService) {
    this.projectService = projectService;
  }

  @RequestMapping(method=RequestMethod.GET)
  public List<Project> list(){
    List<Project> projectList = new ArrayList<Project>();
    projectList.add(projectService.getProject(2L));
    projectList.add(projectService.getProject(3L));
    return projectList;
  }

}

和jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"
    xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.0">
    <jsp:directive.page contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1" session="false" />
    <jsp:output doctype-root-element="html"
        doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
        omit-xml-declaration="true" />
    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Insert title here</title>
</head>
<body>
    <table>
        <c:forEach var="project" items="${projectList}">
            <tr>
                <td>${project.id}</td>
                <td>${project.name}</td>
            </tr>
        </c:forEach>
    </table>
</body>
    </html>
</jsp:root>

在我的eclipse崩溃之前,我在日志中收到类似下面的消息:

DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/views/'
DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/views/WEB-INF/views/'
DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/views/WEB-INF/views/WEB-INF/views/project/list'
Aug 22, 2011 8:02:35 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet appServlet threw exception
java.lang.StackOverflowError
    at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:505)
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
    at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:547)
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
    at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:547)

并进一步记录日志

at org.springframework.web.context.request.ServletRequestAttributes.updateAccessedSessionAttributes(ServletRequestAttributes.java:222)
    at org.springframework.web.context.request.AbstractRequestAttributes.requestCompleted(AbstractRequestAttributes.java:48)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:664)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

当我实际使用UrlBasedViewResolver时,在堆栈跟踪中看到InternalResourceView让我更加困惑。 WEB-INF / views实际上是前缀大约50多次,然后抛出错误我粘贴了一个较小的表示,以避免冗长。

不确定导致此效果的原因。

1 个答案:

答案 0 :(得分:0)

得到了春季论坛人员的一些帮助,我做了以下更改:

  • 分别从servlet-context和controllers XML中删除了注释驱动的广告mvc:annotation-driven标签。

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->

  <beans:bean id="projectDao" class="chomu.dao.ProjectDaoImpl" />
  <beans:bean id="projectService" class="chomu.service.ProjectPersistenceService" />

  <!-- Bind to a naming convention for controllers and views -->
  <!-- p:interceptors-ref="localeChangeInterceptor" -->
  <beans:bean id="classnameControllerMappings"
    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"
    p:order="1" p:caseSensitive="true">
  <beans:property name="defaultHandler">
    <beans:bean
      class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
  </beans:property>
  </beans:bean>

  <!-- Enables annotated POJO @Controllers -->
  <beans:bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

  <!-- Enables plain Controllers -->
  <beans:bean
    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

  <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
  <beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="viewClass"
      value="org.springframework.web.servlet.view.JstlView" />
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
  </beans:bean>

  <!-- Imports user-defined @Controller beans that process client requests -->
  <beans:import resource="controllers.xml" />

</beans:beans>

controllers.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

  <!-- Scans within the base package of the application for @Components to 
    configure as beans -->
  <context:component-scan base-package="chomu" />

  <mvc:resources mapping="/resources/**" location="/resources/" />

  <bean id="validator"
    class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</beans>

但根本原因是servlet映射出错,因此将Dispatechr Servlet映射更改为web.xml中的以下内容

    <!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/app/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>
  

将/更改为/导致递归添加视图,因为它无法找到正确的视图。

春季论坛主题是here