一、ajax傳遞單個或多個元素到action
function unique(val){
var errorMsg = document.getElementById("tasknameid") ;
if(val == null || val == ""){
errorMsg.innerHTML = "<font color=\"red\"> 請輸入任務名!</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\"> " + 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