Springmvc接受不同对象的同名参数解决方法:

来源:互联网 发布:linux防暴力破解 编辑:程序博客网 时间:2024/05/20 18:49

方法一:@initbind注解的形式添加绑定对象(官方推荐用法)

         @InitBinder

         public voidinitCategoryBean(WebDataBinder binder){

                   binder.setFieldDefaultPrefix("category.");

         }

         /**

          * 修改分类

          */

         @RequestMapping("/alterCategory")

         publicString modifyCategory(Category category,Model model) {

                   try{

                            categoryService.alterCategory(category);

                            model.addAttribute("message","修改分类成功");

                   }catch(Exception e) {

                            model.addAttribute("message","修改分类失败");

                   }

                   return"/jsp/message.jsp";

         }

 

JSP页面:

<input type="hidden"  name="category.id"  value=" ${id }"/>

             <table width="500px;">

                      <tr>

                                <td>分类名称:</td>

                                <td><inputtype="text" name="category.name" style="width:200px"></td>

                      </tr>

 

 

 

方法二:添加@ModelAttribute("category")注解(有时好像没用..后来再看)

@RequestMapping("/alterCategory")

         publicString modifyCategory(@ModelAttribute("category") Categorycategory,Model model) {

                   try{

                            categoryService.alterCategory(category);

                            model.addAttribute("message","修改分类成功");

                   }catch(Exception e) {

                            model.addAttribute("message","修改分类失败");

                   }

                   return"/jsp/message.jsp";

 

原创粉丝点击