Playframework2 as reverse routing in spring
Can anyone please advise me on the routing mechanism in spring.
I am using thymeleaf for my view and I would like to use class names and method names for my url in views - just like in playframework.
But what I like about spring is that I define url before declaring the controller method.
Chorus for your judgment. Thank.
source to share
Since 4.1, the Spring Framework provides a way to generate routes to resources from templates (i.e., reverse routing in views).
You can check the reference documentation on the topic , but mostly use autogenerated named routes for this.
I don't know if Thymeleaf supports this in his standard dialect, but you could easily extend it ; if not, this is probably a feature that could be brought into the Thymeleaf project.
Let's say you have a MyUserController:
@Controller
public class MyResourceController {
@RequestMapping("/user/{name}")
public String showUser(String name, Model model) {
...
return "show";
}
}
With such a dialect, you can refer to an action like this:
<a th:uri="mvcUrl('MRC#ShowUser').buildAndExpand('bob')">Show user Bob</a>
<!-- will generate "/user/bob" -->
source to share
This is a common thread within the spring framework.
-
Whenever the user makes a request, he goes to the spring DispatcherServlet first. The DispatcherServlet's job is to send a request to the spring mvc controller (custom controller)
-
You can define your custom controller like this:
Controller: (code snippet)
package nl.springexamples.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(){
return "test";
}
}
-
In the servlet context file, specify the directory / package path of your controller.
Example:
<context:component-scan base-package="nl.springexamples.mvc"/>
-
In the above controller, it returns the string 'test' which is the name of the view file (it will usually be jsp).
JSP file: test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1> Welcome to Spring!!!</h1>
</body>
</html>
- Define this logical mapping of the string name to view the file in the servlet context as follows:
Example. How to define innerViewResolver as shown below.
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value ="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
I think it's pretty much about spring mvc and routing flow. Hope this helped you.
source to share