Playframework2 like reverse routing in spring

2019-07-07 02:53发布

Can Anyone can advice me routing mechanism in spring.

I use 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 I like in spring that I define url before the controller method declaration.

Whaitting for Your sugestion. Thanks.

2条回答
小情绪 Triste *
2楼-- · 2019-07-07 03:27

Since version 4.1, 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 subject, but it's basically using auto-generated named routes for that.

I don't know if Thymeleaf supports this in its standard dialect, but you could quite easily extend it; if not, this is probably a feature that could be contributed to the Thymeleaf project.

Let's say you have a MyUserController like this:

@Controller
public class MyResourceController {

  @RequestMapping("/user/{name}")
  public String showUser(String name, Model model) {

    ...
    return "show";
  }
}

With such a dialect, you could then refer to an action like this:

<a th:uri="mvcUrl('MRC#ShowUser').buildAndExpand('bob')">Show user Bob</a>
<!-- will generate "/user/bob" -->
查看更多
仙女界的扛把子
3楼-- · 2019-07-07 03:29

This is the general flow in spring framework.

  1. Whenever user makes a request, it will first go to Spring's DispatcherServlet. The DispatcherServlet job is to send the request to spring mvc controller (custom controller)

  2. 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";
    }
}
  1. In servlet- context file , mention the directory/package path of your controller.

    Example: <context:component-scan base-package="nl.springexamples.mvc"/>

  2. In the above controller, it is returning string 'test' which is name of the view file(usually, it will 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>
  1. Define this logical mapping of string name to view file in servlet-context like this:

Example: How to define internalViewResolver is 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, that's pretty much about spring mvc and it's routing flow. I hope it helped you.

查看更多
登录 后发表回答