我的目标:
http://host:port/myApp/about
返回一个名为的jsp页面about.jsp
http://host:port/myApp/about/log
是返回 JSON 的服务调用
我的 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" ...>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4jExposeWebAppRoot</param-name>
<param-value>false</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<servlet-name>myApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
...
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myApp</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
我的 servlet.xml(使用 Spring 3.0.3.RELEASE):
<beans ...>
<mvc:annotation-driven />
<context:component-scan ... />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="15728640" /> <!-- 15 MB -->
<property name="MaxInMemorySize" value="15728640" /> <!-- 15 MB -->
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="2" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
<property name="defaultContentType" value="application/json" />
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
</constructor-arg>
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
</beans>
我的控制器:
@Controller
@RequestMapping("about")
public class AboutController {
@RequestMapping(method=GET)
public String getStats() {
return "about";
}
@RequestMapping(value="log", method=GET)
public @ResponseBody List<String> getLog() {
...
}
}
在服务器(jetty 6,servlet-api 2.5)启动时,我看到:
INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/about/log] onto handler [AboutController@63843ca4]
INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/about/log.*] onto handler [AboutController@63843ca4]
INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/about/log/] onto handler [AboutController@63843ca4]
INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/about] onto handler [AboutController@63843ca4]
INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/about.*] onto handler [AboutController@63843ca4]
INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/about/] onto handler [AboutController@63843ca4]
上述目标的设置结果:
产生错误:
WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myApp/WEB-INF/views/about.jsp] in DispatcherServlet with name 'myApp'
- 按预期工作
我检查过/尝试过的事情:
WEB-INF/views/about.jsp
确实存在- 我已经在我的控制器中为这两种方法设置了断点,并且它们都被适当地调用了;退出我的控制器类后的某个时间发生故障
- 的顺序
InternalResourceViewResolver
被故意设置为第一个。我知道Spring 文档建议不要这样做,但是当我尝试把它放在最后时,我得到一个空{ }
返回,我认为这是因为ContentNegotiatingViewResolver
拿起“about”字符串并以某种方式解释它。我对订单没有要求,但ContentNegotiatingViewResolver
需要保持原样的设置(除非绝对有必要更改它们)。 - 将 web.xml 中的 url-pattern 设置为
<url-pattern>/</url-pattern>
,但这会破坏对静态资源的引用(这不是一个选项,因为这些资源当前正在被生产中的许多第 3 方使用) 将 web.xml 中的 url-pattern 设置为
<url-pattern>/about/*</url-pattern>
,但随后我得到与上面列出的相反的结果:- 按预期工作
产生错误:
WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myApp/about/log.json] in DispatcherServlet with name 'myApp'
有趣的是,该 url
http://host:port/myApp/about/about/log
确实适用于这个 url-pattern
所以,我有点坚持要解决什么才能实现既定目标。