论坛首页 Java企业应用论坛

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

浏览 47456 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-06-13  
dancewing 写道
@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  
icanfly 写道

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



这样不行吗?在我记忆里应该可以的吧


我的意思是,user里面有个name字段、client中有一个name字段。这种情况下SpringMVC分辨不出来两个name属于谁的。
而如果想像Struts2一样加个user.前缀或者client.前缀,SpringMVC是不支持这种功能的,前面有人说initBinder我不知道行不行,我只知道我一楼的那种方法可以解决。
0 请登录后投票
   发表时间:2012-06-13  
yxb1990 写道
dancewing 写道
@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>

这个方法试了下,可以,学到了,感觉如果楼主说的这个情况下比较少发生的话,可以用这种方法,改源代码暂时还动不了,

  多谢

到现在为止我是知道三种方法了
第一种是直接修改SpringMVC源码
第二种是使用@initBinder
第三种是新建Form封装类

条条大路通罗马~~~~~
0 请登录后投票
   发表时间:2012-06-13  
我印象中,好像是支持的呀
0 请登录后投票
   发表时间:2012-06-13  
楼主你重复造轮子了,Spring天生就支持你这个功能的。。。查下关于Spring的Simple Form之类的吧。。。
0 请登录后投票
   发表时间:2012-06-13  
wcdzxxgc 写道
楼主你重复造轮子了,Spring天生就支持你这个功能的。。。查下关于Spring的Simple Form之类的吧。。。


我并没有重复造轮子,我感觉你没有认真看我的需求和说明。详细解释的我前面已经解释过了

 

0 请登录后投票
   发表时间:2012-06-13  
wcdzxxgc 写道
楼主你重复造轮子了,Spring天生就支持你这个功能的。。。查下关于Spring的Simple Form之类的吧。。。



我感觉你并没有认真负责的和我谈论这个话题,但是我还是和你详细说一下吧。
现在需要在Controller中接受两个对象a、b。两个类如下:

class A{
   private String name;
   private String mark;
   private String index;
}
class B{
   private String name;
   private String mark;
   private String index;
}

 

 因此需要在form中如下编写html:

<form>
    <!-- A信息 -->
    <input type='text' name='name'/>
    <input type='text' name='mark'/>
    <input type='text' name='index'/>
    <!-- B信息 -->
    <input type='text' name='name'/>
    <input type='text' name='mark'/>
    <input type='text' name='index'/>
</form>

 

 Spring默认支持上面这种格式的表单,但是这种情况下明显无法区分A和B的信息。因此便需要按下面这样编写html:

<form>
    <!-- A信息 -->
    <input type='text' name='a.name'/>
    <input type='text' name='a.mark'/>
    <input type='text' name='a.index'/>
    <!-- B信息 -->
    <input type='text' name='b.name'/>
    <input type='text' name='b.mark'/>
    <input type='text' name='b.index'/>
</form>

 

这样写的话第一种方法,需要程序员在程序中进行一些多余的编程。

第一种就是多写两个@initBinder方法

第二种就是多写一个XXXXForm的pojo类

 

我不知道你有什么更好的方法,有的话请说的明白清晰一点。

0 请登录后投票
   发表时间:2012-06-13   最后修改:2012-06-13
楼主厉害!  源码级别的研究了! 佩服
0 请登录后投票
   发表时间:2012-06-13  
dispatcher-servlet.xml示例中有个
    <!--
    <!-<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">-->
    <!--<property name="webBindingInitializer">-->
    <!--<bean class="com.comet.demo.common.web.DemoBindingInitializer"/>-->
    <!--</property>-->
    <!--</bean>-->
    <!---->
0 请登录后投票
   发表时间:2012-06-13  
icanfly 写道

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



这样不行吗?在我记忆里应该可以的吧



我记得这样也是可以的.本身即支持。

参数直接就是user.name / client.name 就OK

如果2个都User对象,使用注解区分开就可以了。

@request("user1")User user,@request("user2")User user2
0 请登录后投票
论坛首页 Java企业应用版

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