spring mvc @ PathVariable Bind URI Template Variable Values

  • 2021-12-09 08:41:53
  • OfStack

Directory @ PathVariable binds URI template variable values @ RequestParam (parameter bound to controller) and @ PathVariable (parameter bound to url template variable)

@ PathVariable Bind URI Template Variable Values

@ PathVariable is used to map the template variables in the request URL to the parameters of the function processing method.


@RequestMapping(value="/users/{userId}/topics/{topicId}")  
public String test(  
       @PathVariable(value="userId") int userId,   
       @PathVariable(value="topicId") int topicId) 

If the requested URL is "Controller URL/users/123/topics/456", the template variables {userId} and {topicId} in URL are automatically bound to the parameters with the same name annotated by @ PathVariable, that is, userId=123 and topicId=456 after entering the parameters.

The code is in PathVariableTypeController.

@ RequestParam (parameter bound to controller) and @ PathVariable (parameter bound to url template variable)

spring mvc: Exercises @ RequestParam and @ PathVariable

@RequestParam Annotations bind request parameters to your controller method parameters @PathVariable Annotation binds 1 method parameter to the value of 1 URI template variable

@ RequestParam: Annotation binds request parameters to your controller method parameters


@RequestMapping(value="/example/user")
public String UserInfo(Model model, @RequestParam(value="name", defaultValue="Guest") String name)
    

Example:


package springmvc;  
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
@Controller
public class RequestParamExampleController { 
    @RequestMapping(value="/example/user")
    public String UserInfo(Model model,
            @RequestParam(value="name", defaultValue="Guest") String name)
    {       
        model.addAttribute("name", name);
        if("admin".equals(name))
        {
            model.addAttribute("email", "admin@google.com");
        }else {
            model.addAttribute("email", "not set");
        }         
        return "example_user";         
    }
}
    

@ PathVariable: Annotation binds 1 method parameter to the value of 1 URI template variable


@RequestMapping(value="/example/info/{language}/{id}/{name}")
public String userInfo2(Model model,
            @PathVariable(value="language") String language,
            @PathVariable(value="id") Long id,
            @PathVariable(value="name")  String name)

Example:


package springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
 
@Controller
public class RequestParamExampleController { 
        @RequestMapping(value="/example/person/{name}/{age}")
    public String userPerson(Model model,
            @PathVariable(value="name") String name,
            @PathVariable(value="age") Long age)
    {
        model.addAttribute("name", name);
        model.addAttribute("age", age);
        String desc = "";
        if(age > 20)
        {
            desc = "oldman";
        }else {
            desc = "yongman";
        }
        model.addAttribute("desc", desc);
        return "example_person";
    } 
}

Related articles: