开发者

Spring MVC, Form and Dependency Injection

开发者 https://www.devze.com 2023-03-13 13:48 出处:网络
Currently learning Spring MVC with Spring 3, I\'m trying to find the correct way to receive a form and handle it. That is what I\'ve got at the moment :

Currently learning Spring MVC with Spring 3, I'm trying to find the correct way to receive a form and handle it. That is what I've got at the moment :

    @RequestMapping(method = RequestMethod.POST)
public String saveUserInfoIntoSession(Personne personne,
        HttpSession session, ModelMap model) {
    //

    session.setAttribute("personne", personne);
    return "ageAndAddress";
}
  1. Is this the correct 开发者_运维问答way to handle a SimpleForm ? As the SimpleFormController has been deprecated ...
  2. What if, Personne was not a class but an Interface, and I would like to, say, have an xml configuration find to decide which implementation I want to use ?

Thanks


You may simplify it to something like this...

@Controller
@SessionAttributes("personne")
public class MyController {
    ....
    @RequestMapping(method = RequestMethod.POST)
    public String saveUserInfoIntoSession(@ModelAttribute Personne personne, ModelMap model) {
        return "ageAndAddress";
    }

However, I would suggest you avoid passing form state via HTTP sessions. Request parameters + model is usually sufficient for most cases. If you have a complex wizard-like form, try better Spring WebFlow.

0

精彩评论

暂无评论...
验证码 换一张
取 消