Spring MVC provides ways to build and encode URI’s from Controller, methods and views. Using UriComponentsBuilder and UriComponents we can create uri.
1 2 3 4 |
UriComponents uriComponents = UriComponentsBuilder.fromUriString( "http://localhost:8080/employee/{empId}/dept/{deptId}").build(); URI uri = uriComponents.expand("1001", "2500").encode().toUri(); |
The UriComponents can also be build using individual components like hostName, port, path.
1 2 |
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("localhost").port(8080) .path("/employee/{empId}/dept/{deptId}").build().expand("1001", "2500").encode(); |
Spring MVC Building URI from Controller methods
As seen from the above code snippets, we can also build uri from the methods of controllers.
If you have a controller
1 2 3 4 5 6 7 8 9 10 |
@Controller @RequestMapping(value = "/employee") public class EmployeeController { @RequestMapping(value = "/details/{empId}") public ModelAndView employeeDetails(@PathVariable int empId) { //Your implementation } } |
You can use the below code to generate URI
1 2 3 4 |
UriComponents uriComponents = MvcUriComponentsBuilder .fromMethodName(EmployeeController.class, "employeeDetails", 375).buildAndExpand(); URI uri = uriComponents.encode().toUri(); |
Here we provided the actual values.If the method had more arguments you can supply null for arguments not needed for the URL. In general only @PathVariable and @RequestParam arguments are relevant for constructing the URL.
This URI building technique is useful in mock testing your app.
Spring MVC Building URI from Views
We can also build URI to our annotated Controller from the view pages.
Every @RequestMapping is assigned a default name based on the capital letters of the class and the full method name. For example, the method getFoo in class FooController is assigned the name “FC#getFoo”. This strategy can be replaced or customized by creating an instance of HandlerMethodMappingNamingStrategy and plugging it into your RequestMappingHandlerMapping. The default strategy implementation also looks at the name attribute on @RequestMapping and uses that if present. That means if the default mapping name assigned conflicts with another (e.g. overloaded methods) you can assign a name explicitly on the @RequestMapping. Source
Suppose we have the same controller as we did for our earlier example
1 2 3 4 5 6 7 8 9 |
@Controller @RequestMapping(value = "/employee") public class EmployeeController { @RequestMapping(value = "/details/{empId}") public ModelAndView employeeDetails(@PathVariable int empId) { // Your implementation } } |
You can create a link in your JSP as below.
1 2 3 |
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %> ... <a href="${s:mvcUrl('EC#employeeDetails').arg(0,375).buildAndExpand()}">Employee Details</a> |
Note : the arg has 2 params. 1st the position of the pathvariable or RequestParam and it starts from 0. 2nd is the actual value of the pathvariable or RequestParam.