我試圖配置DispatcherServlet中的URL以映射到沒有擴展名的URL。我終於明白了這一點,但我不明白爲什麼URL正在按照這種方式工作。瞭解使用DispatcherServlet使用alwaysUseFullPath屬性的URL映射
假設'foobar'的上下文...如果DispatcherServlet的url模式是/ rest/*並且我有一個/ rest/js的RequestMapping,那麼爲了進入頁面,我必須轉到主機名:port/foobar/rest/rest/js。爲什麼網址有雙重/休息/休息?
這沒有任何意義,因爲如果我有多個DispatcherServlets,不能將相同的RequestMapping轉到不同的URL嗎?即如果RequestMapping是'/ js',並且我有一個DispatcherServlet設置爲/ rest/*,另一個設置爲/ json/*,那麼hostname:port/foobar/rest/js和hostname:port/foobar/json/js返回相同的頁面?
如果@RequestMapping位於類定義上,我無法得到URL,它只能在方法上使用。
web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:beans.xml
</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
調度-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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="my.controller" />
<mvc:annotation-driven />
<!-- Handlers -->
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="alwaysUseFullPath" value="true" />
</bean>
<!-- View Resolvers -->
<bean id="defaultViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
JServicesController.java:
@Controller
public class JServicesController {
@Autowired
private TheService theService;
@ResponseBody
@RequestMapping("/rest/js")
public TheContent getTheContent() {
return theService.getTheContent();
}
}
任何想法爲什麼當我將@RequestMapping放在類型定義上時,我無法完成這項工作?它只適用於該方法,而不像我見過的衆多示例。 – acvcu 2012-02-03 17:26:42
粘貼相關代碼 – soulcheck 2012-02-03 17:47:00
這是我已發佈的相同代碼,除非您將@RequestMapping(「/ rest/js」)移至課程級別。 – acvcu 2012-02-03 17:57:13