Spring MCV:无法处理数据输入
我的目标是通过处理用户的登录名和密码来授权用户。我试图复制,但面临着问题 我有一个实体用户类:Spring MCV:无法处理数据输入,spring,jsp,spring-mvc,thymeleaf,request-mapping,Spring,Jsp,Spring Mvc,Thymeleaf,Request Mapping,我的目标是通过处理用户的登录名和密码来授权用户。我试图复制,但面临着问题 我有一个实体用户类: @DynamicUpdate public class EntityUser { String login; String password; public EntityUser() {} public EntityUser ( String login, String password )
@DynamicUpdate
public class EntityUser
{
String login;
String password;
public EntityUser() {}
public EntityUser
(
String login,
String password
)
{
this.login = login;
this.password = password;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@RequestMapping(value = "/loginCheck", method = RequestMethod.GET)
public String userForm(Model model)
{
EntityUser user = new EntityUser();
user.setLogin("login");
user.setPassword("password");
model.addAttribute("user", user);
System.out.println(user.getLogin());
System.out.println(user.getPassword());
return "/loginCheck";
}
@RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
public String processUser(@ModelAttribute(value="user") EntityUser user)
{
System.out.println(user.getLogin());
System.out.println(user.getPassword());
loginInfo = "Jakarta";
return "redirect:/controllers";
}
这是我的.jsp文件片段:
<form action="#" th:action="@{/loginCheck}" th:object="${user}" method="post">
<table border="1" width="30%" cellpadding="3">
<thead>
<tr>
<th colspan="2">Login Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>User Name</td>
<td><input type="text" th:field="*{login}"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" th:field="*{password}"/></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</tbody>
</table>
</form>
输入值并按下“提交”按钮后,没有调用GET或POST方法(控制台中没有任何打印),页面将移动到/#
另一方面,当我替换
formaction=“#”
到
表单操作=“/hellostringmvc/loginCheck”
,
调用了POST方法,但打印的两个字符串均为“null”
那么,有什么问题吗?有人知道吗
编辑:
这是我的和文件。您的pom.xml没有spring boot starter thymeleaf依赖项,因此thymeleaf标记不会在html/jsp模板中处理。在pom.xml中添加thymeleaf starter依赖项并重新生成:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
org.springframework.boot
您的pom.xml没有spring boot starter thymeleaf依赖项,因此thymeleaf标记不会在html/jsp模板中处理。在pom.xml中添加thymeleaf starter依赖项并重新生成:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
org.springframework.boot
并根据您自己的需要对其进行定制。看起来thymeleaf并不是用来将输入值传递到模型中的用户对象中的。您正在尝试混合使用视图类型。JSP和Thymeleaf。你可能想坚持其中一个。我建议坚持使用thymeleaf,并以这种方式解决问题。可以包含pom.xml和MVC config类吗?pom.xml和web.aml是addedIt,看起来不像thymeleaf用于将输入值传递到模型中的用户对象中。您正在尝试混合使用视图类型。JSP和Thymeleaf。你可能想坚持其中一个。我建议坚持使用thymeleaf,并以这种方式解决问题。是否可以包括pom.xml和MVC配置类?添加了pom.xml和web.aml