开发者

JstlView adds path prefix recursively causing StackOverflowError

all,

I am writing a demo application to learn usage of the

org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping

class. When I run the application; I get a stack over flow error and logs show that /WEB-INF/view/ is getting recursively prefixed to the URL path. Please help in solving this.

Here's the 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>

the 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>

the Controller code

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;
  }

}

and the 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>

I get message similar to the one below in logs before my eclipse crashes:

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)

and further down the logs

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)

Seeing InternalResourceView in the stack trace when I am actually using UrlBasedViewResolver puzzles me even more. The WEB-INF/views is actually prefixed some 50 odd times before throwing the error I have pasted a smaller representation of it to avoid verbos开发者_如何学City.

Not sure what causing this effect.


Got it to work with some help from the spring forum guys, I made the following changes:

  • Removed the annotation-driven ad mvc:annotation-driven tags from the servlet-context and controllers XML respectively.

the 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>

the 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>

but the root cause was a fault in the servlet mapping, so changed the Dispatechr Servlet mapping to following in 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>

Changed /* to / which was causing the recursive addition of view since it could not find the correct one.

The spring Forum thread is here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜