【问题标题】:SpringMVC: DispatcherServlet makes extra requests to viewSpringMVC:DispatcherServlet 发出额外的查看请求
【发布时间】:2012-05-18 23:21:04
【问题描述】:

我在使用 Spring MVC 时遇到了一个奇怪的问题。我有一个像这样的简单控制器:

@Controller
@RequestMapping("admin")
public class AdminController {

@RequestMapping(value = "", method = RequestMethod.GET)
public String home() {
    return "home";
}

当我运行我的服务器并访问 url: localhost/admin 时,我收到 404 错误。视图 home.jsp 存在并且应该被渲染。当我检查我的 Spring 事件日志时,会显示以下内容:

DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/admin]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /admin
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public java.lang.String be.roots.buildinginspector.web.controller.AdminController.home()]
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'adminController'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/admin] is: -1
DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'home'; URL [home]] in DispatcherServlet with name 'appServlet'
DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'domainOfExpertise' of type [be.roots.buildinginspector.business.model.DomainOfExpertise] to request in view with name 'home'
DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'org.springframework.validation.BindingResult.domainOfExpertise' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'home'
DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [home] in InternalResourceView 'home'
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/home]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /home
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/home]
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/home] in DispatcherServlet with name 'appServlet'

一切都得到了正确处理,但不仅仅是显示视图,DispatcherServlet 向所请求视图名称的 url 发出一个新的 GET 请求。

我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/spring/config-core-business.xml
                 classpath*:/spring/config-app-security.xml
    </param-value>
</context-param>

<!-- Spring Security filter -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<!-- 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/appServlet/config-core-web.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>

<filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

相关的spring上下文部分(config-core-web.xml):

<resources mapping="/resources/**" location="../../../resources" />

<!-- 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="prefix" value="/WEB-INF/views/"/>
    <beans:property name="suffix" value=".jsp"/>
</beans:bean>

【问题讨论】:

标签: spring spring-mvc


【解决方案1】:

试试这个:

@RequestMapping(value = "admin",
        method = {RequestMethod.GET, RequestMethod.POST })

【讨论】:

    【解决方案2】:

    毕竟这是一个与 tomcat 相关的问题。出于某种原因,当我在 IDE 中重新创建配置时,错误已解决。感谢您的帮助。

    【讨论】:

    【解决方案3】:

    在您的方法上定义的请求映射注释限制您的控制器响应以“/admin/home”开头的请求。

    我会应用以下修改:

    @Controller
    @RequestMapping("/admin")
    public class AdminController {
    
       @RequestMapping(method = RequestMethod.GET)
       public String home() {
          return "home";
       }
    }
    

    【讨论】:

      【解决方案4】:
      @Controller
      @RequestMapping("admin")
      public class AdminController {
      
      @RequestMapping(method = RequestMethod.GET)
      public String home() {
          return "home";
      }
      

      为 home() 函数移除 @RequestMapping 的 'value' 属性。

      【讨论】:

      • 这仍然给出了同样奇怪的行为。日志显示相同:找到了正确的控制器/视图,但是在它尝试显示它之后,对控制器方法返回的 url 发出另一个 get 请求。
      • config-core-web.xml 文件中的内容。可能你也在那个文件中给出
      • 移除 hiddenHttpMethodFilter 并尝试一次。
      • config-core-web.xml 包含我的视图解析器和资源映射
      【解决方案5】:

      我认为这是 web.xml 中的 servlet 映射问题。仅在 web.xml 中将其更改为 /admin 地址。也许现在你有:

      <url-pattern>*</url-pattern>
      

      改成:

      <url-pattern>/admin/*</url-pattern>
      

      【讨论】:

      • 我的 是 /,在我访问过的几个网站上都推荐了它
      • @geoffreydv:是的。但是所有请求都将使用 spring DispatcherServlet 处理。所以你不必惊讶你有两个 DispatcherServlet 调用。而且,好的,您将定义处理 /home url 并返回“newurl”的控制器。它仍然转到 Dispatcher servlet。接下来你会做什么?
      猜你喜欢
      • 2012-02-16
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多