1.向前台傳遞數據;
2.向后台傳遞數據;
3.ajax post 提交數據到服務端時中文亂碼解決方法;
4.數組類型參數傳遞;
1.向前台傳遞數據:
1.1 字符串數據傳遞:
這種方式只是單一的向前台傳遞字符串(比如傳遞ajax 請求某個數據的結果),通過 response 實現;
1.1.1 Action 類:
1 public String getResult(){ 2 HttpServletResponse response=ServletActionContext.getResponse(); 3 response.setContentType("text/html;charset=GBK");//解決中文亂碼 4 PrintStream out=null; //流 5 try { 6 out = new PrintStream(response.getOutputStream()); 7 out.print("向前台傳遞一個字符串結果"); 8 } catch (IOException e) { 9 // TODO Auto-generated catch block 10 e.printStackTrace(); 11 }finally{ 12 out.flush(); 13 out.close(); 14 } 15 return null; //最后返回null 16 }
1.1.2 struts 配置文件:
1 <action name="testAction" class="testAction" method="getResult"> 2 <result name="success"></result> <!-- 這里不用填寫什么 --> 3 </action>
1.2 對象數據傳輸:
對象數據通常是已json 格式傳輸,在 struts2 配置文件內引入 json-default(普通json 格式) 包或者 jackson-json-default(加強型json格式,在返回的json數據中包含對象類型,類似這樣的結果("_javaType_":"com.action.TestAction");可以根據業務情況選用,隨着業務系統的龐大,我一般用javascript 在前台綁定數據,這樣當涉及到判斷數據類型時就可以采用這個字段的值來處理:
1.2.1 Action 類:
1 List<Student> lsStudent; //lsStudent 屬性要有get\set方法 2 /** 3 * 根據班級ID 獲得該班級的學生信息 4 * return 班級所有學生信息 5 */ 6 public String getStudentByClassId(){ 7 8 HttpRequest request=ServletActionContext.getRequest(); 9 String classId=request.getParameter("classId"); //班級ID 10 11 /* 12 *這里的判斷很容易出錯,如果你用(id!=null)做判斷條件,當沒有獲得值時,id 是一個空的String對象,空對象不能做判斷,這就好像你在Java類中這樣寫代碼: String tb; 13 if(tb==null||tb==""){// 錯誤:The local variable tb may not have been initialized 14 System.out.println(tb); 15 } 16 */ 17 if(null!=id ||!"".equals(id)){ 18 lsStudent=TestStudent.getStudentById(id); 19 } 20 21 return SUCCESS; 22 }
1.2.1 struts 配置文件:
1 <action name="testAction" class="testAction" method="getResult"> 2 <result name="success" type="strongtype-json"> 3 <param name="root"> 4 lsStudent <!-- 這里返回action 中要返回的對象(一定要有get/set方法) --> 5 </param> 6 </result> 7 </action>
最終前台會得到這樣的json數據:
1 [{"__javaType__":"com.base.Student","name":"張三","age":"10","homeAddr":null,"stuNum":0,"classNum":0},{"__javaType__":"com.base.Student","name":"李四","age":"20","homeAddr":null,"stuNum":0,"classNum":0}]
1.2.3 前台js獲得json數據:
1 $.ajax({ 2 type:"post", 3 url:"testAction.action", 4 data:{ 5 classId:classId 6 }, 7 success:function(rs){ 8 var studentArray=[]; //前台創建個數組對象保存數據 9 $.each(rs,function(i,item){ 10 var student; 11 student.name=item.name; 12 student.id=item.id; 13 student.age=item.age; 14 studentArray.push(student); //將student 對象信息保存到數組對象中 15 }); 16 }, 17 error:function(){ 18 alert("獲取數據時發生錯誤"); 19 } 20 });
3.ajax post 提交數據到服務端時中文亂碼解決方法:
get 方式提交數據到服務端不會亂碼,但對數據量有限制;post 可以提交大數據量,但中文會發生亂碼,解決方法:
在JS上用使用 encodeURIComponent 對字符編碼處理:
1 studentRuselt=encodeURIComponent(JSON.stringify(result),"utf-8"); //這里用了json2 來將對象轉換為json格式,然后在用encodeURIComponent來設置編碼; 2 3 $.ajax({ 4 type:"post", 5 url:"saveExamQuestionAnswer.action", 6 cache:true, 7 async:true, //這里指定值時不能加雙引號(會設置無效) 8 contentType: "application/x-www-form-urlencoded; charset=utf-8", 9 data: { 10 studentRuselt: studentRuselt 11 } 12 )};
Action類上用java.net.URLDecoder.URLDecoder.decode方法轉碼:
studentRuselt=URLDecoder.decode(studentRuselt,"UTF-8");
這樣得到的中文不會亂碼,還有另外一個js組件:encodeURI也可以對字符進行處理,提交時它會使用jquery默認編碼提交數據,但使用encodeURIComponent 組件指定編碼,細節清晰,前台后台處理編碼一致這樣比較穩妥;
4.數組類型參數傳遞:
若一個請求中包含多個值,如:(test.action?tid=1&tid=2&tid=3),參數都是同一個,只是指定多個值,這樣請求時后台會發生解析錯誤,應先使用 tradititonal 格式化:
1 $.ajax({ 2 type:"post", 3 url:"test.action", 4 data:{ 5 tid:[1,2,3] 6 }, 7 traditional:true 8 9 });