Back to this issue...
I found a solution which looks a little bit kludgy but at least works (at least for me :-)
First, the controller must implement BeanNameAware and ApplicationContextAware, then add the following method to it:
Code:
private void addControllerMappingToRequest(HttpServletRequest request) throws Exception {
MySimpleUrlHandlerMapping handlerMapping = (MySimpleUrlHandlerMapping) applicationContext.getBean("urlMapping");
String mapping = handlerMapping.getMappingForBean(beanName);
log.debug("Mapping for " + beanName + " is " + mapping);
request.setAttribute(REQUEST_PATH_MAPPING, mapping);
}
MySimpleUrlHandlerMapping extends Spring's SimpleUrlHandlerMapping to expose path to controller hashmap, therefore you have to use it in your *-servlet.xml context xml file:
Code:
<bean id="urlMapping" class="MySimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="some/mapping/**">someController</prop>
<prop key="another/mapping/**">anotherController</prop>
MySimpleUrlHandlerMapping wraps some setter methods and adds a new one, getMappingForBean(beanName):
Code:
public class ImsSimpleUrlHandlerMapping extends SimpleUrlHandlerMapping {
private final Map localUrlMap = new HashMap();
public void setMappings(Properties mappings) {
localUrlMap.putAll(mappings);
super.setUrlMap(localUrlMap);
}
public void setUrlMap(Map urlMap) {
localUrlMap.putAll(urlMap);
super.setUrlMap(localUrlMap);
}
public String getMappingForBean(String beanName) {
Iterator iter = localUrlMap.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
if (localUrlMap.get(key).equals(beanName)) {
return key;
}
}
return null;
}
}
After finding which path was used to trigger the controller, put the path into request (see above) and use it later when you want to recognize what part of the request path is actually the parameter. E.g. if I need to load an image from url http://host/app/action/my/mapping/some/dir/image.jpg, and "my/mapping" is the mapping and "some/dir/image.jpg" is the parameter I would have something like this:
Code:
String mapping = (String) request.getAttribute(MyController.REQUEST_PATH_MAPPING);
mapping = mapping.substring(0, mapping.lastIndexOf('/'));
log.info("Mapping: " + mapping);
String pathInfo = request.getPathInfo();
String imageName = pathInfo.substring(pathInfo.lastIndexOf(mapping) + mapping.length());
log.info("Image name: " + imageName);
Again, this solution works for me but it has some drawbacks (what if there are more than one url mapping for a certain controller bean?).
If you have a better solution, or if you see some major problems with this one, please let me know.