我已经为此工作了一段时间,根本不知道出了什么问题。我正在 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 是问题,因为调试日志显示所有 URL 都将发送到调度程序 servlet——这正是我想要的。
这是我的调度程序-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
效果很好,并映射到 Controller 注释。
任何帮助将不胜感激!谢谢!
更新:以下是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]