Understanding URL mapping using DispatcherServlet

2020-07-23 08:52发布

问题:

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.

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?

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?

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:

<?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();
    }
}

回答1:

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?

The idea is that you can have multiple DispatcherServlets and you have to differentiate between one and another.

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 /·

... then wouldn't hostname:port/foobar/rest/js and hostname:port/foobar/json/js return the same page?

Only if you configure them to do so. Each dispatcher can component-scan different packages and have different request mappings for the same path.