Java 使用DispatcherServlet和AlwaySuseFillPath属性了解URL映射
我已尝试将DispatcherServlet中的URL配置为映射到不带扩展名的URL。我终于明白了这一点,但我不明白为什么URL的工作方式是这样的 假设上下文为“foobar”。。。如果DispatcherServlet的url模式是/rest/*并且我有一个/rest/js的RequestMapping,那么为了进入页面,我必须转到主机名:port/foobar/rest/rest/js。为什么URL上有一个double/rest/rest 这是没有意义的,因为如果我有多个DispatcherServlet,那么同一个RequestMapping不能转到不同的URL吗?i、 e.如果RequestMapping是'/js',我有一个DispatcherServlet设置为/rest/*和另一个设置为/json/*,那么hostname:port/foobar/rest/js和hostname:port/foobar/json/js不会返回相同的页面吗 如果@RequestMapping在类定义上,我根本无法找到URL——它只在方法上起作用 web.xml:Java 使用DispatcherServlet和AlwaySuseFillPath属性了解URL映射,java,spring,spring-mvc,Java,Spring,Spring Mvc,我已尝试将DispatcherServlet中的URL配置为映射到不带扩展名的URL。我终于明白了这一点,但我不明白为什么URL的工作方式是这样的 假设上下文为“foobar”。。。如果DispatcherServlet的url模式是/rest/*并且我有一个/rest/js的RequestMapping,那么为了进入页面,我必须转到主机名:port/foobar/rest/rest/js。为什么URL上有一个double/rest/rest 这是没有意义的,因为如果我有多个Dispatcher
<?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>
假设上下文为“foobar”。。。如果DispatcherServlet的url模式是/rest/*并且我有一个/rest/js的RequestMapping,那么为了进入页面,我必须转到主机名:port/foobar/rest/rest/js。为什么URL上有一个double/rest/rest
其思想是,您可以拥有多个DispatcherServlet
,并且必须区分其中一个和另一个
例如,您可以让一个dispatcher为documents@/documents/
提供服务,另一个为images@/images
提供服务,然后为这两个dispatcher提供一个@RequestMapping(“/whatever”)
如果双rest/rest
激怒了您,那么您可以将调度程序映射到服务/
·
。。。那么hostname:port/foobar/rest/js和hostname:port/foobar/json/js不会返回相同的页面吗
仅当您将其配置为这样做时。每个dispatcher都可以
组件扫描
不同的包,并对同一路径具有不同的请求映射。知道为什么我在类型定义中使用@RequestMapping时无法实现这一点吗?它只在方法上起作用,与我所看到的众多示例不同。它与我已经发布的代码相同,只是您将@RequestMapping(“/rest/js”)移到了类级别。我认为您需要同时注释类和方法才能起作用,否则spring将这些方法视为普通的公共方法,而不是RequestMappings。@acvcu或,引用对于基于Servlet的处理程序方法,如果没有根据指定的MethodNameResolver(默认情况下为InternalPathMethodNameResolver)显式指定路径,则在缩小范围时会考虑方法名称。请注意,这仅适用于不明确的注释映射,这些映射没有明确指定路径映射。
@acvcu因此也会考虑方法名称,但仅当映射是不明确的时。无论如何,您都必须对方法进行注释,即使您对类进行了注释。
<?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>
@Controller
public class JServicesController {
@Autowired
private TheService theService;
@ResponseBody
@RequestMapping("/rest/js")
public TheContent getTheContent() {
return theService.getTheContent();
}
}