简体   繁体   中英

Redirect to another page using Spring Thymeleaf

I would like to redirect from the home page index.html (user) after pressing the "register" button to the page where he will be able to create an account. However, nothing happens. Adds only to the address: http://localhost:8080/DrinkShop note at the end /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>

href from index.html:

 <a th:href="@{/register}">Register</a>

registerUser.html ( probably optional in this case )

<!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>

controller

@Controller
public class HomeController {

    @GetMapping
    public String home(Model model) {
        model.addAttribute("userNew", new User());
        return "index";
    }

    @GetMapping("/register")
    public String register()
    {
        return "registerUser";
    }

}

html files is in : DrinkShop\\src\\main\\resources\\templates

application.properties

spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.html

Try this:

    @GetMapping("/register")
    public String register()
    {
        return "redirect:/";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM