對jquery的ajax的一個封裝 – Javascript教程_JS教程_技術文章 – 程式設計聯盟

<pre class="java" name="code"><pre class="java" name="code"><pre class="java" name="code">package com.aegon_cnooc.oa.admin.action; 
 
import java.io.PrintWriter; 
import java.util.List; 
import java.util.Map; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import net.sf.json.JSONArray; 
import net.sf.json.JSONSerializer; 
import net.sf.json.util.JSONUtils; 
 
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward; 
import org.apache.struts.action.ActionMapping; 
 
import com.aegon_cnooc.framework.base.action.BaseAction; 
import com.aegon_cnooc.framework.log.Log; 
import com.aegon_cnooc.oa.admin.service.LoginService; 
import com.aegon_cnooc.util.LogPathUtil; 
/**
 * 
 * @Despcrition:
 * @Author: liuxinghui
 * @Version: 1.0
 * @CreateDate: Jul 12, 2011
 */ 
public class OnloadEmployeeAction extends BaseAction{ 
    private LoginService loginService; 
    Log logWrite=new Log(); 
/**
 * 組織架構定義中點擊單選按鈕獲得相應的員工
 */ 
    public ActionForward executeAction(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) 
            throws Exception { 
        logWrite.writeLog(LogPathUtil.getPath(), "start OnloadEmployeeAction…", "info"); 
        String employeeType=request.getParameter("employee_type"); 
        if("all".equals(employeeType)){ 
        //Employees all of company 
        List employeesAllOfCompany=loginService.getEmployeesAllOfCompany(); 
        logWrite.writeLog(LogPathUtil.getPath(), "Load employeesAllOfCompany  employeesAllOfCompany.size():"+employeesAllOfCompany.size(), "info"); 
        JSONArray json = (JSONArray)JSONSerializer.toJSON(employeesAllOfCompany); 
        response.setContentType("application/json;charset=gbk");  
        response.setCharacterEncoding("gbk");  
        PrintWriter pw = response.getWriter();  
        pw.write(JSONUtils.valueToString(json));  
        pw.flush(); 
        }else{ 
        //Employees not belong to any groupp 
        List employeeNotBelongToAnyGroup=loginService.getEmployeesNotBelongToAnyGroup(); 
        logWrite.writeLog(LogPathUtil.getPath(), "Load employeeNotBelongToAnyGroup  employeeNotBelongToAnyGroup.size():"+employeeNotBelongToAnyGroup.size(), "info"); 
        JSONArray json = (JSONArray)JSONSerializer.toJSON(employeeNotBelongToAnyGroup); 
        response.setContentType("application/json;charset=gbk");  
        response.setCharacterEncoding("gbk");  
        PrintWriter pw = response.getWriter();  
        pw.write(JSONUtils.valueToString(json));  
        pw.flush(); 
        } 
        return null; 
    } 
public void setLoginService(LoginService loginService) { 
    this.loginService = loginService; 

    public static void main(String[] args) { 
        String testStr =  
"{ \"employees\":[{\"user_id\": 841 , \"user_name\":\"EnidChen\"},{\"user_id\": \"1445\" , \"user_name\":\"JulieZhu\"},{\"user_id\": \"1447\" , \"user_name\":\"JasonMa\"}]}"; 
     
         
        System.out.println(testStr); 
        Map json = (Map) JSONSerializer.toJSON(testStr); 
        List employees = (List) json.get("employees"); 
        System.out.println(employees.size()); 
        Map json2 = (Map) JSONSerializer.toJSON(employees.get(0)); 
         
        System.out.println(json2.get("user_id")); 
        System.out.println(json2.get("user_name")); 
         
    } 

[java]
function load_allemployees(employee_type){ 
            var url = "onLoadEmployee.do"; 
            var p_data = "employee_type=" + employee_type; 
            doAjaxSubmit(url, p_data, filSelect); 
    } 
    function filSelect(data){ 
    $("#left_").empty(); 
    var employeeArr = document.getElementById('left_'); 
    for(var i=0;i<data.length;i++) 
    { 
               var op = document.createElement("OPTION"); 
                op.value = data[i].user_id;  
                op.innerHTML =data[i].user_name; 
               employeeArr.appendChild(op); 
  
    } 
    } 
    function doAjaxSubmit(p_url, p_data, p_callback) { 
        $.ajax({ 
            type:"POST", 
            url: p_url, 
            data: p_data, 
            dataType:"json", 
            success: function(data){ 
                p_callback(data); 
            } 
        }); 
    } 
     
摘自 xinghui_liu的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。