3

I have a class as follows:

@Controller
@RequestMapping("/path1")
public class MyController
{
    @RequestMapping(value = "/path2", method = RequestMethod.GET)
    public ModelAndView func(ModelAndView mav)
    {
        String path = getRequestMappingValue(); // Here I expect a function which returns "/path1/path2"
        mav.setViewName(path  + ".jsp");
        return mav;
    }
}

What I require is the function getRequestMappingValue(), which returns the value of annotation @RequestMapping (in this case, it is "/path1/path2")

Chip Zhang
  • 641
  • 2
  • 11
  • 26
  • one way would be reflection – jmj Jan 14 '15 at 01:18
  • @JigarJoshi could you please explain more detailedly? I am a newbie to Java and SpringMVC. – Chip Zhang Jan 14 '15 at 01:20
  • 2
    There is no good reason to do this. If you have a JSP in `something/path1/` called `path2.jsp`, just write that in `setViewName`. Don't make it depend on some other metadata. – Sotirios Delimanolis Jan 14 '15 at 01:23
  • I agree with Sotirios, but if you really wanted to do this, an easier way would be to extract a string constant and have both the @RequestMapping path and view name use that constant. –  Jan 14 '15 at 01:27
  • @JigarJoshi The question is a duplicate of the other question, but that question doesn't have any answers that appear to specifically answer this one. OP, it would be helpful if you explained your use case more clearly; what you said you want (`/path1/path2`) isn't the annotation value. – chrylis -cautiouslyoptimistic- Jan 14 '15 at 01:36
  • A good use case might be building a RESTful webservice, where the json output is required to include its resource location for later client side usage. – John Deverall Mar 20 '15 at 10:15
  • This solution here works. [http://stackoverflow.com/questions/16394348/spring-mvc-get-requestmapping-value-in-the-method][1] – John Deverall Mar 20 '15 at 10:22

2 Answers2

1

Doesn't this solution here achieve pretty much what you're looking for?

private final static String MAPPING = "/hello";

@RequestMapping(value = MAPPING)
@ResponseBody
public void helloMethod(AtmosphereResource atmosphereResource) {
   // MAPPING accessible as it's stored in instance variable
}
Community
  • 1
  • 1
John Deverall
  • 5,954
  • 3
  • 27
  • 37
0

The whole point of MVC is mapping requests like /user/brian to Controller methods that perform actions (like showUser(Model model)) and return views. Trying to guess the view name based on some value in the request seems like a code smell to me.

Maybe you could explain a bit more your use case?

I wouldn't personally rely on this (I think this is not an advertised feature of the framework), but you can get the path within the current handler mapping like this:

@Controller
@RequestMapping("/path1")
public class MyController {

    @RequestMapping(value = "/path2")
    public String myAction(Model model, HttpServletRequest request) {

        String path = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        // do something
        return "viewName";
    }
}

Please also refer to the javadoc:

 Note: This attribute is not required to be supported by all HandlerMapping implementations. 
 URL-based HandlerMappings will typically support it, but handlers should not necessarily expect 
 this request attribute to be present in all scenarios.
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176