views:

45

answers:

2

Hi there,

I have a problem with my spring controller/request mapping struture. In web.xml I have defined 2 dispatcher servlets, that map the following request paths:

  1. Servlet: /pathA/*
  2. Servlet /pathB/*

All my controllers are defined in the package com.myproject.controllers, so both controllers serving for paths under /pathA/* and /pathB/*. I am doing a component-scan in both of my servlets. How do I need to set the ReuestMapping annotations for the following Controller:

@Controller
public class MyController {

  // /pathA
  public void action1() {
  }

  // /pathA/action2
  public void action2() {
  }
}

I really get confused here, i have tried so many different things, I hope you can help me!

Sincerely, Heinrich

A: 

try smt like this

 @RequestMapping(method = RequestMethod.GET, value = "/pathA")
 @RequestMapping(method = RequestMethod.GET, value = "/pathA/bla-bla")

if it wouldn't help there is some variant with urlrewriters.

Stas
Okay, would it maybe help to merge all my servlets into a single with requestPath "/*"? So I could give the paths you mentioned and it should work, shouldn't it?
Heinrich
@Heinrich: how many servlets do you have?
Stas
Currently 5, but 4 of them are Spring Dispatcher Servlets. I am not sure if this is a good practice in Spring? More. Or less they just sudivide the request paths space
Heinrich
@Heinrich: try read this http://forum.springsource.org/showthread.php?t=10366 , there're some advantages about several dispatcher servlets. Also, I dont think that it is a good practice, but it's only my imho.
Stas
A: 

If you actually need to use several DispatcherServlet, perhaps the best approach is to place their controllers into separate packages and limit component scan of each servlet to its own package.

If you can't do it for some reason, you can configure your servlets as follows:

<bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name = "alwaysUseFullPath" value = "true" />
</bean>

In this case you can use servlet paths in @RequestMapping, as stas showed.

axtavt
Ah! This is exactly what I was searching for. I saw this alwaysUseFullPath property already in the SimpleurlhandlerMapping, but wasn't aware that it could be used as shown too.
Heinrich