开发者

Spring MVC POJO Bind Explanation

开发者 https://www.devze.com 2022-12-28 21:58 出处:网络
I am trying to understand how the binding of objects works in spring mvc.I have a Controller set up as follows, and want to have the freemarker template bind to the accessRequestBean.In the template I

I am trying to understand how the binding of objects works in spring mvc. I have a Controller set up as follows, and want to have the freemarker template bind to the accessRequestBean. In the template I have '<@spring.bind "command.accessRequestBean" />' but that causes errors... How do I bind a form to a POJO?

@Controller
@PreAuthorize("isAuthenticated()")
@RequestMapping("/access")
public class RemoteVendorAccessController {
    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Autowired
    private AdDao adDao;

    @Autowired
    private CadaDao cadaDao;

    @Autowired
    private UserAccessCache userAcce开发者_如何学GossCache;

    private AccessRequestBean accessRequestBean;

    @RequestMapping(method = RequestMethod.GET)
    public String requestAccess(ModelMap map){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        map.addAttribute("title", "Remote Vendor Access Request Form");

        try {
            AdUser user = adDao.getUserFromNt(username);
            map.addAttribute("user", user);
        } catch (UserDoesNotExistException e) {
            String error = "Could not get user information from AD";
            map.addAttribute("error", error);
            logger.error(error + "[" + username + "]", e);
        }

        // Get users manager
        AdUser manager = null;
        try {
            manager = adDao.getManagerFromNt(username);
            map.addAttribute("manager", manager);           
        } catch (Exception e) {
            String error = "Could not get manager information from AD";
            map.addAttribute("error", error);
            logger.error(error + "[" + username + "]", e);
        }

        return("access");
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processRequest(ModelMap map){
            // Want to validate POJO bean here          
        return(null);
    }

    public AccessRequestBean getAccessRequestBean() {
        return accessRequestBean;
    }

    public void setAccessRequestBean(AccessRequestBean accessRequestBean) {
        this.accessRequestBean = accessRequestBean;
    }


}


According to the Spring Documentation, the controller gets a reference to the object holding the data entered in the form by using the @ModelAttribute annotation on a method parameter. The parameter type would be your POJO class which corresponds to the object used to construct the form on the edit template. i.e.

@RequestMapping(method = RequestMethod.POST)
public String processRequest(
         @ModelAttribute POJO pojo,
         BindingResult result,
         ModelMap map){
    new POJOValidator().validate(pojo, result);
    if (result.hasErrors()) {
        return "pojoForm";
    }
    .
    .
    .  
    return(null);
}
0

精彩评论

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

关注公众号