在Jsp中,每次要用表格渲染的时候,如果用系统默认的表格就像下图一样比较丑,如果你要自己渲染的话,就比较麻烦。所以我写了Java 的用Layui渲染表格的方法。
要使用layui渲染表格的方法,要知道后台的传入参数和传出参数,layui官网说的很清楚,我就不过多赘述了,传入的是代表当前页码(page)和代表每页数据量(limit),传出的参数是总行数(count)、数据(data)、数据状态码----可以不设置(code)、状态信息码 —可以不设置(mag)
传入参数:
传出参数:
然后再准备工具包:Gson
Gson提供了fromJson() 和toJson() 两个直接用于解析和生成的方法,前者实现反序列化,后者实现了序列化;同时每个方法都提供了重载方法
然后上代码
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Enumeration;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.gx.dao.IComdao;
import com.gx.dao.impl.ComdaoImpl;
import com.gx.utils.DbUtils;
public JsonObject SelectSenior(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取传入参数
int page= Integer.parseInt(request.getParameter("page"));
int limit=Integer.parseInt(request.getParameter("limit"));
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html ;charset=UTF-8");
//Enumeration enu = request.getParameterNames();
//检查传入参数
//while (enu.hasMoreElements()) {
//String paraName = (String) enu.nextElement();
//System.out.println(paraName + ": " + request.getParameter(paraName));
//}
JsonObject json = new JsonObject();
try {
String strmysql = "select tbsenior.seniorID,tbsenior.`name`,tbsenior.gender,tbsenior.birthday,tbinhospital.checkInmark, "
+ " tbinhospital.dateofagreement,tbinhospital.endtime,tbsenior.idcardno,tbinhospital.inSite, "
+ " tbCaregrade.caregrade,tbYear.`year`,tbPocketbook.pocketbook"
+ " from b_inhospital tbinhospital join s_senior tbsenior on tbinhospital.seniorID = tbsenior.seniorID "
+ " join s_year tbYear on tbinhospital.YearID = tbYear.YearID "
+ " join S_Pocketbook tbPocketbook on tbinhospital.PocketbookID = tbPocketbook.PocketbookID "
+ " join S_Caregrade tbCaregrade on tbinhospital.CaregradeID =tbCaregrade.CaregradeID ";
String strNum = request.getParameter("strNum");
String strCode = request.getParameter("strCode");
Object[] Obj = null;
/// 筛选查询结果
if (!"".equals(strNum) && !"".equals(strCode)) {
strmysql = strmysql + " WHERE tbsenior.`name` LIKE '%?%' && tbsenior.checkInmark LIKE '%?%' ORDER BY tbsenior.NationID; ";
Obj = new Object[2]; //开辟一个长度为2的数组
System.out.println("Obj="+Obj.length);
Obj[0]=strNum;
Obj[1]=strCode;
}
else if (!"".equals(strNum) ) {
strmysql = strmysql + " WHERE tbsenior.`name` LIKE ? ORDER BY tbsenior.NationID; ";
Obj = new Object[1]; //开辟一个长度为1的数组
Obj[0]=strNum;
}else if (!"".equals(strCode) ){
strmysql = strmysql + " WHERE tbsenior.checkInmark LIKE ? ORDER BY tbsenior.NationID; ";
Obj = new Object[1]; //开辟一个长度为1的数组
Obj[1]=strCode;
}
else {
strmysql = strmysql + "ORDER BY tbsenior.NationID; ";
}
IComdao comdao = new ComdaoImpl();
rs = comdao.find(strmysql, Obj); //mysql查询的结果
conn = comdao.getConn();
ps = comdao.getPs();
Gson gson = new Gson();
HashMap hm = new HashMap();
JsonArray jsonarray = new JsonArray();
json.addProperty("code", 0);
json.addProperty("msg", "");
int len = 0; // 总条数
while (rs.next()) {
len++;
//分页,切割
if ((len > (page-1) * limit) && (len < (page) * limit)) {
// 得到结果集(rs)的结构信息,比如字段数、字段名等。
ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
// 返回此 ResultSet对象中的列数。
int icolnum = rsmd.getColumnCount();
for (int i = 1; i <= icolnum; i++) {
// 转换为json 类型
hm.put(rsmd.getColumnName(i).toString(), rs.getString(i));
}
jsonarray.add(gson.toJsonTree(hm));
}
}
json.add("data", jsonarray); //
json.addProperty("count", len);
jsonarray.add(gson.toJsonTree(hm));
System.out.println(jsonarray);
json.add("data", jsonarray);
System.out.println(json);
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtils.close(conn, ps, rs);
}
return json;
}
最后看效果图:
本文地址:https://blog.csdn.net/weixin_44550157/article/details/107895404
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!