日期:2014-05-17  浏览次数:20744 次

spring mvc获取表单提交的对象
第一次用spring mvc框架,不是很懂。

页面提交表单,控件属性都是对象.属性形式,在controller中也声明了对象,set方法也有,不能想struts那样将页面属性值注入到对象中去???




------解决方案--------------------
是可以直接注入到对象中去的,绝对没有问题。
------解决方案--------------------
可以的哦   比方说  struts2    <input name="user.name"/>   Action中需要有user对象
spring   <input name="name"/>      @controller 中   @RequestMapping("/***")方法参数直接给 user对象即可
------解决方案--------------------
你action中的对象是不是又实例化了一遍,如果这样对象肯定为空。
------解决方案--------------------
 @RequestMapping(value = "/user/add", method = {RequestMethod.POST})
    public String add(Model model, @ModelAttribute("command") @Valid UserModel command, BindingResult result) {
        
        //如果有验证错误 返回到form页面
        if(result.hasErrors()) {
            model.addAttribute(Constants.COMMAND, command);
            return toAdd(model);
        }
         userService.save(command);
        return "redirect:/user/success";
    }




@Valid   这个接受对象
------解决方案--------------------
 <input name="user.name"/> 页面是对象.属性 那action中把get方法也写了试试。不应该接不到的。
------解决方案--------------------
action中有user对象,并且user.Clsss中有name属性,在action中提供get和set方法应该是可以的,你尝试一下
------解决方案--------------------
4楼正解