0

I would like to set for each user's own profile link.
Like this:

   @RequestMapping(value = "/{userlogin}", method = RequestMethod.GET)
    public String page(@PathVariable("userlogin") String userlogin, ModelMap model) {
        System.out.println(userlogin);
        return "user";
    }

But static pages get this expression too..
Like this:

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
    System.out.println("hello mapping");
    return "hello";
}

That is when I request GET request "hello" that calls both controllers.
I would like to do that, user controller calls only if other methods not called.

Console, when I calls localhost:8080/123:

123

Console, when I calls localhost:8080/hello:

hello 
hello mapping

or

hello mapping
hello 

I want to get only

hello mapping

when calls localhost:8080/hello

Who knows how it can be implemented?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Oleksandr
  • 3,574
  • 8
  • 41
  • 78
  • Just call one method with path variable with it i.e. requestMapping(value="/hello/{userLogin}") – Harshal Patil Oct 27 '14 at 18:17
  • Thank you for response. But it not what i want.. I changed the post to a more intuitive – Oleksandr Oct 27 '14 at 18:42
  • I advise to look in the direction of filters, which will redirect to other path for localhost:8080/hello and how result to other controller. For example, you may look this link: http://stackoverflow.com/questions/3125296/can-i-exclude-some-concrete-urls-from-url-pattern-inside-filter-mapping – Alexey Semenyuk Oct 27 '14 at 19:14

1 Answers1

1

Spring MVC can use URI Template Patterns with Regular Expressions. Provided :

  • userlogin only contain digits
  • others URL immediately under root contains at least one non digit character

you can use that in your @RequestMapping :

@RequestMapping(value = "/{userlogin:\\d+}", method = RequestMethod.GET)
public String page(@PathVariable("userlogin") String userlogin, ModelMap model) {
    //...
}

If the separation between userlogin and other URL is different from what I imagined, it can be easy to adapt.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252