论坛首页 Java企业应用论坛

[SpringMVC]修改源码使之能够更加智能的自动装配request请求参数.

浏览 47455 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-06-13  
s929498110 写道
从今天凌晨到现在,过去21个小时了。

我数了数java板块最新的总回复,才22个帖子有新回复

呵呵。ITEYE真是越来越冷清了!!!


ITEYE首页改版失败的后遗症!!!
0 请登录后投票
   发表时间:2012-06-13  
hellostory 写道

 
这种方式SpringMVC本来就支持!而且SpringMVC还可以绑定List、Map、Set等集合,楼主你可以参考这篇文章

http://www.iteye.com/topic/973918

 


list 等集合 s2也完美支持

看了这个帖子,spring支持  . 仍然不完善

他需要默认一个form里是一个pojo 才可以 

楼主提出的

<form>

 

  <input name="user.name" />

 

 <input name="client.name" />

 

</form>

 

 

这种形式的表单还没有完美解决方案,不过只能变通的把这些po作为同一个po的属性来变相获取!!??

0 请登录后投票
   发表时间:2012-06-13  
hellostory 写道
s929498110 写道
从今天凌晨到现在,过去21个小时了。

我数了数java板块最新的总回复,才22个帖子有新回复

呵呵。ITEYE真是越来越冷清了!!!


ITEYE首页改版失败的后遗症!!!

我感觉首页改版的像陀屎
1 请登录后投票
   发表时间:2012-06-13  
貌似有更简单的方法吧

@Controller
public class TestBinderController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("acc.");
    }

    @RequestMapping("/testInput")
    public String testBinderInput(){
       return "testInput";
    }

    @RequestMapping("/testOutput")
    public void testBinderOuput(@ModelAttribute Account account,BindingResult result){
        System.out.println(account);
    }
}

@Controller
public class TestBinderOtherController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        System.out.print("TestBinderOtherController:"+binder.getFieldDefaultPrefix());
    }

    @RequestMapping("/testOutput2")
    public void testBinderOuput(@ModelAttribute Account account){
        System.out.println(account);
    }
}

<html>
<body>
<form action="/testOutput" method="post">
    <input name="acc.loginId">
    <input type="submit">
</form>
</body>
</html>


可以把form 的action 分别 指向到/testOutput 和 /testOutput2 然后在controller 的 initBinder 和 testBinderOutput 中设置断点调试下,你可以发现奥妙
0 请登录后投票
   发表时间:2012-06-13  
dancewing 写道
貌似有更简单的方法吧

@Controller
public class TestBinderController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("acc.");
    }

    @RequestMapping("/testInput")
    public String testBinderInput(){
       return "testInput";
    }

    @RequestMapping("/testOutput")
    public void testBinderOuput(@ModelAttribute Account account,BindingResult result){
        System.out.println(account);
    }
}

@Controller
public class TestBinderOtherController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        System.out.print("TestBinderOtherController:"+binder.getFieldDefaultPrefix());
    }

    @RequestMapping("/testOutput2")
    public void testBinderOuput(@ModelAttribute Account account){
        System.out.println(account);
    }
}

<html>
<body>
<form action="/testOutput" method="post">
    <input name="acc.loginId">
    <input type="submit">
</form>
</body>
</html>


可以把form 的action 分别 指向到/testOutput 和 /testOutput2 然后在controller 的 initBinder 和 testBinderOutput 中设置断点调试下,你可以发现奥妙



我没有试你的方法,一方面我感觉这样比较麻烦。另一方面你的这个

binder.setFieldDefaultPrefix("acc.");

管用吗? 我的意思是,如果form中不仅仅有“acc.”, 如果form中有acc.loginId也有user.loginId那样该怎么办呢?

0 请登录后投票
   发表时间:2012-06-13  
kjj 写道
hellostory 写道

 
这种方式SpringMVC本来就支持!而且SpringMVC还可以绑定List、Map、Set等集合,楼主你可以参考这篇文章

http://www.iteye.com/topic/973918

 


list 等集合 s2也完美支持

看了这个帖子,spring支持  . 仍然不完善

他需要默认一个form里是一个pojo 才可以 

楼主提出的

<form>

 

  <input name="user.name" />

 

 <input name="client.name" />

 

</form>

 

 

这种形式的表单还没有完美解决方案,不过只能变通的把这些po作为同一个po的属性来变相获取!!??

 

对头!我的意思就是一个form中有多个pojo信息,并且这些pojo的字段名有重复的这种情况,就像你的user.name和client.name,必须有个前缀来区分才行。

根据我自己的理解,SpringMVC中并没有提供这种解决方案,initBinder不知道能不能解决这种情况

0 请登录后投票
   发表时间:2012-06-13  
你可不可以这样做呢?新建一个类FormBean,这个类里面包含两个对象 User和Client
public class FormBean {
   private class User user;
   private class Client client;
  .........
}


@RequestMapping(.....)
public void (FormBean formBean) {
.....
}


前台传递的参数名称是 user.name和client.name,这样不就可以了
0 请登录后投票
   发表时间:2012-06-13   最后修改:2012-06-13
@Controller
public class TestBinderController {

    @InitBinder("account")
    public void initBinder1(WebDataBinder binder) {
            binder.setFieldDefaultPrefix("acc.");
    }
    @InitBinder("user")
    public void initBinder2(WebDataBinder binder) {
            binder.setFieldDefaultPrefix("user.");
    }

    @RequestMapping("/testInput")
    public String testBinderInput(){
       return "testInput";
    }

    @RequestMapping("/testOutput")
    public void testBinderOuput(@ModelAttribute Account account,@ModelAttribute User user,BindingResult result){
        System.out.println(user);
        System.out.println(account);
    }
}

<html>

<body>
<form action="/testOutput" method="post">
    <input name="acc.loginId">
    <input name="user.loginid">
    <input type="submit">
</form>
</body>
</html>
0 请登录后投票
   发表时间:2012-06-13  
160649888 写道
你可不可以这样做呢?新建一个类FormBean,这个类里面包含两个对象 User和Client
public class FormBean {
   private class User user;
   private class Client client;
  .........
}


@RequestMapping(.....)
public void (FormBean formBean) {
.....
}


前台传递的参数名称是 user.name和client.name,这样不就可以了

你这主意不错。。
只是我已经解决了一个form多个pojo的问题,不需要再新建这种辅助pojo类了
0 请登录后投票
   发表时间:2012-06-13  

@RequestMapping(.....)
public void (User user,Client client) {
.....
}



这样不行吗?在我记忆里应该可以的吧
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics