昨天学校放假,所以偷懒了一天没有更新我的博客。
今天我将向大家讲述JQuery的Ajax,废话不多说。
上AJAX之前我们提及了JSon ,但那是前台json,后台又是怎么的呢?
前提 因为数据库中所存储的数据和后台json的输出表达一个样的,因此我们可以将建实体类用json给替换掉。
第一步
我们需要一个工具包,去将后台实例对象给转成json,
jackson-annotations-2.2.3.jar
jackson-core-2.2.3.jar
jackson-databind-2.2.3.jar
第二步
new 一个ObjectMapper;
不能双向赋值,一定要改为单向,
或者用@JsonIgnore被标识的属性不参与转换为String[];
不然会造成栈溢出。
1:
Student stu1 = new Student("s01","zhangsan");
Student stu2 = new Student("s02","lisi");
Teacher tea1 = new Teacher("t01","xiaoge",null);
Teacher tea2 = new Teacher("t02","yuan",null);
Set<Teacher> teas = new HashSet<>();
teas.add(tea1);
teas.add(tea2);
stu1.setTeas(teas );
Set<Student> stus = new HashSet<>();
stus.add(stu1);
tea1.setStus(stus);
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(stu1));
2:
Map<String, Object> map = new HashMap<>();
map.put("totls",2);
map.put("stu", stu1);
ObjectMapper om = new ObjectMapper();
String str = om.writeValueAsString(map);
System.out.println(str);
因此不仅仅是实体对象可以转成json,
Map集合也是可以的;
接下来就是怎么去从数据库中取值给json
这个是dao方法中的代码
public List<Map<String, Object>> list(Map<String, String[]> map,PageBean pageBean) throws Exception{
String id=JsonUtil.getprepmeval(map, "ID");
String region_parent_id=JsonUtil.getprepmeval(map, "REGION_PARENT_ID");
String sql="select * from ch_region where true";
if(StringUtils.isBlank(region_parent_id)) {
if(StringUtils.isBlank(id)) {
sql+=" and parent_id=7459";
}else {
sql+=" and parent_id="+id;
}
}else {
sql+=" and region_parent_id="+region_parent_id;
}
return super.executeQuery(sql, pageBean);
}
其中JsonUtil是下面代码
这个方法是将原表里面的数据赋值给json
public static List<Map<String, Object>> formatRsToJsonArray(ResultSet rs) throws SQLException{
ResultSetMetaData md = rs.getMetaData();//原表数据
int count = md.getColumnCount();//有多少列名
List<Map<String,Object>> list = new ArrayList<>();//可以将Map集合看成原本的objct对象
Map<String,Object> map = null;
while(rs.next()) {
map= new HashMap<>();
for(int i=1;i<=count;i++) {
map.put(md.getColumnName(i),rs.getObject(i));//md.getColumnName(i)是对应列名,rs.getObject(i)在rs中取到对应的值
}
list.add(map);
}
return list;
}
这个是方便Dao中到json里面属性的值。
public static String getprepmeval(Map<String, String[]> map,String key) {
if(map!=null && map.size()>0) {
String[] vals=map.get(key);
if(vals!=null && vals.length>0) {
String val = Arrays.toString(vals);
val=val.substring(1,val.length()-1);
System.out.println(val);
return val;
}
}
return "";
}
到这里我们就能把数据库的数据给添到json里了。
得到的json对象就可以将他写到AJAX里面去了。
当然你需要一个servlet将其写进Jquery插件中去
private RegionDao dao=new RegionDao();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
List<Map<String, Object>> list = dao.list(req.getParameterMap(), null);
ObjectMapper om = new ObjectMapper();
ResponseUtil.wrtie(resp, om.writeValueAsString(list));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
其中 ResponseUtil.wrtie()为以下代码:
public static void wrtie(HttpServletResponse resp,Object o) throws IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println(o.toString());
out.flush();
out.close();
}
写进ajax的JQeury插件的代码为:
$(function() {
var cm=$("#cm").val();
$.ajax({
url: cm+"/allselectSerlvet.do",//data数据来源地址/资源
success: function(data){
alert("hollo ajxa");//你想实现的功能;
},
dataType: "json" //设置类型
});
因此到这里我的这次学习心得就结束了。