springmodelattribute

How to fix ModelAttributes are return null value?


I'm setting up a spring project, I want to make user registration functionality. I want to submit user data to spring controller and receive data by a @ModelAttribute, But ModelAttribute returns some values properly but some doesn't.

this is my user 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;
    }

}

this is form I want to submit.

<%@ 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>

so I printed out model attribute to log data out.

System.out.println("loginid :"+user.getLoginId()+"username :"+user.getUsername()+" password :"+ user.getPassword());

and it returned loginid :null username :user password :user

this is controller that I receive data. I tried to get a data by a 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";
    }

Solution

  • Try to look here Maybe you have not RequestMapping in your Post method?

    P.S

    Try to change

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

    to

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

    In your controller, try to change

    public String createUser(@ModelAttribute User user, HttpServletRequest request, Model model) 
    

    to

    public String createUser(@ModelAttribute("user") User user, HttpServletRequest request, Model model)