ajax与action进行数据交互

一、ajax传递单个或多个元素到action

    function unique(val){
    var errorMsg = document.getElementById("tasknameid") ;
    if(val == null || val == ""){
        errorMsg.innerHTML = "<font color=\"red\">&nbsp;&nbsp;请输入任务名!</font>";
    }else{
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/manage/task!unique.action",
            data:{
                taskname:val                     //此处可以传递单个或者多个元素,左边为bean类的属性taskname,右边为传递的值val
            },
            dataType:"json",
            success:function (data){
                //alert(data) ;
                if(data != null){
                    errorMsg.innerHTML = "<font color=\"red\">&nbsp;&nbsp;" + data + "</font>";    
                }else{
                    errorMsg.innerHTML = " " ;
                }
            }
        }) ;
    }
}

action接收时直接通过e.getTaskname() 接收即可【e为基类】

二、ajax传递对象到action

function insertTemplet(){
    var taskname = document.getElementById("taskname").value ;
    var taskType = document.getElementById("taskType").value ;
    var termList = document.getElementById("termList").value ;
    var testInfo = document.getElementById("testInfoAjax").value ;
    var compilerid = document.getElementById("compilerid").value ;
    var tasktempl = {"tasktempl.taskname":taskname, "tasktempl.taskType":taskType, "tasktempl.termList":termList, "tasktempl.testInfo":testInfo, "tasktempl.compilerid":compilerid};
    $.ajax({
        type:"post",
        url:"${pageContext.request.contextPath}/manage/task!insertTemplet.action",
        data:tasktempl,                                           //传递定义好的taskTempl对象到action中
        dataType:"json",
        success:function (data){
            if(data != null){
                alert(data) ;
            }else{
                alert("添加模板失败") ;
            }
        }
    }) ;    
}

action接收ajax传递的tasktempl 对象:

private TaskTemplet tasktempl = new TaskTemplet() ;   //用于taskAdd.jsp页面中json传递参数

public TaskTemplet getTasktempl() {
        return tasktempl;
    }

    public void setTasktempl(TaskTemplet tasktempl) {
        this.tasktempl = tasktempl;
    }

这样就可以直接接收了

遗留问题:此处无法通过

data:{
                tasktempl:tasktempl
            },

的形式传递对象

三、action传递对象到ajax

action方法:

    public String executeCompiler() throws Exception{
        Map map = exeCompiler() ;
        String moduleCase = (String)map.get("moduleCase") ;
        String jarDexFile = (String)map.get("jarDexFile") ;
        
        dataMap.put("errorMsg", errorMsg) ;    
        dataMap.put("jsonString", moduleCase) ;
        dataMap.put("jarDexFile", jarDexFile) ;                         //传递map到js页面中

        
        return "executeCompiler" ;

struts配置:    }

            <result name="executeCompiler" type="json">
                <param name="root">dataMap</param>
            </result>

js接收:通过data.属性的方式接收

$.ajax({         //在编译器列表文本框处进行编译
                    type:"post",
                    url:"${pageContext.request.contextPath}/manage/task!executeCompiler.action",
                    data:{
                        compilername:compilerName
                    },
                    dataType:"json",
                    success:function (data){
                        if(data.errorMsg != null){
                            alert(data.errorMsg) ;
                        }else{
                            document.getElementById("filepathid").value=data.jarDexFile ;
                            var newWin = window.showModalDialog("${pageContext.request.contextPath}/manage/task!toTestInfo.action?jsonString="+data.jsonString,window," center:yes") ;
                            
                            if(newWin!="[object]" || newWin != null){
                                document.getElementById("testInfoAjax").value=newWin;
                                var testInfo = document.getElementById("testInfoAjax").value;
                                if(testInfo == "undefined" || testInfo == null || testInfo == ""){
                                    alert("请选择【测试case】!") ;
                                }
                            }
                        }
                    }
                }) ;

如果要传递单个数据,则struts配置如下:

            <result name="unique" type="json">
                <param name="root">errorMsg</param>
            </result>

js接收时仍通过data接收,其实就是errorMsg








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章