Java 如何在Spring2.5中最有效地绑定表单数据?

Java 如何在Spring2.5中最有效地绑定表单数据?,java,spring,spring-mvc,Java,Spring,Spring Mvc,将表单数据绑定到模型的最佳方式是什么?我的意思是我有一个简单的模型类: public class LoginCommand { private String login; private String password; //getters and setters } 在Spring2.5中,将表单数据绑定到此命令的最佳方式是什么?@InitBinder注释有帮助吗?我不明白它是怎么工作的。。我认为它可能起作用的一种方式是贝娄 @Controller public

将表单数据绑定到模型的最佳方式是什么?我的意思是我有一个简单的模型类:

public class LoginCommand {

    private String login;
    private String password;

    //getters and setters
}
在Spring2.5中,将表单数据绑定到此命令的最佳方式是什么?@InitBinder注释有帮助吗?我不明白它是怎么工作的。。我认为它可能起作用的一种方式是贝娄

@Controller
public LoginController {
    @RequestMapping(value = "/login/*", method = RequestMethod.POST)
    public ModelAndView loginAction(@ModelAttribute("loginCommand") LoginCommand lc, BindingResult result) {

        new LoginCommandValidator().validate(lc, result);
        if (result.hasErrors()){
            // didn't validate
        } else {
            // check against db
        }

    }
}

这是最好的方法吗?

对于登录操作,您可能需要研究Spring安全性。除此之外,我将为更一般的问题提供一些见解,并建议您研究@RequestParam注释

@RequestMapping(value = "/login/*", method = RequestMethod.POST)
public ModelAndView handleLogin(@RequestParam("login")    String username,
                                @RequestParam("password") String password) {
    // create constructor, remove setters to make this immutable
    LoginCommand lc = new LoginCommand(username, password);
    // more code here...
}

我意识到解决这个问题可能有多种方法,但我喜欢这种方法,因为它简单明了;或者更简单地说,它没有那么神奇,也很容易阅读,尽管有点冗长

我确实看过了,但是使用ModelAttribute、@InitBinder等新的东西很难理解。在这种情况下,url会是什么样子?比如:/login/?login=login&password=password?URL应该是/login,因为我们使用的是method=RequestMethod.POST登录名和密码将从HTTP正文中读取。所以基本上@RequestParam是字段名,是吗?