同一页面上的多个超链接

时间:2019-04-06 06:25:05

标签: html5 spring-mvc web spring-security frontend

我一直试图在同一页面上设置两个超链接。由于某些原因,无论通过单击超链接进行选择如何,它始终会打开与第一个选项相关联的html页面。我想念什么吗?

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

<head>
  <title>Spring Security Example</title>
</head>

<body>
  <h1>Welcome</h1>
  <p>Click <a th:href="@{/login}"> here </a>for login. </p>
  <p>Click <a th:href="@{/registration}"> here </a>to register as a new user.</p>
</body>

</html>

这里是login.html

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example </title>
    </head>
    <body>
        <div th:if="${param.error}">
            Invalid username and password.
        </div>
        <div th:if="${param.logout}">
            You have been logged out.
        </div>
        <form th:action="@{/login}" method="post">
            <div><label> User Name : <input type="text" align="center" name="username"/> </label></div>
            <div><label> Password: <input type="password" align="center" name="password"/> </label></div>
            <div><input type="submit" value="Sign In"/></div>
        </form>
    </body>
    </html>

以下是registration.html

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example </title>
    </head>
    <body>
        <form th:action="@{/registration}" method="post">
         <div><label> Preferred User Name : <input type="text" align="center" name="username"/> </label></div>
            <div><label> Password: <input type="password" align="center" name="pasisword"/> </label></div>
            <div><label> Reentered password: <input type="password" align="center" name="password"/> </label></div>
            <div><label> Address: <input type="text" align="center" name="address"/> </label></div>
            <div><label> Upload Image : <input type="image" align="center" name="photos"/> </label></div>
            <div><input type="submit" value="new user"/></div>
        </form>
    </body>
</html>

MvcConfig.java如下。

    public class MvcConfig implements WebMvcConfigurer {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/registration").setViewName("registration");
    }

}

1 个答案:

答案 0 :(得分:0)

这可能有帮助。

在href中拆分字符串

@{'/' + ${login}}

像这样:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

<head>
  <title>Spring Security Example</title>
</head>

<body>
  <h1>Welcome</h1>
  <p>Click <a th:href="@{'/' + ${login}}"> here </a>for login. </p>
  <p>Click <a th:href="@{'/' + ${registration}}"> here </a>to register as a new user.</p>
</body>

</html>