2
@RequestMapping(value = {"/new", "/modifyNew"} ) public ModelAndView  public getCreate()

I defined my controller method this way to handle both /new and /modifyNew

Within my method is there a way to find out by which mapping the request came in ..? Was it new or modifyNew? Is this a good way of programming?

hamed
  • 7,939
  • 15
  • 60
  • 114
Jay Kumo
  • 671
  • 1
  • 5
  • 9

2 Answers2

0

You could read the request url as described here but I wouldn't recommend that. The need of accessing actual url is sufficient for splitting to two separate controllers for each endpoint and extracting the common logic to method.

Is this a good way of programming?

In my opinion it's not because you violate the "Single responsibility principle" (wiki). Which means that your function is doing more than one thing at once as it provides implementation for two endpoints separately. It would be ok if the behavior provided by your controller was equal for both endpoints. If you are interested in code quality I recommend you a book "Clean Code" by Robert C. Martin. Have fun !

Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33
0

You can use HttpServletRequest methods for getting requested url:

@RequestMapping(value = {"/new", "/modifyNew"} ) public ModelAndView  public getCreate(HttpServletRequest request){
    String url = request.getRequestURI() //Gets the requested URI.
}

You can get requested url and check it has /new or /modifyNew.

Also, you can check HttpServletRequest documentation above, and use any other method if you need.

hamed
  • 7,939
  • 15
  • 60
  • 114