Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.action; import com.google.gson.Gson; import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.List; import net.sf.json.JSONArray; import org.apache.struts2.ServletActionContext; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; /** * * @author yishan_zhuang */ @Repository("jsonAction") @Scope("prototype") @Transactional public class JsonAction { @Autowired protected SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } private String jq_table_code; private String iufo_table_code; public String getJq_table_code() { return jq_table_code; } public void setJq_table_code(String jq_table_code) { this.jq_table_code = jq_table_code; } public String getIufo_table_code() { return iufo_table_code; } public void setIufo_table_code(String iufo_table_code) { this.iufo_table_code = iufo_table_code; } public void getDuizhaoJson() throws Exception { String sql = ""; if (jq_table_code != null && !("".equals(jq_table_code))) { sql = "select table_name,col_name,iufoTbName,iufoColName from jq_zhibiao_table where table_code='" + jq_table_code + "'"; } else { sql = "select table_name,col_name,iufoTbName,iufoColName from jq_zhibiao_table"; } Session session = sessionFactory.getCurrentSession(); List<Object[]> list = session.createSQLQuery(sql).list(); Map map = null; ArrayList rsList = new ArrayList(); for (Object[] object : list) { map = new HashMap(); map.put("table_name", (String) object[0]); map.put("col_name", (String) object[1]); map.put("iufoTbName", (String) object[2]); map.put("iufoColName", (String) object[3]); rsList.add(map); } JSONArray jo = JSONArray.fromObject(rsList); String json = jo.toString(); ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8"); ServletActionContext.getResponse().getWriter().print(json); } public void iufoGetDropdownData() throws IOException { Session session = sessionFactory.getCurrentSession(); String sql = ""; Map map = null; ArrayList rsList = new ArrayList(); String json = ""; if ("0".equals(iufo_table_code)) { sql = "select distinct(table_code),table_name from iufo_zhibiao_table"; List<Object[]> list = session.createSQLQuery(sql).list(); rsList = new ArrayList(); for (Object[] object : list) { map = new HashMap(); map.put("table_code", (String) object[0]); map.put("table_name", (String) object[1]); rsList.add(map); } System.out.println("count:" + rsList.size()); JSONArray jo = JSONArray.fromObject(rsList); json = jo.toString(); } else { sql = "select * from iufo_zhibiao_table where table_code='" + iufo_table_code + "'"; List<Object[]> list = session.createSQLQuery(sql).list(); rsList = new ArrayList(); for (Object[] object : list) { map = new HashMap(); map.put("id", (BigDecimal) object[0]); map.put("table_code", (String) object[1]); map.put("table_name", (String) object[2]); map.put("col_code", (String) object[3]); map.put("col_name", (String) object[4]); map.put("col_index", (BigDecimal) object[5]); map.put("col_type", (String) object[6]); rsList.add(map); } JSONArray jo = JSONArray.fromObject(rsList); json = jo.toString(); } ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8"); ServletActionContext.getResponse().getWriter().print(json); } public void jqGetDropdownData() throws IOException { if (jq_table_code == null || "".equals(jq_table_code)) { jq_table_code = "0"; } Session session = sessionFactory.getCurrentSession(); String sql = ""; Map map = null; ArrayList rsList = null; String json = ""; if ("0".equals(jq_table_code)) { sql = "select distinct(table_code),table_name from jq_zhibiao_table"; List<Object[]> list = session.createSQLQuery(sql).list(); rsList = new ArrayList(); for (Object[] object : list) { map = new HashMap(); map.put("table_code", (String) object[0]); map.put("table_name", (String) object[1]); rsList.add(map); } Gson gson = new Gson(); json = gson.toJson(rsList); System.out.println("json=" + json); } else { sql = "select * from jq_zhibiao_table where table_code='" + jq_table_code + "'"; List<Object[]> list = session.createSQLQuery(sql).list(); rsList = new ArrayList(); for (Object[] object : list) { map = new HashMap(); map.put("id", (BigDecimal) object[0]); map.put("table_code", (String) object[1]); map.put("table_name", (String) object[2]); map.put("col_code", (String) object[3]); map.put("col_name", (String) object[4]); map.put("col_index", (BigDecimal) object[5]); map.put("col_type", (String) object[6]); rsList.add(map); } Gson gson = new Gson(); json = gson.toJson(rsList); } ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8"); ServletActionContext.getResponse().getWriter().print(json); } }