Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring ThymileAF:为控制器中的操作生成URL_Spring_Spring Boot_Thymeleaf - Fatal编程技术网

Spring ThymileAF:为控制器中的操作生成URL

Spring ThymileAF:为控制器中的操作生成URL,spring,spring-boot,thymeleaf,Spring,Spring Boot,Thymeleaf,我开始学习Spring框架,在Spring之前我使用了Laravel。在Laravel中,我们有各种用于生成URL的助手,例如: <a href="{{ action('MyController@someAction') }}">Some url</a> Thymeleaf中是否有类似的内容,基本上我想生成一个url,它将指向控制器中的某些操作,因此如果我更改控制器的映射,所有锚定标记的url都将更改。Thymeleaf和Spring Boot有很好的关系。 从创建

我开始学习Spring框架,在Spring之前我使用了Laravel。在Laravel中,我们有各种用于生成URL的助手,例如:

<a href="{{ action('MyController@someAction') }}">Some url</a>


Thymeleaf中是否有类似的内容,基本上我想生成一个url,它将指向控制器中的某些操作,因此如果我更改控制器的映射,所有锚定标记的url都将更改。

Thymeleaf和Spring Boot有很好的关系。 从创建Spring启动应用程序 在Spring Boot pom.xml中添加以下依赖项

spring-boot-starter-thymeleaf
WelcomeController.java

@Controller
public class WelcomeController {

    // inject via application.properties
    @Value("${welcome.message:test}")
    private String message = "Hello World";

    @RequestMapping("/")
    public String welcome(Map<String, Object> model) {
        model.put("message", this.message);
        return "welcome";
    }

}
@控制器
公共类WelcomeController{
//通过application.properties注入
@值(${welcome.message:test}”)
私有字符串message=“Hello World”;
@请求映射(“/”)
公共字符串欢迎(地图模型){
model.put(“message”,this.message);
返回“欢迎”;
}
}
在welcome.html中添加以下内容 src/main/resources/templates/welcome.html

添加HTML定义:

<html xmlns:th="http://www.thymeleaf.org">

加入身体

<span th:text="'Message: ' + ${message}"></span>

表视图

<tbody>
    <tr th:each="student: ${students}">
      <td> <a th:href="${student.id}"><span  th:text="${student.name}"></span> </a></td>

    </tr>
  </tbody>


这就是所有的

spring boot framework提供了一个库。为了动态生成链接,您需要将其添加到项目中。该库的渐变依赖项如下所示

compile 'org.springframework.boot:spring-boot-starter-hateoas:2.1.4.RELEASE'
我假设您的构建系统是gradle,但如果您使用的是maven,请使用以下语法

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hateoas</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>

从thymeleaf文档来看,模板似乎基于
@RequestMapping
路径映射URL。我认为没有绑定控制器方法本身的功能。这与我的问题有什么关系。我想知道如何动态创建指向的urlMyController@someAction使用胸腺素
WebMvcLinkBuilder.linkTo(MyController.class).slash("someAction").withSelfRel().getHref();