tags:

views:

581

answers:

2

i'm using spring 3 recently. i want to use REST. the problem is ,i want to use many different path.like notice/* ,user/* etc. i know how to config one .

<servlet>
   <servlet-name>notice</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>notice</servlet-name>
  <url-pattern>/notice/*</url-pattern>
</servlet-mapping>

so,if i want to add /user/* in the web.xml,what should i do? how to configue? thanks

A: 

Just create new servlet and servlet-mapping elements in web.xml for the user servlet:

<!-- notice servlet and servlet-mapping ... -->

<servlet>
   <servlet-name>user</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>user</servlet-name>
   <url-pattern>/user/*</url-pattern>
</servlet-mapping>

Then create the required user-servlet.xml Spring config file and put it in the same location as the existing notice-servlet.xml, so that the user DispatcherServlet can load its configuration.

Kaleb Brasee
+1  A: 

Do you really want to have several dispatcher servlets? I suggest mapping the dispatcher to /

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

And then in your controllers you map to the different "sub urls". For example @RequestMapping(value = "/users", method = RequestMethod.GET) to map your users. The reference manual does a good job of explaining how you can map urls.

NA