I’m currently working on a Spring MVC application and as I mapped, within the web.xml file, all incoming URL to a single DispatcherServlet, I wanted to know whether it would be possible to retrieve the URI that has been effectively mapped. Here’s an example to illustrate my concerns :
import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping(method={GET})
public String processAllRequest(){
return "viewName";
}
}
Since I’ve defined the URL-MAPPING in the web.xml as being “/*”, all incoming requests will end up in my controller class, show above. For instance, both the following requests will be processed by the processAllRequest() method from my controller.
- myApplicationContext/home
- myApplicationContext/logout
Is it possible, somehow, to retrieve the mapped URI? That is, once I’m inside the processAllRequest(), how could I know if it’s been called for …/home or …/logout?
Is it possible to retrieve this kind of info by injecting an HttpServletRequest or another object as argument of the method?
I might have formulated my question in an ambiguous way but what I was looking for, was rather path variable. So my problem was solved this way :
@Controller
public class HomeController {
}
With this solution whenever I request the following requests, the uri argument from my processMappedUri() method will hold the variable value :