[JAVA] [Spring MVC] How to pass path variables

How to receive path variables in Spring, how to pass to redirect destination Memo

How to receive a path variable

// (1)
@GetMapping("{name}")
public String detail(@PathVariable String name) {
    return "detail";
}

// (2)
@GetMapping("idol/{name}")
public String idolDetail(@PathVariable("name") String idolName) {
    return "idol/detail";
}

// (3)
@GetMapping("unit/{unitId}/idol/{idolId}")
public String idolDetail(@PathVariable("unitId") Integer unitId, @PathVariable("idolId") Integer idolId) {
    return "idol/detail";
}

If you want to use the value contained in the path in the process of the method, use the path variable ({variable name}) format in the part where the value you want to receive is entered in the Mapping annotation, and set the corresponding argument to @PathVariable. Specify .

(1) If the path variable name and the variable name actually used are the same, the value attribute of @PathVariable can be omitted. (2) If the variable name is different, specify the path variable name in the value attribute of @PathVariable. (3) Multiple is possible.

How to pass a path variable to the redirect destination

// (1)
@PostMapping("idol/{name}/update")
public String update(@PathVariable String name) {
    return "redirect:/idol/{name}";
}

// (2)
@PostMapping("redirect")
public String redirect(RedirectAttributes redirectAttributes) {
    redirectAttributes.addAttribute("name", "Jinka Osaki");
    return "redirect:/idol/{name}";
}

(1) A variable once received as a path variable can be passed to the redirect destination as it is. In the case of the above code, if you send a POST request to / idol / Kiriko Yuya / update, the URL of the redirect destination will be / idol / Kiriko Yuya. (2) If the variable added by using the ╩╗addAttributemethod of RedirectAttributes is included in the redirect destination path variable, it is expanded as it is as a path variable. In the above case, the redirect destination URL will be/ idol / Osaki Jinka`.

(2) may be a simple string concatenation if it is not necessary to enter a value separately.

reference

Official Reference Spring MVC controller arguments Thorough introduction to Spring Java application development with Spring Framework

Recommended Posts

[Spring MVC] How to pass path variables
[Rails 5] How to use gem gon ~ How to pass variables from Rails to JS ~
How to use Lombok in Spring
How to unit test Spring AOP
How to use Spring Data JDBC
[How to install Spring Data Jpa]
How to set Spring Boot + PostgreSQL
[Processing ├Ś Java] How to use variables
How to use ModelMapper (Spring boot)
How to name variables in Java
How to pass Oracle Java Silver
Pass environment variables to docker container
How to realize huge file upload with TERASOLUNA 5.x (= Spring MVC)
How to name variables 7 selections of discomfort
How Dispatcher servlet works in Spring MVC
Transition from Struts2 to Spring MVC (Controller)
How to pass the value to another screen
How to use environment variables in RubyOnRails
[Struts] How to pass values between jsps
How to write Spring AOP pointcut specifier
How to split Spring Boot message file
Major changes related to Spring Framework 5.0 Web MVC
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
Understand how to share Spring DB connection (DB transaction)
How to use built-in h2db with spring boot
How to make Spring Boot Docker Image smaller
How to use Spring Boot session attributes (@SessionAttributes)
How to deploy
How to add a classpath in Spring Boot
How to use scope and pass processing (Jakarta)
How to create an Excel form using a template file with Spring MVC
How to set environment variables in the properties file of Spring boot application
How to create member variables with JPA Model
How to define multiple orm.xml in Spring4, JPA2.1
[Spring Boot] How to refer to the property file
Spring Boot --How to set session timeout time
How to embed JavaScript variables in HTML with Thymeleaf
I tried to implement file upload with Spring MVC
How to set Dependency Injection (DI) for Spring Boot
How to apply HandlerInterceptor to http inbound-gateway of Spring Integration
How to write a unit test for Spring Boot 2
How to use Struts2 * Spring Framework (Spring plugin) June 2017 Version
[Java] How to omit spring constructor injection with Lombok
[Java] How to extract the file name from the path
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
How to use CommandLineRunner in Spring Batch of Spring Boot
How to test file upload screen in Spring + Selenium
How to specify the resource path in HTML import
How to boot by environment with Spring Boot of Maven
How to use In-Memory Job repository in Spring Batch
How to call AmazonSQSAsync
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select