2012-02-03 237 views
1

我试图配置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/foob​​ar/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(); 
    } 
} 

回答

2

假设 'foobar的' 的上下文.. 。如果DispatcherServlet的url模式是/ rest/*,并且我有一个RequestMapping/rest/js,那么为了进入页面,我必须去hostname:port/foobar/rest/rest/js。为什么网址有双重/休息/休息?

想法是,你可以有多个DispatcherServlets,你必须区分一个和另一个。

可以,例如有一个调度员送达文件@/documents/,另一个提供图片@/images,然后让他们两个服务于不同的东西@RequestMapping("/whatever")

如果双rest/rest刺激你,那么你可以映射你的调度服务/·

...以后就不会主机名:端口/ foobar的/ REST/JS和主机名:端口/ foobar的/ JSON/js返回相同的页面?

只有当您配置他们这样做。每个调度程序可以使用component-scan不同的程序包,并且对同一路径具有不同的请求映射。

+0

任何想法为什么当我将@RequestMapping放在类型定义上时,我无法完成这项工作?它只适用于该方法,而不像我见过的众多示例。 – acvcu 2012-02-03 17:26:42

+0

粘贴相关代码 – soulcheck 2012-02-03 17:47:00

+0

这是我已发布的相同代码,除非您将@RequestMapping(“/ rest/js”)移至课程级别。 – acvcu 2012-02-03 17:57:13