`
yjgyjg4
  • 浏览: 119867 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

liferay 返回 json,jquery处理

    博客分类:
  • ajax
阅读更多

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
import com.autotoll.jv.erp.proxy.PortletActionBeanProxy;
import com.autotoll.jv.erp.util.ErpStatus;
import com.autotoll.jv.pojo.impl.UserInfo;
import com.liferay.portal.json.JSONArrayImpl;
import com.liferay.portal.json.JSONObjectImpl;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.model.User;
import com.liferay.portal.struts.AJAXAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.WebKeys;

public class GetCustNodeAjax extends AJAXAction{

	@SuppressWarnings("unchecked")
	public String getText(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest req, HttpServletResponse response) throws Exception {
		
		ThemeDisplay themeDisplay = 
			(ThemeDisplay) req.getAttribute(WebKeys.THEME_DISPLAY);
		User user = themeDisplay.getUser();
		UserInfo userInfo = PortletActionBeanProxy.
				getInstance().getUserInfoSvc().
				findUserInfoById(user.getUuid());
		
		String lanageId = themeDisplay.getLanguageId();
		String custId = req.getParameter("custId");
		//货源地
		String s_type = ErpStatus.COMP_NODE_HUOYUAN;
		List<Map<String, Object>> s_cityList = PortletActionBeanProxy.
				getInstance().getCityTableSvc().
				findCityTableByNodeId(new Integer(custId), 
				userInfo.getCompInfo().getCompId(), new Short(s_type));
		//目的地
		String d_type = ErpStatus.COMP_NODE_MUDIDI;
		List<Map<String, Object>> d_cityList = PortletActionBeanProxy.
				getInstance().getCityTableSvc().
				findCityTableByNodeId(new Integer(custId), 
				userInfo.getCompInfo().getCompId(), new Short(d_type));
		
		JSONObject jsonObj = new JSONObject();
		//货源地和目的地
		jsonObj = createJson(s_cityList,d_cityList,lanageId);
		//System.out.println("json----"+jsonObj.toString());
		return jsonObj.toString();
	}
	/**
	 * 创建json
	 * @param list
	 * @param lanageId
	 */
	private JSONObject createJson(List<Map<String,Object>> s_list,
			List<Map<String,Object>> d_list,String lanageId){
		
		JSONObject jsonObj = new JSONObject();
		JSONObjectImpl jsonImpl = new JSONObjectImpl();
		JSONArray s_jsonArrId = new JSONArrayImpl();
		JSONArray s_jsonArrAdr = new JSONArrayImpl();
		//货源地
		if (s_list !=null && s_list.size()>0){
			for(int i=0;i<s_list.size();i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map = s_list.get(i);
				String custNodeId = String.valueOf(map.get("0"));
				String proName_tc = String.valueOf(map.get("1"));
				String proName_en = String.valueOf(map.get("2"));
				String proName_sc = String.valueOf(map.get("3"));
				String cityName_tc = String.valueOf(map.get("4"));
				String cityName_en = String.valueOf(map.get("5"));
				String cityName_sc = String.valueOf(map.get("6"));
				String nodeAdr = String.valueOf(map.get("7"));

				s_jsonArrId.put(custNodeId);
				jsonImpl.put("s_custNodeId", s_jsonArrId);
				if (lanageId.equals("zh_CN")) {
					s_jsonArrAdr.put(proName_sc+cityName_sc+nodeAdr);
				} else if (lanageId.equals("zh_TW")){
					s_jsonArrAdr.put(proName_tc+cityName_tc+nodeAdr);
				}else {
					s_jsonArrAdr.put(proName_en+cityName_en+nodeAdr);
				}
			}
		}
		//目的地
		JSONArray d_jsonArrId = new JSONArrayImpl();
		JSONArray d_jsonArrAdr = new JSONArrayImpl();
		
		if (d_list !=null && d_list.size()>0){ 
			for(int i=0;i<d_list.size();i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map = d_list.get(i);
				String custNodeId = String.valueOf(map.get("0"));
				String proName_tc = String.valueOf(map.get("1"));
				String proName_en = String.valueOf(map.get("2"));
				String proName_sc = String.valueOf(map.get("3"));
				String cityName_tc = String.valueOf(map.get("4"));
				String cityName_en = String.valueOf(map.get("5"));
				String cityName_sc = String.valueOf(map.get("6"));
				String nodeAdr = String.valueOf(map.get("7"));

				d_jsonArrId.put(custNodeId);
				
				if (lanageId.equals("zh_CN")) {
					d_jsonArrAdr.put(proName_sc+cityName_sc+nodeAdr);
				} else if (lanageId.equals("zh_TW")){
					d_jsonArrAdr.put(proName_tc+cityName_tc+nodeAdr);
				}else {
					d_jsonArrAdr.put(proName_en+cityName_en+nodeAdr);
				}
			}
		} 
		jsonImpl.put("s_address", s_jsonArrAdr);
		jsonImpl.put("s_custNodeId", s_jsonArrId);
		jsonImpl.put("d_custNodeId", d_jsonArrId);
		jsonImpl.put("d_address", d_jsonArrAdr);
		jsonImpl.put("totalNum", s_list.size()+d_list.size());
		
		jsonObj = jsonImpl.getJSONObject();
		return jsonObj;
	}
}

jsp 页面:
function getCustNodeList(obj){
	var custId = obj.options[obj.selectedIndex].value;
	var starSelect = document.getElementById('startNodeList');
	var deSelect = document.getElementById('detionNodeList');
	
	if(custId != "choose" && custId !="") {
		jQuery.ajax(
		{url: '<%= themeDisplay.getPathMain() %>/erp/getJson',	
		 data: {custId:custId},	
		 success: setCustNode});
	} else {
		starSelect.options.length = 0;
		deSelect.options.length = 0;
		starSelect.options.add(new Option("<liferay-ui:message key="erp.select.option.choose"/>","choose"));
		deSelect.options.add(new Option("<liferay-ui:message key="erp.select.option.choose"/>","choose"));
	}
}
function setCustNode(json) {
	var jsonFile = eval('('+json+')');
	var num = jsonFile.totalNum;
	var starSelect = document.getElementById('startNodeList');
	var deSelect = document.getElementById('detionNodeList');
	starSelect.options.length = 0;
	deSelect.options.length = 0;
	
	for(var i=0 ;i<num-1 ;i++){
		var s_custNodeId; var s_custNodeAdr;
		var d_custNodeId; var d_custNodeAdr;
		if(jsonFile.s_custNodeId != null) {
		   s_custNodeId = jsonFile.s_custNodeId[i];
		}
		if (jsonFile.s_address !=null) {
			s_custNodeAdr = jsonFile.s_address[i];
		}
		if(jsonFile.s_address !=null && jsonFile.s_address !=null) {
			starSelect.options.add(new Option(s_custNodeAdr,s_custNodeId));	
		}
				if (jsonFile.d_custNodeId != null ) {
			d_custNodeId = jsonFile.d_custNodeId[i];
		}
		if (jsonFile.d_address !=null) {
			d_custNodeAdr = jsonFile.d_address[i];
		}
		if (jsonFile.d_custNodeId !=null && jsonFile.d_address != null) {
			deSelect.options.add(new Option(d_custNodeAdr,d_custNodeId));	
		}
	}   
	starSelect.options.add(new Option("<liferay-ui:message key="erp.select.option.choose"/>","choose"));
	deSelect.options.add(new Option("<liferay-ui:message key="erp.select.option.choose"/>","choose"));	
}
分享到:
评论

相关推荐

    struts2_spring_json.rar_liferay struts2 json_portlet json_spring

    1. **Struts2 JSON插件**:了解如何配置Struts2的json-plugin,使得Action可以直接返回JSON结果,而无需额外的视图层处理。 2. **Spring整合Struts2**:学习如何在Spring容器中管理Struts2的Action类,以及如何通过...

    liferay6.0.5在ie6ie7操作布局等功能时的js错误解决方法和补丁包

    总之,处理Liferay在旧版IE中的兼容性问题需要对JavaScript、CSS和Liferay的架构有深入理解。通过上述方法,应该能有效解决"不停loading"和JavaScript错误的问题,使Liferay 6.0.5在IE6和IE7下也能正常运行。在后续...

    基于serverResource方式的liferay中ajax方式的的实现

    在Liferay中,serverResource是一种处理客户端请求并返回资源的方式,尤其适用于异步请求,如Ajax。这种方式使得开发者能够更新页面的部分内容,而无需刷新整个页面,从而提高用户体验。下面我们将逐步解析这个过程...

    Liferay architecture 5

    Liferay Portal支持多种技术,包括AJAX、Apache ServiceMix、ehcache、Groovy、Hibernate、ICEfaces、Java J2EE/JEE、jBPM、JGroups、jQuery JavaScript Framework、Lucene、MuleSource ESB、PHP、Ruby、Seam、...

    liferayPortal

    - jQuery JavaScript Framework:一个流行的JavaScript库。 - Lucene:一个高性能全文搜索引擎库。 - Mule Source ESB:一个企业服务总线。 - PHP:一种广泛使用的脚本语言。 - Ruby:一种简单快速的编程语言。 - ...

    portlet:jQUeryUI Portlet和PHP后端

    这可以通过jQuery的$.ajax()函数或者更高级的$.getJSON()、$.post()等方法实现。例如,当用户在portlet界面上触发一个操作,如点击按钮,portlet可以通过Ajax发送请求到PHP后端,PHP处理数据后返回结果,再由portlet...

    ajax portlet

    在Ajax环境下,通常是在动作或资源请求阶段发送Ajax请求,然后在服务器端处理,返回结果,客户端JavaScript处理并更新DOM树。 5. **portlet配置**: 配置portlet以支持Ajax,可能需要在portlet.xml中声明对Ajax的...

    spring2.0MVC+spring2.5MVC+portletMVC

    4. **AJAX支持**:通过jQuery和JSON,Spring 2.5更好地支持了异步请求和响应。 5. **更多注解驱动的开发**:例如,@ModelAttribute用于将请求参数绑定到模型对象,@InitBinder用于初始化数据绑定行为。 至于...

Global site tag (gtag.js) - Google Analytics