jquery+json+struts2的ajax功能實現總結,折磨我3天希望可以幫助到你

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="jquery-1[1].2.1.pack.js"></script>
<script type="text/javascript">
function select(x) {
var selectname = "table1";
var urlvalue = "table1_select";
if(x==1){
urlvalue = urlvalue + "?selectid="+$("#table1 option:selected").get(0).value;
selectname = "table2";
}else{

}
$.ajax({
url : urlvalue,
type : 'post',
dataType : 'json',
contentType : 'application/json:charset=utf-8',
cache : false,
success : function(data) {
var obj = data;
document.getElementById(selectname).options.length = 0;
$.each(obj, function(key, val) {
var op = new Option(obj[key].name, obj[key].id);
document.getElementById(selectname).options.add(op);
});
}
});
}
window.οnlοad=select;
</script>
</head>

<body>
之所以用jquery因爲有時候不想刷新頁面!!!<br>
<input type="text" value="讓我崩潰3天的json經過王總的:" size="40">
<select id="table1" οnchange="select(1);"></select>
<input type="text" value="讓我:" size="5">
<select id="table2" οnchange="select(2);"></select>
</body>
</html>

-------------------------上面是頁面部分,下面是action部分---------------------------------

package com.jquerytojson.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.PropertyFilter;

import com.jquerytojson.entity.Table1;
import com.jquerytojson.entity.Table2;
import com.jquerytojson.services.Table1Servcie;
import com.jquerytojson.services.Table2Service;
import com.opensymphony.xwork2.ActionSupport;

public class Table1Action extends ActionSupport {
private List<?> list;
private Table1Servcie table1Servcie;
private Table2Service table2Service;


public void select() throws UnsupportedEncodingException {
list = table1Servcie.getList();

HttpServletRequest request = ServletActionContext.getRequest();
request.setCharacterEncoding("UTF-8");
String seledid = new String();
//這裏如果你從jsp傳來的如果是中文的話...你明白,應爲不能json簡單迭代出級聯表,只能再查一遍了
if(request.getParameter("selectid")!=null){
seledid=new String(request.getParameter("selectid").getBytes("ISO-8859-1"));
System.out.println(seledid);
list = table2Service.getlList(seledid);
System.out.println(list.size());
}
//設置json配置,防止實體類中級聯的死循環,擦json對級聯的操作那麼不方便T_T
JsonConfig cfg = new JsonConfig();
cfg.setJsonPropertyFilter(new PropertyFilter()
{
public boolean apply(Object source, String name, Object value) {
//table2s實體類中set名字
if(name.equals("table2s")||name.equals("table1")) {
return true;
} else {
return false;
}
}
});
//轉成json數組,需要jar包支持
JSONArray jsonArray = JSONArray.fromObject(list,cfg);
//用servlet往頁面返回
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
out.print(jsonArray);
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.close();
}
}

public Table1Servcie getTable1Servcie() {
return table1Servcie;
}

public void setTable1Servcie(Table1Servcie table1Servcie) {
this.table1Servcie = table1Servcie;
}

public List<?> getList() {
return list;
}

public void setList(List<?> list) {
this.list = list;
}
public Table1 table1;



public Table1 getTable1() {
return table1;
}

public void setTable1(Table1 table1) {
this.table1 = table1;
}

public Table2Service getTable2Service() {
return table2Service;
}

public void setTable2Service(Table2Service table2Service) {
this.table2Service = table2Service;
}

}
-----------------------------------------------------------------

相應的json包啊,jquery.js啊你都導進去,struts.xml不用配置正常寫就行,action中沒有返回

<package name="default" extends="struts-default">
<action name="table1_*" class="Table1Action" method="{1}">
</action>
</package>


發佈了22 篇原創文章 · 獲贊 9 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章