我是网络应用程序开发的新手,我想为我的网络应用程序创建一个登录页面(使用Spring boot + Thymeleaf),但是当我发出{{1}请求时,我无法显示form.html
}。
这是我的/form
课程:
Entity
我的 package com.example.springboot.model;
public class ApplicationUser {
private String username;
private String password;
public ApplicationUser(String username, String password) {
this.username = username;
this.password = password;
}
public ApplicationUser() { }
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
看起来像这样:
form.html
而 <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Form Submission</title>
</head>
<body>
<h1>User form</h1>
<form action="#" th:action="@{/form}" th:object="${applicationuser}" method="post">
<p>Username: <input type="text" th:field="*{username}"/></p>
<p>Password: <input type="text" th:field="*{password}"/></p>
<p><input type="submit" value="Send"/></p>
</form>
</body>
</html>
是
result.html
但<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Result</title>
</head>
<body>
<h1>Result</h1>
<p th:text="'username: ' + ${applicationuser.username}"/>
<p th:text="'password: ' + ${applicationuser.password}"/>
<a href="/form">Submit another</a>
</body>
</html>
中的applicationuser
以红色突出显示(&#34;无法解析&#34; applicationuser&#34;)。这可能是个问题吗?如何解决?
我的th:object="${applicationuser}"
课程是
Controller
我只是想获取输入表单并在控制台中显示它们,但没办法...... 当我在浏览器中提出请求时:http://localhost:8080/form我获得了一个白色页面,其中包含跟随字符串&#34; form&#34;,它不会显示文本字段和按钮。 我在pom.xml中有百里香的依赖:
@RestController
public class Test {
@RequestMapping(value = "/form",method = RequestMethod.GET)
public String userForm(Model model){
model.addAttribute("applicationuser",new ApplicationUser());
return "form";
}
@RequestMapping(value = "/form",method = RequestMethod.POST)
public String userSubmit(@ModelAttribute ApplicationUser user,Model model){
model.addAttribute("applicationuser",user);
String info = String.format("User Submission: username = %s, password = %s", user.getUsername(),user.getPassword());
System.out.println(info);
return "result";
}
}
有谁知道我的代码有什么问题?
答案 0 :(得分:0)
似乎我遇到了你的问题。更改此部分
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
到
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:sf="http://www.w3.org/1999/xhtml" lang="en">
并改变你的
<p>Username: <input type="text" th:field="*{username}"/></p>
<p>Password: <input type="text" th:field="*{password}"/></p>
到
<p>Username: <input type="text" id="username" name="username"/></p>
<p>Password: <input type="text" id="password" name="password" /></p>
还有一件事,将您@RestController
更改为@Controller
如果有效,请告诉我。