Pergunta

Currently i am getting into Spring, everything is working so far, beside forms. The mapped function in the Controller gets called when the form gets submited, but the values are allways null. I hope you have any idea how to solve this error. I am using Spring 3.1.2

The controller-class:

@Controller
public class MyController{


@RequestMapping(value="/registerSave.html", method=RequestMethod.POST)
public ModelAndView registerSave(@ModelAttribute("UserInput") UserInput a) {
    Logger log = LoggerFactory.getLogger(MeinController.class);
    log.info("username: " + a.getUsername()); //a.getUserName() is null >.<
    ModelAndView mav= new ModelAndView();
    User registerUser= new User(a.getUsername(), securityService.encodePassword(a.getPassword()), a.getFullname(), a.getEmail());
    try{
        messageService.createUser(registerUser);
        mav.setViewName("registerSuccess");
    } catch(Exception exception) {
        log.info("cannot create user "+  a.getUsername()+ " : "+ exception.getMessage());
        mav.setViewName("registerFail");
    }
    return mav;
}

}

The Jsp:

<!DOCTYPE HTML>
<html>
<body > 
<article>
    <section>
        <form method="post" action="registerSave.html" modelAttribute="UserInput">
            <section>E-Mail Adresse:<br>
                <input id="email" autofocus="autofocus" size="35" type="text" required="required" name="email" maxlength="30"/>
            </section>
            <section>Username:<br>
                <input size="35" type="text" required="required" name="email" maxlength="30"/>
            </section>
            <section>Password:<br>
                <input size="35" required="required" type="password" name="passwort"/>
            </section>
            <section class="right">Password:<br>
                <input id="inPw2" title="Password$Bitte geben sie hier das selbe Passwort ein, wie im ersten Feld$Dies dient der überprüfung, für den Fall, dass sie sich vertippt haben" size="35" required="required" type="password" name="passwort"/>
            </section>
            <section >Your full name:<br>
                <input id="fullname" size="35" type="text" required="required" name="email" maxlength="30"/>
            </section>          
            <p>
                <input size="35" type="submit" value="Regestieren"/>
            </p>
        </form>
    </section>
</article>
</body>
</html>

the class UserInput:

public class UserInput {

private String username;
private String password; 
private String fullname;
private String email;
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 getFullname() {
    return fullname;
}
public void setFullname(String fullname) {
    this.fullname = fullname;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
}
Foi útil?

Solução

Seems like your username input element has name=email. There is no input box in the form for username. Everything but this is right and should work.

Outras dicas

Please add the ModelAttribute to the form

<form method="post" action="registerSave.html" modelAttribute="UserInput">

I think this is because we are not setting the Command values in the GET request. A new command object is getting created when you post the request. Try adding this method to the controller class and see if that solves..

 @RequestMapping(value="/registerSave.html", method=RequestMethod.GET)
 public ModelAndView registerGet() {
    ModelAndView mav = new ModelAndView();
    UserInput userInput = new UserInput;
    mav.addObject("UserInput", userInput);
    mav.setViewName("registerSave.html");
    return mav;
  }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top