我正在建立一个Spring项目,我想进行用户注册功能。我想将用户数据提交给spring控制器并通过@ModelAttribute
接收数据,但是ModelAttribute
会正确地返回一些值,但某些不会。
这是我的用户bean。
public class User {
private Long id;
private String userName;
private String password;
private String email;
private Date regdate;
private String loginId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getRegdate() {
return regdate;
}
public void setRegdate(Date regdate) {
this.regdate = regdate;
}
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
}
这是我要提交的表格。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="register" method="post" modelAttribute="user">
<label for="loginid">ID:</label>
<input type="text" name="loginid">
<label for="password">password :</label>
<input type="password" name="password">
<label for="username">username :</label>
<input type="text" name="username">
<label for="email">e-mail :</label>
<input type="text" name="email">
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
所以我打印出模型属性以注销数据。
System.out.println("loginid :"+user.getLoginId()+"username :"+user.getUsername()+" password :"+ user.getPassword());
并返回loginid:null用户名:user密码:user
这是我接收数据的控制器。我试图通过ModelAttribute注释获取数据
@RequestMapping(value="/register", method=RequestMethod.POST)
public String createUser(@ModelAttribute User user, HttpServletRequest request, Model model) {
String ip = request.getRemoteAddr().toString();
System.out.println("loginid :"+user.getLoginId()+"id :"+user.getId()+"username :"+user.getUsername()+"password :"+ user.getPassword());
try {
userService.addUser(user,ip);
return "registrationsuccess";
}catch(Exception e) {
e.printStackTrace();
}
return "registrationfail";
}
答案 0 :(得分:0)
尝试看看here 也许您的Post方法中没有RequestMapping?
PS
尝试更改
<form action="register" method="post" modelAttribute="user">
<label for="loginid">ID:</label>
<input type="text" name="loginid">
<label for="password">password :</label>
<input type="password" name="password">
<label for="username">username :</label>
<input type="text" name="username">
<label for="email">e-mail :</label>
<input type="text" name="email">
<input type="submit" name="submit" value="submit"/>
</form>
到
<form:form method="post" action="/register" modelAttribute="user">
<form:label path="loginid">ID:</form:label>
<form:input path="loginid" />
<form:label path="password">password:</form:label>
<form:input path="password" />
...
<input type="submit" value="submit" />
</form:form>
在您的控制器中,尝试更改
public String createUser(@ModelAttribute User user, HttpServletRequest request, Model model)
到
public String createUser(@ModelAttribute("user") User user, HttpServletRequest request, Model model)