10.2. Controllers with Parameters¶
Now that you know the basics of a controller method, we can start to add some more variables into the mix. Some controller methods can take in parameters in the form of query strings or sections of the URL path. Passing this URL data to the controller is one step closer to more flexible web applications.
A route is the mechanism by which a request path gets assigned to a controller within our application. We’ll explore routes and how data is transferred from a webpage with a given route to a specific controller.
10.2.1. Query Strings are URL Data¶
A brief refresher: query strings are additional bits of information tacked onto the ends of urls. They contain data in key-value pairs.
www.galaxyglossary.net?aKey=aValue&anotherKey=anotherValue&thirdKey=thirdValue
Note
Do HTTP requests and responses feel unfamiliar? Do you remember what a query string is? If you’re feeling rusty on these topics, it’s a good idea to brush up now, as routing requires a foundational understanding of HTTP data transfer.
Here’s our introduction to HTTP for reviewing the concepts.
10.2.2. Controllers and Query Parameters - Video¶
Note
The starter code for this video is found in the static-responses branch
of the hello-spring-demo
repo. The final code presented in this video is found on the query-parameters branch.
Be sure to code along to the videos on your own hello-spring
project.
10.2.3. @RequestParam
¶
We can pass @RequestParam
as an argument of a controller method. This annotation as an
argument lets the handler method know about a given query string based on its own argument.
// Responds to get requests at /hello?coder=LaunchCoder
@GetMapping("hello")
@ResponseBody
public String hello(@RequestParam String coder) {
return "Hello, " + coder + "!";
}
The controller method looks for the query string in the URL that matches its parameter, coder
, and puts
the paired value of that coder
key into the response text.
10.2.4. Controllers and Path Parameters - Video¶
Note
The starter code for this video is found in the query-parameters branch
of the hello-spring-demo
repo. The final code presented in this video is found on the path-variables branch.
Please code along to the videos on your own hello-spring
project.
10.2.5. @PathVariable
¶
Another way to handle data with a controller is by accessing the data via a segment of the
URL. This is done with @PathVariable
. @PathVariable
takes an argument that, if matching
a portion of the URL, will deliver this data into the handler.
// Responds to get requests at /hello/LaunchCode
@GetMapping("hello/{name}")
@ResponseBody
public String helloAgain(@PathVariable String name) {
return "Hello, " + name + "!";
}
}
Above, name
is a placeholder, indicating where in the URL segment to look for the @PathVariable
. From
the comment, we know that that the actual value is LaunchCode
, but this can easily be changed. If we changed
the value of this URL segment to /hello/Ada
, then this controller would respond with Hello, Ada
when a
GET
request is made.
Note
Also know that you can redirect a user by removing the @ResponseBody
annotation from the controller and returning
"redirect:/DESIREDPATH"
.
10.2.6. Check Your Understanding¶
Question
Your application is served at myfavoriteplanets.net
. What is the path
that this controller maps to?
@GetMapping("venus")
@ResponseBody
public String venusSurface(@RequestParam String terrestrial) {
if (terrestrial == true) {
return "Venus is rocky."
} else {
return "Venus is gaseous."
}
myfavoriteplanets.net/venus?terrestrial=true
net.myfavoriteplanets/venus?terrestrial=true
myfavoriteplanets/venus?terrestrial=true
myfavoriteplanets/venus/terrestrial
Question
Your application is served at myfavoriteplanets.net
. What URL do you
need to hit so that the response is:
Akatsuki currently orbits Venus.
?
@GetMapping("venus/{orbiter}")
@ResponseBody
public String venusOrbiter(@PathVariable String orbiter) {
return orbiter + " currently orbits Venus."
}
myfavoriteplanets.net/venus/{Akatsuki}
myfavoriteplanets.net/venus/orbiter=Akatsuki
myfavoriteplanets.net/venus/Akatsuki
myfavoriteplanets.net/venus/name=Akatsuki