struts2自动封装ajax提交过来的数据

aspshipin 2013-10-06

情况是这样的:

Action:

public class HomeMemberAction extends ParentAction{

private HomeMember homeMember;

//查询方法

public String homeMemberList(){
System.out.println("===" + homeMember.getMemberName());
homeMemberList = this.homeMemberService.getHomeMemberList();
return Action.SUCCESS;
}

public HomeMember getHomeMember() {
return homeMember;
}


public void setHomeMember(HomeMember homeMember) {
this.homeMember = homeMember;
}

}

配置文件:

<action name="homeMemberList" class="com.issweb.homemember.web.HomeMemberAction" method="homeMemberList">
<result type="json">
<!-- 不序列化Action里为null的字段 -->
<param name="excludeNullProperties">true</param>
<param name="root">homeMemberList</param>
</result>
</action>

js:

function loadHomeMemberData(){
//组装参数
var memberName = $("#memberName").val();
var sex = $("#sex").val();
var age = $("#age").val();
var nickName = $("#nickName").val();

//请求后台
$.ajax({
url:"/member/homeMemberList.action",
type:"post",
dataType:"json",
data:{
"homeMember.memberName":memberName,
"homeMember.sex":sex,
"homeMember.age":age,
"homeMember.nickName":nickName
},
beforeSend:function(){
$('#loading').show();//请求之前,展显进度条
},
complete:function(){
$('#loading').hide();//请求之后,隐藏进度条
},
success:createTable
});
}

描述:

ajax提交数据到Action中,如果想让提交的数据自动封装到homeMember 类中,需要在提交的时候,做以下手脚:

data:{
"homeMember.memberName":memberName,
"homeMember.sex":sex,
"homeMember.age":age,
"homeMember.nickName":nickName
},

以Action对象中的属性名字做为前缀。

千万要注意:js中获得参数值方式一定要正确,不然后台获取不到参数,例如:var memberName = $("#memberName").val();

如果写成:var memberName = $("memberName").val(); 前台也不会报错,后台也获取不到值,很不好确定是哪里的原因。

相关推荐

mmywcoco / 0评论 2020-06-06