kaka2008
kaka2008

Reputation: 693

how to config many DispatcherServlet in spring 3 mvc?

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

Upvotes: 2

Views: 6965

Answers (3)

Erdnase
Erdnase

Reputation: 790

Having multiple servlets is not recommended. The servlets once created will not die and will remain in memory untill application is restarted. This will cause memory to be used by servlets who have completed their purpose.

Therefore it is advised to have only one servlet which is called the Front Controller. It should control all the requests. For additional urls, map them using @RequestMapping mentioned in NA's answer.

Upvotes: 0

NA.
NA.

Reputation: 6579

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.

Upvotes: 3

Kaleb Brasee
Kaleb Brasee

Reputation: 51945

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.

Upvotes: 5

Related Questions