使用alwaysUseFullPath属性了解使用DispatcherServlet的URL映射 [英] Understanding URL mapping using DispatcherServlet using alwaysUseFullPath property
问题描述
我尝试在DispatcherServlet中配置URL以映射到没有扩展名的URL。我终于明白了这一点,但我不明白为什么URL正在以它的方式运行。
I have tried to configure the URLs in my DispatcherServlet to map to URLs without an extension. I finally figured this out, but I don't understand why the URL is working out the way it is.
假设'foobar'的上下文......如果DispatcherServlet的url-pattern是/ rest / *,我有一个RequestMapping为/ rest / js,然后为了进入页面,我必须转到hostname:port / foobar / rest / rest / js。为什么URL上有双/休息/休息?
Assuming a context of 'foobar'... if the url-pattern for the DispatcherServlet is /rest/* and I have a RequestMapping of /rest/js, then in order to get to the page, I have to go to hostname:port/foobar/rest/rest/js . Why does the URL have a double /rest/rest on it?
这没有意义,因为如果我有多个DispatcherServlets,那么同一个RequestMapping不能转到不同的URL吗?即如果RequestMapping为'/ js',并且我将DispatcherServlet设置为/ rest / *而另一个设置为/ json / *,则不会使用hostname:port / foobar / rest / js和hostname:port / foobar / json / js返回同一页?
This doesn't make sense, because if I have multiple DispatcherServlets, couldn't the same RequestMapping go to different URLs? i.e. if the RequestMapping is '/js', and I have a DispatcherServlet set to go to /rest/* and another set to /json/*, then wouldn't hostname:port/foobar/rest/js and hostname:port/foobar/json/js return the same page?
如果@RequestMapping在类定义上,我根本无法获得URL - 它只能起作用关于方法。
I can't get the URL to come up at all if the @RequestMapping is on the class definition - it only works on the method.
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>
dispatcher-servlet.xml:
dispatcher-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:
JServicesController.java:
@Controller
public class JServicesController {
@Autowired
private TheService theService;
@ResponseBody
@RequestMapping("/rest/js")
public TheContent getTheContent() {
return theService.getTheContent();
}
}
推荐答案
假设'foobar'的上下文...如果DispatcherServlet的url-pattern是/ rest / *并且我有一个RequestMapping为/ rest / js,那么为了到达页面,我必须去主机名:port / foobar / rest / rest / js。为什么URL上有双/休息/休息?
Assuming a context of 'foobar'... if the url-pattern for the DispatcherServlet is /rest/* and I have a RequestMapping of /rest/js, then in order to get to the page, I have to go to hostname:port/foobar/rest/rest/js . Why does the URL have a double /rest/rest on it?
这个想法是你可以有多个 DispatcherServlets
,你必须区分彼此。
The idea is that you can have multiple DispatcherServlets
and you have to differentiate between one and another.
你可以,例如让一个调度员提供文件@ / documents /
和另一个服务图像@ / images
然后有一个 @RequestMapping(/ whatever)
为他们两人提供不同的东西。
You can, for example have one dispatcher serving documents @ /documents/
and another serving images @ /images
and then have a @RequestMapping("/whatever")
for both of them serving different things.
如果双休息/休息
会让你烦恼,然后你可以将你的调度员映射到服务 /
·
If double rest/rest
irritates you, then you can map your dispatcher to serve /
·
...然后不会't hostname:port / foobar / rest / js和hostname:port / foobar / json / js返回同一页?
... then wouldn't hostname:port/foobar/rest/js and hostname:port/foobar/json/js return the same page?
仅限如果你配置他们这样做。每个调度程序都可以 component-scan
不同的包,并为同一路径提供不同的请求映射。
Only if you configure them to do so. Each dispatcher can component-scan
different packages and have different request mappings for the same path.
这篇关于使用alwaysUseFullPath属性了解使用DispatcherServlet的URL映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!