如何修复 ModelAttributes 为 return 空值?

How to fix ModelAttributes are return null value?

我正在设置一个 spring 项目,我想实现 用户注册 功能。我想将用户数据提交给 spring 控制器并通过 @ModelAttribute 接收数据,但是 ModelAttribute returns 有些值正确但有些不正确。

这是我的用户 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 username :user password :user

这是我接收数据的控制器。我试图通过 ModelAttribute Annotation

获取数据
@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";
    }

试着看看here 也许您的 Post 方法中没有 RequestMapping?

P.S

尝试改变

<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)