简体   繁体   中英

Spring DispatcherServlet

I have an application that uses Spring with the following configuration

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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">

<mvc:annotation-driven/>
<mvc:resources mapping="/*" location="/" />
<context:component-scan base-package="controller" />

And this is the whole web.xml file

<web-app>
<servlet>
    <servlet-name>springrest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springrest</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

The application runs very well on localhost:8080/web, but I want to change it to localhost:8080/web/rest and for doing that I tried to change the url pattern from

<servlet-mapping>
    <servlet-name>springrest</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

to

<servlet-mapping>
    <servlet-name>springrest</servlet-name>
    <url-pattern>/rest</url-pattern>
</servlet-mapping>

But unfortunately it doesn't work, what am I doing wrong?

With your change you are filtering the URL intercepted by the DispatcherServlet to be only those starting with /rest and not all / as before, so you are just reducing the number of URL handled by Spring, not changing mappings. This is not what you want, I am afraid is not possible to "move down one level" (from /web to /web/rest ) a java web application with some web.xml configuration, you should rewrite your mappings in Spring configuration (what was / is now /rest , what was /foo is now /rest/foo ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM