Spring mvc 3.1 tomcat 7-404不解析jsp但控制器注释工作正常

Posted

技术标签:

【中文标题】Spring mvc 3.1 tomcat 7-404不解析jsp但控制器注释工作正常【英文标题】:Spring mvc 3.1 tomcat 7 - 404 not resolving jsp but controller annotations working fine 【发布时间】:2012-11-11 14:09:13 【问题描述】:

我已经为此工作了一段时间,根本不知道出了什么问题。我正在 Tomcat 7 上部署 Spring 3.1 MVC 应用程序。通过查看 DEBUG 输出,我可以看到 1)MVC 调度程序 servlet 根据需要获取我的所有 URL 2)映射到控制器注释的任何 URL 都可以正常工作 3)任何 URL需要映射到 JSP 得到 404。另外,我使用的是 JDK 1.7.0_09。我的 JSP 页面在 /WEB-INF/jsp/ 中。

提前感谢我可能遗漏的任何提示!

这是我的 web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app 
    id="WebApp_ID" 
    version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name></display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

我尝试将我的 URL 模式修改为 /、/、/.、. “/”似乎是我迄今为止最远的地方。另外,我可能是错的,但我认为 URL 不是问题,因为 DEBUG 日志显示所有 URL 都将发送到调度程序 servlet——这正是我想要的。

这是我的 dispatcher-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"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <!--Tell the servlet where to look for annotated methods-->
    <context:component-scan base-package="com.test.web.mvc.ctrl" />

    <!--Enables many annotations and searches for @Controller annotated methods etc.. -->
    <context:annotation-config />

    <!--JSR-303 (Bean validation) support will be detected on classpath and enabled automatically-->
    <mvc:annotation-driven />

    <!--This tag allows for mapping the DispatcherServlet to "/" (all extensions etc)-->
    <mvc:default-servlet-handler/>

    <!-- ControllerClassNameHandlerMapping -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
</beans>

我尝试通过http://localhost:8080/hello 的网址发送。我的应用程序部署到“/”并且解析器没有将“/home”视图映射到/WEB-INF/jsp/hello.jsp,尽管调试日志显示它正确地确认“/home”是传入视图名字。

http://localhost:8080/rest/user/bob_user 效果很好,并且被映射到一个控制器注解。

任何帮助将不胜感激!谢谢!

更新: 这是http://localhost:8080/hello 的调试日志,应该转换为 /WEB-INF/jsp/hello.jsp

2012-11-23 03:45:50,089 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/hello]
2012-11-23 03:45:50,089 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@5c9a010c] in DispatcherServlet with name 'dispatcher'
2012-11-23 03:45:50,089 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /hello
2012-11-23 03:45:50,093 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/hello]
2012-11-23 03:45:50,093 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@49049c10] in DispatcherServlet with name 'dispatcher'
2012-11-23 03:45:50,093 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - No handler mapping found for [/hello]
2012-11-23 03:45:50,094 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@17bd11ee] in DispatcherServlet with name 'dispatcher'
2012-11-23 03:45:50,094 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping - No handler mapping found for [/hello]
2012-11-23 03:45:50,094 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@f212a54] in DispatcherServlet with name 'dispatcher'
2012-11-23 03:45:50,094 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/hello] are [/**]
2012-11-23 03:45:50,094 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/hello] are 
2012-11-23 03:45:50,094 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/hello] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@4a61f88e] and 1 interceptor
2012-11-23 03:45:50,094 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@1a33f07b]
2012-11-23 03:45:50,094 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@47f128ad]
2012-11-23 03:45:50,094 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/hello] is: -1
2012-11-23 03:45:50,095 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
2012-11-23 03:45:50,095 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@2f38fd40
2012-11-23 03:45:50,095 [http-bio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
2012-11-23 03:45:50,095 [http-bio-8080-exec-8] DEBUG org.springframework.web.context.support.XmlWebApplicationContext - Publishing event in WebApplicationContext for namespace 'dispatcher-servlet': ServletRequestHandledEvent: url=[/hello]; client=[127.0.0.1]; method=[GET]; servlet=[dispatcher]; session=[D81D9C8BE397827739EB48BC3BB7A35D]; user=[null]; time=[7ms]; status=[OK]
2012-11-23 03:45:50,095 [http-bio-8080-exec-8] DEBUG org.springframework.web.context.support.XmlWebApplicationContext - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/hello]; client=[127.0.0.1]; method=[GET]; servlet=[dispatcher]; session=[D81D9C8BE397827739EB48BC3BB7A35D]; user=[null]; time=[7ms]; status=[OK]

【问题讨论】:

您是否启用了日志记录?日志说什么? 我将更新帖子以反映调试日志... 控制器返回"home",对吧? “hello”没有控制器/请求映射。我希望它直接解析为 /WEB-INF/jsp/hello.jsp 但它会发回 404。 so.. 我通过创建一个具有@RequestMapping("/viewName") 方法的新控制器类解决了这个问题。 Spring MVC 的匹配方式,这应该是最后也是最通用的匹配。结果,如果我得到 URL,它将尝试首先匹配其他控制器,然后最后匹配这个。当它匹配时,我只返回一个带有视图名称的字符串,因此它最终会转到 /WEB-INF/jsp/view-name.jsp。有人有更好的主意吗?谢谢! 【参考方案1】:

也许你的 mvc:annotation-driven 覆盖了你的 RequestMappingHandlerMapping。

还要从组件扫描中排除 @Controller 类:

<context:component-scan base-package="org.example">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

因为控制器已经被

找到了

【讨论】:

【参考方案2】:

所以.. 我通过创建一个具有@RequestMapping("/viewName") 方法的新控制器类解决了这个问题。 Spring MVC 匹配的方式,这应该是最后也是最通用的匹配。结果,如果我得到 URL,它将尝试首先匹配其他控制器,然后最后匹配这个。当它匹配时,我只返回一个带有视图名称的字符串,因此它最终会转到/WEB-INF/jsp/[view-name].jsp。有人有更好的主意吗?谢谢!

【讨论】:

以上是关于Spring mvc 3.1 tomcat 7-404不解析jsp但控制器注释工作正常的主要内容,如果未能解决你的问题,请参考以下文章

Spring MVC项目无法部署到tomcat中运行。

内存泄漏 - Tomcat、Spring MVC

使用 Spring MVC 在 tomcat 中的 PATCH 方法

Spring MVC 使用 STS 和 tomcat devloader

spring MVC tomcat启动 执行

带有 Spring Boot 的 Spring MVC 不适用于 Eclipse 的 Tomcat 服务器