+ -
当前位置:首页 → 问答吧 → ext ajax 提交json对象给struts2,struts2无法解析对象

ext ajax 提交json对象给struts2,struts2无法解析对象

时间:2010-11-08

来源:互联网

前台:var obj ={"newid":"5"};
obj["hero.heroName"]="grg";
obj["hero.hid"]=6;
Ext.Ajax.request({
  url: 'je/heroTest_giveHeroInfo',
  method:"POST",
  success: function(data){
  Ext.Msg.show(data.responseText);
  },
 
  failure: function(){alert("failure");},
  //这种方式,struts2只能接受到hero.hid,hero.heroName会为空
  params:{'hero.heroName':'sdf','hero.hid':45}
  //这种方式hero对象就为空了,什么都接受不到
//params: Ext.encode(obj)
  });
je/heroTest_giveHeroInfo action:

public class HeroTestAction extends CommonAction {

private HeroTest hero;

public void setHero(HeroTest hero) {
this.hero = hero;
}
private int newid;

public void setNewid(int newid) {
this.newid = newid;
}
private String json;

public void setJson(String json) {
this.json = json;
}
public void giveHeroInfo() throws IOException{
System.out.println(json);
System.out.println("新ID:"+newid);
System.out.println("英雄的名字:"+hero.getHeroName());
System.out.println("英雄的id:"+hero.getHid());
sendMsg(hero.getHeroName());
//sendMsg(((Integer)newid).toString());
}
private void sendMsg(String content) throws IOException {
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.getWriter().write(content);
}
}

struts2.xml配置文件:
<package name="jsonExt" namespace="/je" extends="json-default">
<interceptors>
  <interceptor-stack name="myDefaultStack">
  <interceptor-ref name="json"/>
  <interceptor-ref name="defaultStack"/>
  </interceptor-stack>
 </interceptors>
 <default-interceptor-ref name="myDefaultStack"/>
<action name="heroTest_*" class="com.foodchina.mbca.action.test.HeroTestAction" method="{1}">
<result type="json"/>

 
  </action>
   
   
</package> 

如红字所示,问题就是struts2无法接受ext ajax传过来的json对象。我使用jquery可以成功,但是ext我哪里做的不对?

作者: elfenliedef   发布时间: 2010-11-08

<interceptor-stack name="myDefaultStack">
  <interceptor-ref name="json"/>
  <interceptor-ref name="defaultStack"/>
  </interceptor-stack>
 </interceptors>
我不敢肯定,但我觉得这个地方的配置应该是:
<interceptor-stack name="myDefaultStack">
  <interceptor-ref name="defaultStack"/>
  <interceptor-ref name="json"/>
  </interceptor-stack>
 </interceptors>

作者: licip   发布时间: 2010-11-08