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

From Java

Spring mvc @PathVariable

From Dev

Spring 3 @PathVariable validation

From Dev

@PathVariable problems with encoding - Spring MVC

From Dev

@PathVariable problems with encoding - Spring MVC

From Dev

Spring MVC @PathVariable treated as @RequestParam

From Dev

How to validate Spring MVC @PathVariable values?

From Dev

Spring MVC @PathVariable with url address throwing error

From Dev

Send file path as @PathVariable in Spring MVC

From Dev

Spring MVC : Unable to extract pathvariable value in a controller

From Dev

Spring MVC @Pathvariable won't work

From Dev

Spring MVC: @PathVariable doesn't work with cyrillic

From Dev

Pass parameter to Spring controller using @PathVariable

From Dev

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

From Dev

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

From Dev

Spring MVC- Storing and Retrieving @PathVariable Map<String, String>

From Dev

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

From Dev

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

From Dev

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

From Dev

get pathvariable with sending and recieving json data Rest spring mvc

From Dev

NoClassDefFoundError while using AjaxUrlBasedViewResolver in Spring mvc 3

From Dev

Give name to PDF in Spring Controller without using @PathVariable

From Dev

Spring optional @PathVariable?

From Dev

@PathVariable Validation in Spring 4

From Dev

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

From Dev

How to upload file using spring mvc3?

From Dev

Create basic form using spring 3 mvc framework

From Dev

Thymeleaf with Spring MVC 3

From Dev

Spring MVC 3 localeChangeInterceptor

From Dev

Using && in Spring MVC @PreAuthorize

Related Related

  1. 1

    Spring mvc @PathVariable

  2. 2

    Spring 3 @PathVariable validation

  3. 3

    @PathVariable problems with encoding - Spring MVC

  4. 4

    @PathVariable problems with encoding - Spring MVC

  5. 5

    Spring MVC @PathVariable treated as @RequestParam

  6. 6

    How to validate Spring MVC @PathVariable values?

  7. 7

    Spring MVC @PathVariable with url address throwing error

  8. 8

    Send file path as @PathVariable in Spring MVC

  9. 9

    Spring MVC : Unable to extract pathvariable value in a controller

  10. 10

    Spring MVC @Pathvariable won't work

  11. 11

    Spring MVC: @PathVariable doesn't work with cyrillic

  12. 12

    Pass parameter to Spring controller using @PathVariable

  13. 13

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

  14. 14

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

  15. 15

    Spring MVC- Storing and Retrieving @PathVariable Map<String, String>

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

    get pathvariable with sending and recieving json data Rest spring mvc

  20. 20

    NoClassDefFoundError while using AjaxUrlBasedViewResolver in Spring mvc 3

  21. 21

    Give name to PDF in Spring Controller without using @PathVariable

  22. 22

    Spring optional @PathVariable?

  23. 23

    @PathVariable Validation in Spring 4

  24. 24

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

  25. 25

    How to upload file using spring mvc3?

  26. 26

    Create basic form using spring 3 mvc framework

  27. 27

    Thymeleaf with Spring MVC 3

  28. 28

    Spring MVC 3 localeChangeInterceptor

  29. 29

    Using && in Spring MVC @PreAuthorize

HotTag

Archive