Using pathvariable in spring mvc 3

Chaitanya

I am learning Spring MVC from Spring in Action 3rd Edition and came across usage of path variables. I was not clear on how it works based on the example given in the book, please help me in understanding the concept here:

@RequestMapping(method=RequestMethod.POST)
public String addSpitterFromForm(@Valid Spitter spitter, BindingResult bindingResult) {
   if(bindingResult.hasErrors()){
       return"spitters/edit";
   }
   spitterService.saveSpitter(spitter);
   return "redirect:/spitters/" + spitter.getUsername();
}

As for the path that it’s redirecting to, it’ll take the form of /spitters/{username} where {username} represents the username of the Spitter that was just submitted. For example, if the user registered under the name habuma, then they’d be redirected to /spitters/habuma after the form submission.

In above statement, it says the request is redirected to /spitters/habuma where habuma is user name.

@RequestMapping(value="/{username}",method=RequestMethod.GET)
public String showSpitterProfile(@PathVariable String username, Model model){
    model.addAttribute(spitterService.getSpitter(username));
    return "spitters/view";
}

For example, if the request path is /username/habuma, then habuma will be passed in to showSpitterProfile() for the username.

and here it says the showSpitterProfile() method handles requests for /username/habuma which is contradicting with statement that is mentioned earlier.

It looks like the first statement itself is correct, but please tell me if the method showSpitterProfile handles both the URLs i.e /splitters/habuma and /username/habuma or /spitters/username/habuma?

Pavel Horal

There is no /username path component if the @RequestMapping on the class level (not shown in your question) is only @RequestMapping("/spitter"). There is probably a typo in the book. Correct sentence would be:

For example, if the request path is /spitter/habuma, then habuma will be passed in to showSpitterProfile() for the username.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring mvc @PathVariable

Multiple @PathVariable in Spring MVC

How to unit test a Spring MVC controller using @PathVariable?

Spring 3 @PathVariable validation

@PathVariable problems with encoding - Spring MVC

Spring MVC @PathVariable treated as @RequestParam

Send file path as @PathVariable in Spring MVC

Spring MVC : Unable to extract pathvariable value in a controller

Spring MVC @Pathvariable won't work

Spring MVC: @PathVariable doesn't work with cyrillic

Pass parameter to Spring controller using @PathVariable

How does Spring MVC @PathVariable receive parameters with multiple '/'?

Spring MVC trying to pass a variable by url. @PathVariable

Spring MVC 3.0: Is String the preferred type to be used for @PathVariable?

Spring-MVC PathVariable matching regular expression not starting with word

get pathvariable with sending and recieving json data Rest spring mvc

Junit Test cases for Spring MVC controller to check @PathVariable parameters in @RequestMapping

How to map several segments of URL to one PathVariable in spring-mvc?

NoClassDefFoundError while using AjaxUrlBasedViewResolver in Spring mvc 3

Spring boot report 404 error when using @PathVariable

Give name to PDF in Spring Controller without using @PathVariable

Spring boot using pathvariable to get hold of an id from url

Spring @PathVariable as a Year

PathVariable in Spring Controller

PathVariable in Spring Controller

Spring set default PathVariable

@PathVariable Validation in Spring 4

Spring @PathVariable annotation not working

Junit Test cases for Spring MVC 4 controller to check @PathVariable parameters in @RequestMapping