我开始学习Spring框架,在Spring之前我使用了Laravel。在Laravel中,我们有各种帮助生成网址,例如:
<a href="{{ action('MyController@someAction') }}">Some url</a>
Thymeleaf中是否有类似内容,基本上我想生成一个url,它会指向控制器中的某些动作,所以如果我更改控制器的映射,则所有锚标记的url都会改变。
答案 0 :(得分:0)
Thymeleaf和Spring Boot有很好的关系。 从Spring-initializr创建Spring Boot应用程序 在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";
}
}
在welcome.html中添加以下内容 的src /主/资源/模板/ welcome.html
添加HTML定义:
<html xmlns:th="http://www.thymeleaf.org">
加入Body。
<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>
And Thats All
答案 1 :(得分:0)
存在一个用于Spring Boot框架的库。您需要在项目中添加以动态生成链接。该库的gradle依赖性如下所示。
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>
之后,您可以如下所示动态生成链接。
WebMvcLinkBuilder.linkTo(MyController.class).slash("someAction").withSelfRel().getHref();