方案一:
不使用struts2的json,
1:导入json需要的jar包:(5个)
2:在Action中写:
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
//查询是否是通过简记码和仓库ID进行查询,获取的唯一货物
Goods g = goodsService.searchGoods(goods);
//使用JsonConfig去掉不用的属性,因为集合和对象影响json的转换
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"store","historys"});//过滤Goods对象的store和historys的属性
JSONObject object = null;
if(g!=null){
object = JSONObject.fromObject(g,config);
}
else{
g = new Goods();
object = JSONObject.fromObject(g,config);//转换成json数据时,Goods对象不能为null
}
//将服务器响应的数据返回到页面
PrintWriter out =ServletActionContext.getResponse().getWriter();
out.println(object.toString());
System.out.println(object.toString());
3:struts.xml文件
不需要做任何的配置
4:在jsp页面中写:
//获取简记码
var nm =$("#nm").val();
//获取仓库的id
var storeid =$("#storeid").val();
var jsonData ={"nm":nm,"storeid":storeid};
$.post("goodsAction_searchGoods.do",jsonData,function(data,textStatus){
data =eval("("+data+")");//如果不使用struts2的json传递数据,需要使用eval函数
if(data!=null){
$("#name").val(data.name);
$("#unit").val(data.unit);
$("#mode").val(data.mode);
$("#storeamount").html("库存数量:"+data.amount);
}
else{
$("#name").val("");
$("#unit").val("");
$("#mode").val("");
$("#storeamount").html("库存数量:"+0);
}
});
方案二:
使用struts2的json,
1:从struts2的包中,导入json需要的jar包:(1个)
2:在Action中写:
Goods g = goodsService.searchGoods(goods);
ServletActionContext.getContext().getValueStack().pop();
ServletActionContext.getContext().getValueStack().push(g);
return"search";
3:struts.xml文件
<actionname="goodsAction_*"class="goodsAction"method="{1}">
<resultname="search"type="json">
</result>
</action>
4:在jsp页面中写:
//获取简记码
var nm =$("#nm").val();
//获取仓库的id
var storeid =$("#storeid").val();
var jsonData = {"nm":nm,"storeid":storeid};
$.post("goodsAction_searchGoods.do",jsonData,function(data,textStatus){
if(data!=null){
$("#name").val(data.name);
$("#unit").val(data.unit);
$("#mode").val(data.mode);
$("#storeamount").html("库存数量:"+data.amount);
}
else{
$("#name").val("");
$("#unit").val("");
$("#mode").val("");
$("#storeamount").html("库存数量:"+0);
}
});
方案三:
public String selectResIp() throws IOException {
try {
String requestParameters = ParametersUtils.getRequestParameters("resVal");
if (!StringUtils.isEmpty(requestParameters)) {
tbwsaddrlist = webSer.getAllAddr(requestParameters);
tbwsuserlist = webSer.getAllUser(requestParameters);
String serid = this.getstrSerid();
if (!StringUtils.isEmpty(serid)) {
Tbwsser modify = webSer.modify(serid);
String addrlistss = modify.getAddrlist();
String userlistss = modify.getUserlist();
userMsgCheck = new Gson().toJson(userlistss);
addrMsgCheck = new Gson().toJson(addrlistss);
} else {
addrMsgCheck = "";
userMsgCheck = "";
}
msg = new Gson().toJson(tbwsaddrlist);
mrg = new Gson().toJson(tbwsuserlist);
}
} catch (Exception e) {
log.error("根据资源位置获取所在的iP", e);
}
return "selectResIp";
}
strus.xml
<result name="selectResIp" type="json">
<param name="includeProperties">msg,addrMsgCheck,userMsgCheck,mrg</param>
</result>
private String msg;
private String mrg;
private String addrMsgCheck;
private String userMsgCheck;
需要get set
jsp
function Resselect(){
var resVal = $("#resourcefield").val();
var serid = $("#serids").val();
$.post("webserManger_selectResIp.action",{"resVal":resVal,"serid":serid},
function(data){
var obj = eval("("+data.msg+")");
var objc = data.addrMsgCheck;
var userobj = eval("("+data.mrg+")");
var userobjc = data.userMsgCheck;
$("#addrlistLi").remove();
str="";
str+=" <li id=‘addrlistLi‘>";
str+="<span class=‘list_name‘>资源名称:</span>";
for(var i =0 ;i<obj.length;i++){
if(objc.indexOf(obj[i].addrid)>=0){
str+="<input id=‘addrList‘
type=‘checkbox‘ checked=‘checked‘ name=‘userAddrList‘
value="+obj[i].addrid+"/>"
str+=""+obj[i].addrname+"";
}else{
str+="<input id=‘addrList‘ type=‘checkbox‘ name=‘userAddrList‘ value="+obj[i].addrid+">"
str+=""+obj[i].addrname+"";
}
str+="  ";
}
str+= "<div class=‘clear‘></div>"
str+="</li>";
$("#descer").before(str);
$("#userListssd").remove();
utr = "";
utr+="<li id=‘userListssd‘>";
utr+="<span class=‘list_name‘>用户名称: </span>";
for(var j=0;j<userobj.length;j++){
if(userobjc.indexOf(userobj[j].userid)>=0){
utr+="<input id=‘userList‘
type=‘checkbox‘ checked=‘checked‘ name=‘userNameList‘
value="+userobj[j].userid+"/>";
utr+=""+userobj[j].username+"";
}else{
utr+="<input id=‘userList‘ type=‘checkbox‘ name=‘userNameList‘ value="+userobj[j].userid+">";
utr+=""+userobj[j].username+"";
}
utr+="  ";
}
utr +="<div class=‘clear‘></div>"
utr += "</li>";
$("#listport").after(utr);
});