oracle+servlet+extjs4 分页表格布局示例代码

Log.java

 package com.example.entity;

 import java.util.Date;

 public class Log {

     private int id;
private Date timestamp;
private String module;
private String function;
private String method;
private String userId;
private String userName;
private String localIp;
private String remoteIp;
private int type;
private String fileName;
private int fileSize;
private String params;
private int success;
private String error;
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getFileSize() {
return fileSize;
}
public void setFileSize(int fileSize) {
this.fileSize = fileSize;
}
public String getFunction() {
return function;
}
public void setFunction(String function) {
this.function = function;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLocalIp() {
return localIp;
}
public void setLocalIp(String localIp) {
this.localIp = localIp;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
public String getParams() {
return params;
}
public void setParams(String params) {
this.params = params;
}
public String getRemoteIp() {
return remoteIp;
}
public void setRemoteIp(String remoteIp) {
this.remoteIp = remoteIp;
}
public int getSuccess() {
return success;
}
public void setSuccess(int success) {
this.success = success;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
} }

LogDAO.java

 package com.example.DAO;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import com.example.entity.Log; public class LogDAO { private static Connection getConn() {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.97.201:1521:fznop";
String username = "bi_swxt";
String password = "swxt2013";
Connection conn = null;
try {
Class.forName(driver);
// new oracle.jdbc.driver.OracleDriver();
conn = DriverManager.getConnection(url, username, password);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
} return conn;
} public static List<Log> findAll(int start, int limit) {
int s = start +1;
int e = start + limit;
Connection conn = getConn();
String sql = "select id,timestamp,module,function,method,user_id,user_name,local_ip," +
"remote_ip,type,file_name,file_size,params,success,error from (select r.*, rownum as d " +
"from (select * from system_log) r where rownum <= " +e + ") where d >= " +s;
PreparedStatement pstmt;
List<Log> list = new ArrayList<Log>(); try {
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Log log = new Log();
log.setId(rs.getInt(1));
log.setTimestamp(rs.getDate(2));
log.setModule(rs.getString(3));
log.setFunction(rs.getString(4));
log.setMethod(rs.getString(5));
log.setUserId(rs.getString(6));
log.setUserName(rs.getString(7));
log.setLocalIp(rs.getString(8));
log.setRemoteIp(rs.getString(9));
log.setType(rs.getInt(10));
log.setFileName(rs.getString(11));
log.setFileSize(rs.getInt(12));
log.setParams(rs.getString(13));
log.setSuccess(rs.getInt(14));
log.setError(rs.getString(15));
list.add(log);
}
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
return list;
} public static int getTotal(){
Connection conn = getConn();
String sql = " select count(*) from system_log";
PreparedStatement pstmt;
int total = 0;
try {
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
total= rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return total;
}
}

LogServlet.java

 package com.example.servlet;

 import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.example.DAO.LogDAO;
import com.example.entity.Log; public class LogServlet extends HttpServlet { private static final long serialVersionUID = -4798851845756886622L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int start = Integer.parseInt(req.getParameter("start"));
int limit = Integer.parseInt(req.getParameter("limit"));
LogDAO logDAO = new LogDAO();
List<Log> list = logDAO.findAll(start , limit);
int total = logDAO.getTotal();
StringBuilder sb = new StringBuilder();
sb.append("{results: " + total + ", rows:[");
for (Log log : list) {
sb.append("{");
sb.append("ID : " + log.getId());
sb.append(", TIMESTAMP : \'" + log.getTimestamp() + "\'");
sb.append(", MODULE : \'" + log.getModule() + "\'");
sb.append(", FUNCTION : \'" + log.getFunction() + "\'");
sb.append(", METHOD : \'" + log.getMethod() + "\'");
sb.append(", USER_ID : \'" + log.getUserId() + "\'");
sb.append(", USER_NAME : \'" + log.getUserName() + "\'");
sb.append(", LOCAL_IP : \'" + log.getLocalIp() + "\'");
sb.append(", REMOTE_IP : \'" + log.getRemoteIp() + "\'");
sb.append(", TYPE : " + log.getType());
sb.append(", FILE_NAME : \'" + log.getFileName() + "\'");
sb.append(", FILE_SIZE : " + log.getFileSize());
sb.append(", PARAMS : \'" + log.getParams() + "\'");
sb.append(", SUCCESS : " + log.getSuccess());
sb.append(", ERROR : \'" + log.getError() + "\'");
sb.append("},");
} String json = sb.substring(0, sb.length() - 1); json += "]}";
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
out.println(json);
out.close();
} }

grid.js

 Ext.onReady(function(){
var itemsPerPage = 10; var store = Ext.create('Ext.data.Store',{
autoLoad: {start: 0, limit: itemsPerPage},
fields: ['ID','TIMESTAMP','MODULE','FUNCTION','METHOD','USER_ID','USER_NAME'
,'LOCAL_IP','REMOTE_IP','TYPE','FILE_NAME','FILE_SIZE','PARAMS','SUCCESS','ERROR'],//定义字段
pageSize: itemsPerPage,
proxy: {
type: 'ajax',
url: 'log.do',
reader: {
type: 'json',
root: 'rows',
totalProperty: 'results'
}
}
});
//创建Grid表格组件
Ext.create('Ext.grid.Panel',{
title : 'Grid表格示例',
renderTo: Ext.getBody(),
width:1300,
height:330,
frame:true,
store: store, columns: [//配置表格列
{header: "ID", width: 80, dataIndex: 'ID', sortable: true},
{header: "TIMESTAMP", width: 80, dataIndex: 'TIMESTAMP', sortable: true},
{header: "MODULE", width: 80, dataIndex: 'MODULE', sortable: true},
{header: "FUNCTION", width: 80, dataIndex: 'FUNCTION', sortable: true},
{header: "METHOD", width: 80, dataIndex: 'METHOD', sortable: true},
{header: "USER_ID", width: 80, dataIndex: 'USER_ID', sortable: true},
{header: "USER_NAME", width: 80, dataIndex: 'USER_NAME', sortable: true},
{header: "LOCAL_IP", width: 80, dataIndex: 'LOCAL_IP', sortable: true},
{header: "REMOTE_IP", width: 80, dataIndex: 'REMOTE_IP', sortable: true},
{header: "TYPE", width: 80, dataIndex: 'TYPE', sortable: true},
{header: "FILE_NAME", width: 80, dataIndex: 'FILE_NAME', sortable: true},
{header: "FILE_SIZE", width: 80, dataIndex: 'FILE_SIZE', sortable: true},
{header: "PARAMS", width: 80, dataIndex: 'PARAMS', sortable: true},
{header: "SUCCESS", width: 80, dataIndex: 'SUCCESS', sortable: true},
{header: "ERROR", width: 80, dataIndex: 'ERROR', sortable: true}
],
bbar:[{
xtype: 'pagingtoolbar',
store: store,
displayInfo: true,
displayMsg: '显示第 {0} 条到 {1} 条记录,一共 {2} 条',
emptyMsg: "当前查询条件无数据,请重新查询"
}]
});
});

index.jsp

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MyTestWeb</title>
<link type="text/css" rel="stylesheet" href="resource/common/extjs/resources/css/ext-all-neptune.css" />
</head>
<body >
<script type="text/javascript" src="resource/common/extjs/ext-all.js"></script>
<script type="text/javascript" src="resource/common/extjs/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="resource/common/extjs/packages/ext-theme-neptune/build/ext-theme-neptune.js"></script>
<script type="text/javascript" src="resource/grid/grid.js"></script>
<%--<div >
</div>
--%></body>
</html>

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet>
<servlet-name>Log</servlet-name>
<servlet-class>
com.example.servlet.LogServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Log</servlet-name>
<url-pattern>/log.do</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
上一篇:Struts2基础学习(七)—值栈和OGNL


下一篇:linux几种时间函数总结