1

目前我正在进入 Spring,除了表格之外,一切都在工作。提交表单时会调用控制器中的映射函数,但值始终为空。我希望你知道如何解决这个错误。我正在使用 Spring 3.1.2

控制器类:

@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;
}

}

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>

用户输入类:

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;
}
}
4

2 回答 2

3

似乎您的用户名输入元素具有 name=email。表单中没有用户名的输入框。除此以外的一切都是正确的,应该可以工作。

于 2012-12-05T21:30:51.160 回答
1

ModelAttribute在表格中添加

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

我认为这是因为我们没有在 GET 请求中设置命令值。当您发布请求时,将创建一个新的命令对象。尝试将此方法添加到控制器类中,看看是否可以解决..

 @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;
  }
于 2012-12-05T21:15:53.563 回答