html - 同一页面上的多个超链接
问题描述
我一直在尝试在同一页面上设置两个超链接。由于某些原因,无论通过单击超链接做出何种选择,它总是会打开与第一个选项关联的 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");
}
}
解决方案
这可能会有所帮助。
拆分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>
推荐阅读
- javascript - 反应:未捕获(承诺)错误:请求失败,状态码为 400
- google-apps-script - 如何基于二级,甚至三级过滤器进行过滤?
- css - 反应在更新时更改 CSS 背景
- regex - 如何匹配行尾的空格作为前一句
- django - UUID 主键的格式错误的十六进制 UUID 错误字符串
- java - 如何处理 CDI 创建的 Runnable
- graphql - 使用 gatsby-source-prismic 无法返回 Prismic 切片
- c# - 使用 Kendo Grid 和 Odata 数据源对数字列进行过滤的最简单方法是什么?
- php - Laravel 不能通过外键连接两个表
- php - 如何更新我的查询以符合 sql_mode=only_full_group_by 启用?