thymeleaf 超链接怎么用
时间: 2023-09-08 10:12:54 浏览: 132
Thymeleaf中使用超链接可以通过`<a>`标签来实现。以下是一个简单的示例:
```html
<a th:href="@{/path/to/destination}">Link Text</a>
```
在这个示例中,`th:href`属性用于指定链接的目标路径。`@{/path/to/destination}`是Thymeleaf的URL表达式,用于生成相对于当前上下文的链接路径。你需要将`/path/to/destination`替换为你实际的目标路径。
你还可以在`<a>`标签内部添加文本,用作链接的显示文本。在示例中,我们使用"Link Text"作为显示文本,你可以根据需要进行替换。
希望这个简单的示例能帮助到你!如果有任何进一步的问题,请随时提问。
相关问题
thymeleaf超链接语法
Thymeleaf中超链接的语法如下:
```html
<a th:href="@{/url/path}">Link Text</a>
```
其中,`@{/url/path}`表示相对于应用程序根目录的URL路径。
如果要在新窗口中打开链接,可以添加`target="_blank"`属性:
```html
<a th:href="@{/url/path}" target="_blank">Link Text</a>
```
创建一个success.html 页面,在首页通过 thymeleaf 超链接 <a th:href/>和浏览器超链接<a/>2种不同方式分别实现跳转到success.html页面。
好的,下面是实现步骤:
1. 在Web应用程序的“src/main/resources/templates”目录下创建一个名为“success.html”的Thymeleaf模板文件。在该文件中添加以下内容:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Success Page</title>
</head>
<body>
<h2>Success!</h2>
</body>
</html>
```
2. 在index.html文件中,添加两个超链接,一个使用Thymeleaf,一个使用常规的HTML链接。如下所示:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home Page</title>
</head>
<body>
<h2>Welcome to the Home Page</h2>
<a th:href="@{/success}">Go to Success Page (Thymeleaf)</a>
<br />
<a href="/success">Go to Success Page (HTML)</a>
</body>
</html>
```
3. 在Web应用程序的“src/main/resources/application.properties”文件中添加以下内容:
```
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
```
4. 在Web应用程序的“src/main/java”目录下创建一个名为“com.example.demo”的Java包。
5. 在“com.example.demo”包中创建一个名为“DemoApplication.java”的类,并添加以下内容:
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
6. 在Web应用程序的“src/main/java”目录下创建一个名为“com.example.demo.controller”的Java包。
7. 在“com.example.demo.controller”包中创建一个名为“HomeController.java”的控制器类,并添加以下内容:
```java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home() {
return "index";
}
@GetMapping("/success")
public String success() {
return "success";
}
}
```
现在你可以启动应用程序,并在浏览器中输入http://localhost:8080/来访问首页。点击超链接“Go to Success Page (Thymeleaf)”或“Go to Success Page (HTML)”将跳转到“success.html”页面。
希望这些步骤可以帮助到你实现要求的功能。