0

I have two URLs to map: The root : http://www.website.com : When user request the root I want to show the home page. The root with parameter: http://www.website.com?ref=xxxx : When user requests this, I want to create a cookie and go to the root ("/").

Here is my controller:

@RequestMapping(value="/",method=RequestMethod.GET)
public String doGreatThings(
    @RequestParam(value="ref", required=false) String identifier,
    ServletRequest request, 
    ServletResponse response){

        if(identifier!=null){

        }
        return "/";
    }

This gives me an infinite loop. Is it possible to distinguish between both mappings in the controller?

http://www.mywebsite.com

vs.

http://www.mywebsite.com?ref=xxxxx
kryger
  • 12,906
  • 8
  • 44
  • 65
Pracede
  • 4,226
  • 16
  • 65
  • 110
  • possible duplicate of [create two method for same url pattern with different arguments](http://stackoverflow.com/questions/15853035/create-two-method-for-same-url-pattern-with-different-arguments) – kryger Jun 09 '15 at 14:33

2 Answers2

2

The answer linked in some of the previous comments is the way to go.

You should use the params attribute of the @RequestMapping to filter by param, but note that you can also negate this filter, so map to an url that doesn't contain a param. Following should be a mapping that works for you

the mapping without the parameter note the ! that marks the negation

@RequestMapping(value="/",method=RequestMethod.GET, params="!ref")
public ModelAndView doGreatThings(
    @RequestParam(value="ref", required=false) String identifier,
    ServletRequest request, 
    ServletResponse response){
    // show home page
}

the mapping with the parameter

@RequestMapping(value="/",method=RequestMethod.GET, params="ref")
public ModelAndView doGreatThings(
    @RequestParam(value="ref") String identifier,
    ServletRequest request, 
    ServletResponse response){
        // create the cookie
        // redirect to home page
}
Community
  • 1
  • 1
Master Slave
  • 27,771
  • 4
  • 57
  • 55
0

I have edited my answer as per @kryger comment, You can use params attribute to filter by parameter.

Please check this link for the original answer.

Community
  • 1
  • 1
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53