SSH框架Action中JSON操作及struct.xml配置

页面向action中发送AJAX、json请求前面已经讲过了,这里不再重复,今天主要讲一下后台如何处理并返回json数据。

方式不唯一,会用其中一种就可以,我用的是Map

首先action中创建map对象

	private Map result;//用于返回AJAX

	public Map getResult() {
		return result;
	}

	public void setResult(Map result) {
		this.result = result;
	}

在需要返回的处理方法中填充map,map中可以放入String,boolean,实体对象等。其中editEdutype为对象

				map.put("result",true);
//				map.put("edutypid",editEdutype.getEdutypid());
//				map.put("edutypename", editEdutype.getEdutypename());
//				map.put("edutypecharcter", editEdutype.getEdutypecharcter());
//				map.put("edutypedesc", editEdutype.getEdutypedesc());
//				map.put("status", editEdutype.getStatus());
				map.put("editEdutype", editEdutype);

然后在structs.xml中配置

首先,package要继承json-default

    <package name="default" namespace="/" extends="json-default">

然后配置action

<action name="user_*" class="newEmployeeAction" method="{1}">
		<result name="ajaxlistSUCCESS" type="json">
			<param name="root">result</param>
		</result>
</action>


最后在jsp中调用返回的json

 success: function(data) {  
	                  if(true == data.result){
	                  	var edutypID = data.edutypid
	                  	//alert(edutypID);
	                  	//document.getElementById("picUserID").value=userID; 
	                  	alert("保存成功");
	                  	
	                  	var url = "edutype_pagelist.action";
          				// alert("url为:" + url);
          				window.location.href=url;
	                  } else {
	                  	
	                  	alert("保存失败");
	                  
	                  }    


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值