Emi*_*mil 0 java redirect thymeleaf
我想在按下“注册”按钮后从主页index.html(用户)重定向到他能够创建帐户的页面。然而,什么也没有发生。仅添加到地址:http://localhost:8080/DrinkShop注释在 /register 末尾
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.myapp</groupId>
<artifactId>DrinkShop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>DrinkShop</name>
<description>Spring Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
来自index.html的href:
<a th:href="@{/register}">Register</a>
Run Code Online (Sandbox Code Playgroud)
registerUser.html(在这种情况下可能是可选的)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Drink Shop</title>
</head>
<body>
<div th:replace="header :: header"></div>
<h1>New user</h1>
<form action="#" method="post" th:action="@{save}" th:object="${userNew}">
<p>Username: <input type="text" th:field="*{username}"> </p>
<p>Password: <input type="text" th:field="*{password}" /></p>
<p>email: <input type="text" th:field="*{email}" /></p>
<input type="submit" value="Save" />
</form>
<a th:href="@{/show}">Show all user</a>
<div th:fragment="footer"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
控制器
@Controller
public class HomeController {
@GetMapping
public String home(Model model) {
model.addAttribute("userNew", new User());
return "index";
}
@GetMapping("/register")
public String register()
{
return "registerUser";
}
}
Run Code Online (Sandbox Code Playgroud)
html 文件位于:DrinkShop\src\main\resources\templates
应用程序属性
spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.html
Run Code Online (Sandbox Code Playgroud)
小智 8
尝试这个:
@GetMapping("/register")
public String register()
{
return "redirect:/";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25617 次 |
| 最近记录: |