ITeye博客

jQuery param serialization and Struts2 built in type conversion not compatible

lerluc · 2010-11-21 05:31 · Web前端 · Ajax · jQuery · JavaScript · Markdown

页面上jQuery需要把一个复杂类型的javascript数组对象作为参数传到Struts2 action,但是他们的默认格式不匹配导致action无法正确获得参数。 Struts2 action[code="java"] class SomeAction{ List list; //getter setter //execute() } class SomeBean{ int id; //getter setter } [/code] 参数为以下格式(OGNL格式)时,Struts2 action能正确映射 [code="http post"] list[0].id=1 [/code] 但是当jQuery发送ajax call的时候 [code="javascript"] $.post(url, {list: [{id: 1}]}) [/code] 参数会被序列化为 [code="http post"] list[0][id]=1 [/code] 要转换成功,要么把jQuery的序列化格式改掉来迎合Struts2,要么给Struts2 action写个converter来迎合jQuery,前者更简单。于是 [code="jQuery-1.4.4.js line6134"] //buildParams( prefix + "[" + k + "]", v, traditional, add ); buildParams( prefix + "." + k, v, traditional, add ); [/code] 改jQuery源码并不是个好做法,最好是做个patch把$.param()覆盖掉.

评论(1)

  1. #1 楼 · lykm02 · 2011-04-20 11:13

    Thanks very much. 终于找到解决方案了。谢谢。 不过,converter应该怎么写呢? 可否给个例子?

评论功能维护中,当前仅展示历史评论。