1

i would like to redirect a request something like this

localhost:8080 /firstSpringProject/{uniqueusername}

to a specific controller named 'profile':

@RequestMapping(value="/profile")
public String profiles(Model model){

    based on the uniqueusername i would like to render a profile page 

    return "profile";
}

I am using spring mvc; how can I resolve this situation is there any other way to do this?

2
  • 1
    It's not clear what the source pattern is. Do you just mean something like @RequestMapping("/{uniqueUserName}")? Commented Apr 27, 2014 at 7:13
  • the sourece request may like this: firstSpringProject/username1 or firstSpringProject/username2 or so and i would like to redirec that into @RequestMapping(value="/profile") and then based on the url i would like to render user profile (here it may be username1 or 2) Commented Apr 27, 2014 at 7:24

2 Answers 2

2

Spring documentation says on redirect view:

Note that URI template variables from the present request are automatically made available when expanding a redirect URL and do not need to be added explicitly neither through Model nor RedirectAttributes. For example:

@RequestMapping(value = "/files/{path}", method = RequestMethod.POST)
public String upload(...) {
    // ...
    return "redirect:files/{path}";
}

Keep in mind that version lower than 3.1.4 are affected by a memory leak due to caching redirect views.

If you are using Spring 3.1.3 or lower and you are doing this

return "redirect : profile?username="+username;

you will see OutOfMemoryError sometime.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you may use spring path variable here. You have to create a controller method that will take username as per your URL requirement and will redirect to profile method with username parameter.

    @RequestMapping(value="/{username}")
    public String getUserName(Model model,@PathVariable("username") String username){

        //process username here and then redirect to ur profile method

        return "redirect : profile?username="+username;
    }

    @RequestMapping(value="/profile")
    public String profiles(Model model,String username){

        //have a username and render a profile page 

        return "profile";
    }

Thank you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.