0

我想从输入表单中获取数据(登录数据->密码+用户名)并将其保存到 bean 中。然后,我想拿这个 bean 和里面的数据做一个获取 reuest。

我声明一个类:

public class Login {



private String username;
private String password;




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

}

getrequest 如下:

  @RequestMapping(value = "/log",method = RequestMethod.GET)
     public String addPersonLogin( Model model,HttpServletRequest request) {

       //take the bean here
    }

谁能告诉我如何创建 jsp 以及如何将 bean 放入 addPersonLogin 中?

4

1 回答 1

1

你可以这样做(使用@ModelAttribute):

@InitBinder
public void initBinder(WebDataBinder binder) {
    // You can register your validator for bean Login here
}

@RequestMapping(value = "/log",method = RequestMethod.GET)
public String addPersonLogin(@ModelAttribute Login login, HttpServletRequest request) {
    // To make it work your form fields should have names equals to bean fields names
    login.getUserName();
    login.getPassword();
}
于 2012-05-11T10:35:42.743 回答