一步一步做项目(17)日志处理

一步一步做项目(17)日志处理


在前面 一步一步做项目(11)扩展(管理课程信息)的基础上进行,前面的文章详细介绍了如何添加新功能,这里就不再赘述。
本讲只介绍用户日志的增删改查,当然,对于日志来说,通常是自动添加的,用户一般只能浏览和查看,不能手动添加、改变和删除,而且,日志信息量增长很快,数据量大,需要进行分页处理。而如何自动获取用户日志信息,将在下一讲介绍。

类图

要进行用户日志管理,涉及的类及其关系如下图所示:

类图

model

UserLog.java类的代码如下:

package cn.lut.curiezhang.model;

/**
 * 创建实体类UserLog,进行日志处理
 * @author curiezhang
 */
public class UserLog {
	// 日志id
	private String userLogId;
	// 日志名
	private String userLogName;
	// 用户id
	private String userId;
	// 用户名
	private String userName;
	// 用户ip
	private String userIp;
	// 日志时间
	private String logDate;
	// 访问url
	private String visitUrl;
	// 操作Action
	private String operateAction;
	// 操作方法 0浏览 1添加 2修改 3删除 4上传 5下载 6缴费 7打印准考证 8验证个人信息
	private String operateMethod;
	// 操作参数(浏览、添加、修改、删除、上传、下载、缴费、打印准考证、验证个人信息等)
	private String operateParameter;
	// 操作结果(0成功 1失败)
	private String operateResult;
	// 返回值
	private String returnValue;
	// 备注(暂时没用)
	private String memo;
	public String getUserLogId() {
		return userLogId;
	}
	public void setUserLogId(String userLogId) {
		this.userLogId = userLogId;
	}
	public String getUserLogName() {
		return userLogName;
	}
	public void setUserLogName(String userLogName) {
		this.userLogName = userLogName;
	}
	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;
	}
	public String getUserIp() {
		return userIp;
	}
	public void setUserIp(String userIp) {
		this.userIp = userIp;
	}
	public String getLogDate() {
		return logDate;
	}
	public void setLogDate(String logDate) {
		this.logDate = logDate;
	}
	public String getVisitUrl() {
		return visitUrl;
	}
	public void setVisitUrl(String visitUrl) {
		this.visitUrl = visitUrl;
	}
	public String getOperateAction() {
		return operateAction;
	}
	public void setOperateAction(String operateAction) {
		this.operateAction = operateAction;
	}
	public String getOperateMethod() {
		return operateMethod;
	}
	public void setOperateMethod(String operateMethod) {
		this.operateMethod = operateMethod;
	}
	public String getOperateParameter() {
		return operateParameter;
	}
	public void setOperateParameter(String operateParameter) {
		this.operateParameter = operateParameter;
	}
	public String getOperateResult() {
		return operateResult;
	}
	public void setOperateResult(String operateResult) {
		this.operateResult = operateResult;
	}
	public String getReturnValue() {
		return returnValue;
	}
	public void setReturnValue(String returnValue) {
		this.returnValue = returnValue;
	}
	public String getMemo() {
		return memo;
	}
	public void setMemo(String memo) {
		this.memo = memo;
	}
}

dao

对应的Dao类,代码如下:

package cn.lut.curiezhang.dao;

import java.util.Collection;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Query;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import cn.lut.curiezhang.model.UserLog;

/**
 * UserLog 管理的DAO的类
 * @author curiezhang
 *
 */
@SuppressWarnings("deprecation")
public class UserLogDao extends HibernateDaoSupport {
	private static final Logger log = LogManager.getLogger(UserLogDao.class);

	/**
	 * Dao中保存信息
	 * @param userLog
	 */
	public void save(UserLog userLog) {
		log.debug("cmis:DAO层 > 保存信息,id为 {}", userLog.getUserLogId());
		this.getHibernateTemplate().save(userLog);
	}

	/**
	 * Dao中查询所有行的方法
	 */
	@SuppressWarnings("unchecked")
	public Collection<UserLog> getAll() {
		log.debug("cmis:DAO层 > 查询所有信息");
    	Collection<UserLog> list;
		list = (Collection<UserLog>) this.getHibernateTemplate().find("from UserLog");
		return list;
	}

	/**
	 * 根据id删除
	 * @param userLogId
	 */
	public void delete(String userLogId) {
		log.debug("cmis:DAO层 > 删除信息,id为 {}", userLogId);
		Object model = this.getHibernateTemplate().get(UserLog.class, userLogId);
		this.getHibernateTemplate().delete(model);
	}

	/**
	 * 根据id查询
	 * @param userLogId
	 */
	public UserLog getUserLogById(String userLogId) {
		log.debug("cmis:DAO层 > 根据id查询信息,id为 {}", userLogId);
		return this.getHibernateTemplate().get(UserLog.class, userLogId);
	}

	/**
	 * 更新信息
	 * @param userLog
	 */
	public void update(UserLog userLog) {
		log.debug("cmis:DAO层 > 更新信息,id为 {}", userLog.getUserLogId());
		this.getHibernateTemplate().update(userLog);
	}

	/**
	 * 根据search查询
	 * @param search
	 */
	@SuppressWarnings("unchecked")
	public Collection<UserLog> listSearch(String search) {
		log.debug("cmis:DAO层 > 查询所有信息");
    	Collection<UserLog> list;
    	String hql = "from UserLog where userLogName like '%" + search + "%'" + " or userName like '%" + search + "%'"
    			 + " or userIp like '%" + search + "%'" + " or visitUrl like '%" + search + "%'"
    			  + " or logDate like '%" + search + "%'";
		list = (Collection<UserLog>) this.getHibernateTemplate().find(hql);
		return list;
	}

	/**
	 * 分页查询
	 * @param search
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Collection<UserLog> listPage(int page, int pageSize, String search) {
		log.debug("cmis:DAO层 > 分页查询,page={},pageSize={}", page, pageSize);
    	List<UserLog> list;
    	String hql = "from UserLog";
    	hql += " where userLogName like '%" + search + "%'" + " or userName like '%" + search + "%'"
   			 + " or userIp like '%" + search + "%'" + " or visitUrl like '%" + search + "%'"
   			  + " or logDate like '%" + search + "%'";
    	Query query = this.currentSession().createQuery(hql);  
        /*if (objects != null) {  
            for (int i = 0; i < objects.length; i++) {  
                query.setParameter(i, objects[i]);  
            }  
        } */
        query.setFirstResult((page - 1) * pageSize).setMaxResults(pageSize);  
        list = query.list();  
        return list;  
	}

	/**
	 * 得到行数
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	public int count() {  
        Query query = this.currentSession().createQuery("select count(*) from UserLog");  
        return ((Long)query.iterate().next()).intValue();  
    }

	/**
	 * 得到行数
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	public int countSearch(String search) {  
    	String hql = "select count(*) from UserLog";
    	hql += " where userLogName like '%" + search + "%'" + " or userName like '%" + search + "%'"
   			 + " or userIp like '%" + search + "%'" + " or visitUrl like '%" + search + "%'"
   			  + " or logDate like '%" + search + "%'";
		Query query = this.currentSession().createQuery(hql);  
        return ((Long)query.iterate().next()).intValue();  
    }

	/**
	 * 分页查询
	 * @param search
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Collection<UserLog> listPage(int page, int length, String search, String orderBy) {
		log.debug("cmis:DAO层 > 分页查询,page={},pageSize={}", page, length);
    	List<UserLog> list;
    	String hql = "from UserLog";
    	hql += " where userLogName like '%" + search + "%'" + " or userName like '%" + search + "%'"
   			 + " or userIp like '%" + search + "%'" + " or visitUrl like '%" + search + "%'"
   			  + " or logDate like '%" + search + "%' " + orderBy;
    	Query query = this.currentSession().createQuery(hql);  
        query.setFirstResult((page - 1) * length).setMaxResults(length);  
        list = query.list();  
        return list;  
	}

	/**
	 * 得到行数
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	public int count(String condition) {  
        Query query = this.currentSession().createQuery("select count(*) from UserLog where visitUrl like '%" + condition + "%'");  
        return ((Long)query.iterate().next()).intValue();  
    }

	/**
	 * 得到行数
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	public int countSearch(String search, String condition) {  
    	String hql = "select count(*) from UserLog";
    	hql += " where (userLogName like '%" + search + "%'" + " or userName like '%" + search + "%'"
   			 + " or userIp like '%" + search + "%'" + " or visitUrl like '%" + search + "%'"
   			  + " or logDate like '%" + search + "%') and visitUrl like '%" + condition + "%'";
		Query query = this.currentSession().createQuery(hql);  
        return ((Long)query.iterate().next()).intValue();  
    }

	/**
	 * 分页查询
	 * @param search
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Collection<UserLog> listPage(int page, int length, String search, String orderBy, String condition) {
		log.debug("cmis:DAO层 > 分页查询,page={},pageSize={}", page, length);
    	List<UserLog> list;
    	String hql = "from UserLog";
    	hql += " where (userLogName like '%" + search + "%'" + " or userName like '%" + search + "%'"
   			 + " or userIp like '%" + search + "%'" + " or visitUrl like '%" + search + "%'"
   			  + " or logDate like '%" + search + "%') and visitUrl like '%" + condition + "%' " + orderBy;
    	Query query = this.currentSession().createQuery(hql);  
        query.setFirstResult((page - 1) * length).setMaxResults(length);  
        list = query.list();  
        return list;  
	}

	/**
	 * 删除所有信息
	 */
	public void deleteAll() {
		String sql = "DELETE FROM UserLog";
		this.currentSession().createQuery(sql).executeUpdate();
	}
}

service

对应的Service类,代码如下:

package cn.lut.curiezhang.service;

import java.util.Collection;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import cn.lut.curiezhang.dao.UserLogDao;
import cn.lut.curiezhang.model.UserLog;

/**
 * UserLog 管理的业务层的类
 * @author curiezhang
 *
 */
@Transactional
public class UserLogService {
	private static final Logger log = LogManager.getLogger(UserLogService.class);
	/**
	 * 业务层注入DAO的类
	 */
	private UserLogDao userLogDao;
	public void setUserLogDao(UserLogDao userLogDao) {
		log.debug("cmis:Service层 > 注入DAO类");
		this.userLogDao = userLogDao;
	}

	/**
	 * 业务层保存信息的方法
	 * @param userLog
	 */
	public void save(UserLog userLog) {
		log.debug("cmis:Service层 > 保存信息,id为 {}", userLog.getUserLogId());
		userLogDao.save(userLog);
	}

	/**
	 * 业务层查询所有信息的方法
	 */
	private Collection<UserLog> list;
	public Collection<UserLog> getAll() {
		log.debug("cmis:Service层 > 查询所有信息");
		list = userLogDao.getAll();
		return list;
	}

	/**
	 * 业务层删除指定id信息的方法
	 * @param userLogId
	 */
	public void delete(String userLogId) {
		log.debug("cmis:Service层 > 根据id删除信息,id为 {}", userLogId);
		userLogDao.delete(userLogId);
	}

	/**
	 * 业务层使用id查询信息
	 * @param userLogId
	 */
	public UserLog getUserLogById(String userLogId) {
		log.debug("cmis:Service层 > 根据id查询信息,id为 {}", userLogId);
		return userLogDao.getUserLogById(userLogId);
	}

	/**
	 * 业务层更新信息的方法
	 * @param userLog
	 */
	public void update(UserLog userLog) {
		log.debug("cmis:Service层 > 更新信息,id为 {}", userLog.getUserLogId());
		userLogDao.update(userLog);
	}

	/**
	 * 业务层搜索
	 * @param search
	 */
	public Collection<UserLog> listSearch(String search) {
		log.debug("cmis:Service层 > 根据search查询信息,search为 {}", search);
		return userLogDao.listSearch(search);
	}

	/**
	 * 业务层分页
	 * @param search
	 */
	public Collection<UserLog> listPage(int page, int pageSize) {
		log.debug("cmis:Service层 > 分页,page={},pageSize={}", page, pageSize);
		return null;
	}
	/**
	 * 业务层分页
	 * @param search
	 */
	public Collection<UserLog> listPage(int page, int pageSize, String search) {
		log.debug("cmis:Service层 > 分页,page={},pageSize={}, search={}", page, pageSize, search);
		return userLogDao.listPage(page, pageSize, search);
	}

	/**
	 * 业务层统计
	 */
	public int count() {
		log.debug("cmis:Service层 > 得到行数");
		return userLogDao.count();
	}

	/**
	 * 业务层统计
	 */
	public int countSearch(String search) {
		log.debug("cmis:Service层 > 得到行数");
		return userLogDao.countSearch(search);
	}

	/**
	 * 业务层分页
	 * @param search
	 */
	public Collection<UserLog> listPage(int page, int length, String search, String orderBy) {
		log.debug("cmis:Service层 > 分页,page={},pageSize={}, search={}", page, length, search);
		return userLogDao.listPage(page, length, search, orderBy);
	}

	/**
	 * 业务层统计
	 */
	public int count(String condition) {
		log.debug("cmis:Service层 > 得到行数");
		return userLogDao.count(condition);
	}

	/**
	 * 业务层统计
	 */
	public int countSearch(String search, String condition) {
		log.debug("cmis:Service层 > 得到行数");
		return userLogDao.countSearch(search, condition);
	}

	/**
	 * 业务层分页
	 * @param search
	 */
	public Collection<UserLog> listPage(int page, int length, String search, String orderBy, String condition) {
		log.debug("cmis:Service层 > 分页,page={},pageSize={}, search={}", page, length, search);
		return userLogDao.listPage(page, length, search, orderBy, condition);
	}

	/**
	 * 业务层删除所有信息的方法
	 */
	public void deleteAll() {
		log.debug("cmis:Service层 > 删除所有信息");
		userLogDao.deleteAll();
	}
}

action

对应的Action类,其代码如下:

package cn.lut.curiezhang.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
/*import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;*/
import java.util.List;
import java.util.ResourceBundle;

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.lut.curiezhang.model.UserLog;
import cn.lut.curiezhang.service.UserLogService;

/**
 * UserLog 管理的Action类
 * @author curiezhang
 *
 */
@SuppressWarnings("serial")
public class UserLogAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, ModelDriven<Object> {
	private static final Logger log = LogManager.getLogger(UserLogAction.class);
	public static final String USER_SESSION = ResourceBundle.getBundle("Messages").getString("Application.sessionName");
	private HttpServletRequest request;
    public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
    @SuppressWarnings("unused")
	private HttpServletResponse response;
    public void setServletResponse(HttpServletResponse response) {  
        this.response = response;  
    }
	/**
	 * 模型驱动使用的类
	 */
	private UserLog model = new UserLog();
	public void setModel(UserLog model) {
		log.debug("cmis:Action层 > 设置模型");
		this.model = model;
	}
	@Override
	public Object getModel() {
		return (list != null ? list : model);
	}

	/**
	 * 搜索唯一id使用的参数
	 */
	private String userLogId;
	public String getUserLogId() {
		log.debug("cmis:Action层 > 得到id");
		return userLogId;
	}
	public void setUserLogId(String userLogId) {
		log.debug("cmis:Action层 > 设置id");
		if(userLogId != null) {
			this.model = userLogService.getUserLogById(userLogId);
		}
		this.userLogId = userLogId;
	}

	/**
	 * 搜索所有数据时的返回结果
	 */
	private Collection<UserLog> list;
	public Collection<UserLog> getList() {
		log.debug("cmis:Action层 > 得到所有数据,list");
		return list;
	}

	
	/**
	 * Struts和Spring整合过程中按名称自动注入的业务层的类
	 */
	private UserLogService userLogService;
	public void setUserLogService(UserLogService userLogService) {
		log.debug("cmis:Action层 > 注入Service类");
		this.userLogService = userLogService;
	}
	
	/**
	 * 填写表单时执行该方法,可进行有效性验证
	 * @return true 不能通过有效性验证
	 */
	public void validate() {
		log.debug("cmis:Action层 > 有效性验证,id为{}", userLogId);
    }
	/**
	 * *****************************************
	 * 分页处理 开始
	 */
    /** 
     * datatable start value 
     */  
    private int start;
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	private List<UserLog> data;
    public List<UserLog> getData() {
		return data;
	}
	public void setData(List<UserLog> data) {
		this.data = data;
	}
	/** 
     * datatable page size 
     */  
	private int draw;
    private int length;
    private int recordsTotal;
    private int recordsFiltered;
	private int recordsDisplay;
	public int getDraw() {
		return draw;
	}
	public void setDraw(int draw) {
		this.draw = draw;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public int getRecordsTotal() {
		return recordsTotal;
	}
	public void setRecordsTotal(int recordsTotal) {
		this.recordsTotal = recordsTotal;
	}
	public int getRecordsFiltered() {
		return recordsFiltered;
	}
	public void setRecordsFiltered(int recordsFiltered) {
		this.recordsFiltered = recordsFiltered;
	}
	public int getRecordsDisplay() {
		return recordsDisplay;
	}
	public void setRecordsDisplay(int recordsDisplay) {
		this.recordsDisplay = recordsDisplay;
	}
	/**
	 * search
	 */
    private String search;
    public String getSearch() {
		return search;
	}
	public void setSearch(String search) {
		this.search = search;
	}
	/**
	 * order
	 */
    /*private String order;
    public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}*/
    public String indexJson() {
    	log.debug("cmis:Action层 > indexJson查询");
    	recordsTotal = userLogService.count();
    	search = request.getParameter("search[value]");
    	if(search == null || search.trim().length() == 0)
    		search = "";
    	log.debug("cmis:Action层 > 首页查询search={}", search);
        /*order = request.getParameter("order[value]");
    	if(order == null || order.trim().length() == 0)
    		order = "";
    	log.debug("cmis:Action层 > 首页查询order={}", order);*/
    	// 排序参数
    	log.debug("cmis:Action层 > 首页查询order[0][column]={}", request.getParameter("order[0][column]"));
    	log.debug("cmis:Action层 > 首页查询order[0][dir]={}", request.getParameter("order[0][dir]"));
    	String[] order = new String[]{"userLogId",
                "userLogName",
                "userId",
                "userName",
                "userIp",
                "logDate",
                "visitUrl",
                "operateAction",
                "operateMethod",
                "operateParameter",
                "returnValue",
                "memo"};
    	String orderBy = " ";
    	if(request.getParameter("order[0][column]") != null && request.getParameter("order[0][column]").trim().length() > 0)
    		orderBy = " order by " + order[Integer.parseInt(request.getParameter("order[0][column]"))] 
    			+ " " + request.getParameter("order[0][dir]") + " ";
    	int page = 1;
    	if(length != 0)
    		page = start / length + 1;
    	data = (List<UserLog>)userLogService.listPage(page, length, search, orderBy);
    	recordsFiltered = userLogService.countSearch(search);  
        recordsDisplay = data.size(); 
    	log.debug("cmis:Action层 > 首页查询recordsTotal={}", recordsTotal);
    	log.debug("cmis:Action层 > 首页查询search={}", request.getParameter("search[value]"));
    	log.debug("cmis:Action层 > 首页查询draw={}", draw);
    	log.debug("cmis:Action层 > 首页查询start={}", start);
    	log.debug("cmis:Action层 > 首页查询length={}", length);
    	log.debug("cmis:Action层 > 首页查询page={}", page);
    	log.debug("cmis:Action层 > 首页查询recordsDisplay={}", recordsDisplay);
    	log.debug("cmis:Action层 > 首页查询recordsFiltered={}", recordsFiltered);
		return SUCCESS;
    }
    public String indexJsonPayTheFee() {
    	indexJsonCondition("payTheFee");
		return SUCCESS;
    }
    public String indexJsonLogin() {
    	indexJsonCondition("login");
		return SUCCESS;
    }
    public String indexJsonUpload() {
    	indexJsonCondition("fileUpload");
		return SUCCESS;
    }
    public String indexJsonDownload() {
    	indexJsonCondition("fileDownload");
		return SUCCESS;
    }
    public String indexJsonPrintTicket() {
    	indexJsonCondition("printTicket");
		return SUCCESS;
    }
    public String indexJsonInsert() {
    	indexJsonCondition("create");
		return SUCCESS;
    }
    public String indexJsonUpdate() {
    	indexJsonCondition("update");
		return SUCCESS;
    }
    public String indexJsonDelete() {
    	indexJsonCondition("delete");
		return SUCCESS;
    }
    public void indexJsonCondition(String condition) {
    	log.debug("cmis:Action层 > indexJsonPayTheFee查询");
    	recordsTotal = userLogService.count(condition);
    	search = request.getParameter("search[value]");
    	if(search == null || search.trim().length() == 0)
    		search = "";
    	log.debug("cmis:Action层 > 首页查询search={}", search);
    	// 排序参数
    	log.debug("cmis:Action层 > 首页查询order[0][column]={}", request.getParameter("order[0][column]"));
    	log.debug("cmis:Action层 > 首页查询order[0][dir]={}", request.getParameter("order[0][dir]"));
    	String[] order = new String[]{"userLogId",
                "userLogName",
                "userId",
                "userName",
                "userIp",
                "logDate",
                "visitUrl",
                "operateAction",
                "operateMethod",
                "operateParameter",
                "returnValue",
                "memo"};
    	String orderBy = " ";
    	if(request.getParameter("order[0][column]") != null && request.getParameter("order[0][column]").trim().length() > 0)
    		orderBy = " order by " + order[Integer.parseInt(request.getParameter("order[0][column]"))] 
    			+ " " + request.getParameter("order[0][dir]") + " ";
    	int page = 1;
    	if(length != 0)
    		page = start / length + 1;
    	data = (List<UserLog>)userLogService.listPage(page, length, search, orderBy, condition);
    	recordsFiltered = userLogService.countSearch(search, condition);  
        recordsDisplay = data.size(); 
    }
	/**
	 * *****************************************
	 * 分页处理 结束
	 */
	
	/**
	 * 首页
	 * @throws IOException 
	 */
    public String index() {
    	log.debug("cmis:Action层 > 首页查询");
		return SUCCESS;
    }
    public String indexPayTheFee() {
    	log.debug("cmis:Action层 > indexPayTheFee查询");
		return SUCCESS;
    }
    public String indexLogin() {
    	log.debug("cmis:Action层 > indexLogin查询");
		return SUCCESS;
    }
    public String indexUpload() {
    	log.debug("cmis:Action层 > indexUpload查询");
		return SUCCESS;
    }
    public String indexDownload() {
    	log.debug("cmis:Action层 > indexDownload查询");
		return SUCCESS;
    }
    public String indexPrintTicket() {
    	log.debug("cmis:Action层 > indexPrintTicket查询");
		return SUCCESS;
    }
    public String indexInsert() {
    	log.debug("cmis:Action层 > indexInsert查询");
		return SUCCESS;
    }
    public String indexUpdate() {
    	log.debug("cmis:Action层 > indexUpdate查询");
		return SUCCESS;
    }
    public String indexDelete() {
    	log.debug("cmis:Action层 > indexDelete查询");
		return SUCCESS;
    }
	/**
	 * 添加
	 */
    public String add() {
    	log.debug("cmis:Action层 > 添加信息入口");
    	model = new UserLog();
    	return SUCCESS;
    }
	/**
	 * 删除
	 */
    public String deleteConfirm() {
    	log.debug("cmis:Action层 > 删除确认入口");
    	return SUCCESS;
    }
	/**
	 * 修改
	 */
    public String modify() {
    	log.debug("cmis:Action层 > 修改信息入口");
    	return SUCCESS;
    }
	/**
	 * 查询
	 */
    public String browse() {
    	log.debug("cmis:Action层 > 浏览信息入口");
    	return SUCCESS;
    }
	/**
	 * 登录
	 */
    public String loginAdmin() {
    	log.debug("cmis:Action层 > 登录管理入口");
    	return "loginAdmin";
    }
    
    /**
     * 创建消息
     * @return
     */
    public String create() {
    	log.debug("cmis:Action层 > 添加新信息,id为 {}", model.getUserLogId());
        Collection<String> ids = new ArrayList<String>();
        list = userLogService.getAll();
        for(UserLog userLog : list) {
        	ids.add(userLog.getUserLogId());
        }
        if(ids.contains(model.getUserLogId())){
            String info = ResourceBundle.getBundle("Messages").getString("UserLog.result.createError");
            addActionMessage(info);
        	return "error";
        } else {
        	userLogService.save(model);
            String info = ResourceBundle.getBundle("Messages").getString("UserLog.result.create");
            addActionMessage(info);
            return "success";
        }
    }

    /**
     * 删除消息
     * @return
     */
    public String delete() {
    	log.debug("cmis:Action层 > 删除信息,id为 {}", userLogId);
    	Collection<String> names = new ArrayList<String>();
        list = userLogService.getAll();
        for(UserLog userLog : list) {
       		names.add(userLog.getUserLogId());
        }
        if(names.contains(userLogId)){
	        userLogService.delete(userLogId);
	        String info = ResourceBundle.getBundle("Messages").getString("UserLog.result.delete");
	        addActionMessage(info);
	        return SUCCESS;
        } else {
        	String info = ResourceBundle.getBundle("Messages").getString("UserLog.result.deleteError");
        	addActionMessage(info);
        	return ERROR;
        }
    }

    /**
     * 修改消息
     * @return
     */
    public String update() {
    	log.debug("cmis:Action层 > 修改信息,id为 {}", model.getUserLogId());
        userLogService.update(model);
        String info = ResourceBundle.getBundle("Messages").getString("UserLog.result.update");
        addActionMessage(info);
        return SUCCESS;
    }
}

请注意代码中有关分页处理的部分,具体细节请仔细研究代码,了解如何对大量数据进行分页处理,这里就不再赘述了。

配置

配置方法和前面介绍的其他信息的管理是一致的,这里不再赘述。

Messages.properties

在Messages.properties中需要添加对应的描述信息,代码如下:

#################### cmis UserLog ######################
########## UserLog
UserLog.page.title=\u7528\u6237\u65E5\u5FD7\u7BA1\u7406
UserLog.navigation.homepage=\u7528\u6237\u65E5\u5FD7\u7BA1\u7406
UserLog.button.home=\u8FD4\u56DE\u7528\u6237\u65E5\u5FD7\u7BA1\u7406\u9996\u9875
UserLog.button.add=\u6DFB\u52A0\u7528\u6237\u65E5\u5FD7
# UserLog title
UserLog.title.index=\u7528\u6237\u65E5\u5FD7
UserLog.title.add=\u6DFB\u52A0\u7528\u6237\u65E5\u5FD7
UserLog.title.browse=\u6D4F\u89C8\u7528\u6237\u65E5\u5FD7
# UserLog help
UserLog.help.indexTitle=\u7528\u6237\u65E5\u5FD7\u7BA1\u7406\u2014\u5E2E\u52A9
UserLog.help.index=\u5217\u51FA\u6240\u6709\u7684\u7528\u6237\u65E5\u5FD7\uFF0C\u53EF\u4EE5\u5BF9\u5176\u8FDB\u884C\u76F8\u5E94\u7684\u7BA1\u7406\u64CD\u4F5C\u3002
UserLog.help.browseTitle=\u6D4F\u89C8\u7528\u6237\u65E5\u5FD7\u2014\u5E2E\u52A9
UserLog.help.browse=\u6D4F\u89C8\u7528\u6237\u65E5\u5FD7\uFF0C\u67E5\u770B\u7279\u5B9A\u7684\u7528\u6237\u65E5\u5FD7\u7684\u8BE6\u7EC6\u4FE1\u606F\u3002
# UserLog field prompt
# UserLog field name
UserLog.fieldName.userLogId=\u7528\u6237\u65E5\u5FD7\u4EE3\u7801
UserLog.fieldName.userLogName=\u7528\u6237\u65E5\u5FD7\u540D\u79F0
UserLog.fieldName.userId=\u7528\u6237\u7F16\u53F7
UserLog.fieldName.userName=\u7528\u6237\u540D
UserLog.fieldName.userIp=\u7528\u6237ip
UserLog.fieldName.logDate=\u65E5\u5FD7\u65F6\u95F4
UserLog.fieldName.visitUrl=\u8BBF\u95EEurl
UserLog.fieldName.operateAction=\u64CD\u4F5CAction
UserLog.fieldName.operateMethod=\u64CD\u4F5C\u65B9\u6CD5
UserLog.fieldName.operateParameter=\u64CD\u4F5C\u53C2\u6570
UserLog.fieldName.operateResult=\u64CD\u4F5C\u7ED3\u679C
UserLog.fieldName.operateResult.0=\u6210\u529F
UserLog.fieldName.operateResult.1=\u5931\u8D25
UserLog.fieldName.operateResult.others=\u9519\u8BEF
UserLog.fieldName.returnValue=\u8FD4\u56DE\u503C
UserLog.fieldName.memo=\u5907\u6CE8
# UserLog field error
# UserLog result info
#################### /cmis UserLog ######################

网页

jsp页面主要有两种,一种是查看详细信息的页面browse.jsp,如下所示:

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- cmis.title -->
  <title><s:text name="Application.name"/> - <s:text name="UserLog.page.title"/></title>
  <!-- /cmis.title -->
  <jsp:include page="../includes/beginning-notheme.jsp" flush="true" />	
</head>
<body>
  <h4>
    <!-- cmis.navigation -->
    <i class="fa fa-home"></i>&nbsp;
    <a href="${pageContext.request.contextPath}/"><s:text name="Navigation.application.homepage"/></a>
    &nbsp;<i class="fa fa-angle-right"></i>&nbsp;
    <a href="${pageContext.request.contextPath}/admin/index"><s:text name="Navigation.admin.homepage"/></a>
    &nbsp;<i class="fa fa-angle-right"></i>&nbsp;
    <a href="${pageContext.request.contextPath}/admin/userLog/index"><s:text name="Navigation.admin.userLog.homepage"/></a>
    <!-- /cmis.navigation -->
  </h4>
  <!-- mainContent -->
  <!-- cmis.UserLog.title -->
  <h3><s:text name="UserLog.title.browse"/> - <s:property value="model.userLogName"/></h3>
  <!-- /cmis.UserLog.title -->
  <hr>
  <s:actionmessage cssClass="alert alert-danger"/>
  
  <table class="table table-striped">
    <!-- userLogId -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.userLogId"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.userLogId"/></td>
    </tr>
    <!-- /userLogId -->
    <!-- userLogName -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.userLogName"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.userLogName"/></td>
    </tr>
    <!-- /userLogName -->
    <!-- userId -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.userId"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.userId"/></td>
    </tr>
    <!-- /userId -->
    <!-- userName -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.userName"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.userName"/></td>
    </tr>
    <!-- /userName -->
    <!-- userIp -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.userIp"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.userIp"/></td>
    </tr>
    <!-- /userIp -->
    <!-- logDate -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.logDate"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.logDate"/></td>
    </tr>
    <!-- /logDate -->
    <!-- visitUrl -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.visitUrl"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.visitUrl"/></td>
    </tr>
    <!-- /visitUrl -->
    <!-- operateAction -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.operateAction"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.operateAction"/></td>
    </tr>
    <!-- /operateAction -->
    <!-- operateMethod -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.operateMethod"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.operateMethod"/></td>
    </tr>
    <!-- /operateMethod -->
    <!-- operateParameter -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.operateParameter"/></td>
      <td class="span9 verticalMiddle">
        <textarea id="json_content_value" class="form-control d-none"><s:property value="model.operateParameter"/></textarea>
        <br/>
        <blockquote>
          <p id="json_output_value"></p>
        </blockquote>
      </td>
    </tr>
    <!-- /operateParameter -->
    <!-- operateResult -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.operateResult"/></td>
      <td class="span9 verticalMiddle">
        <s:if test="model.operateResult == 0">
          <s:text name="UserLog.fieldName.operateResult.0"/>
        </s:if><s:elseif test="model.operateResult == 1">
          <s:text name="UserLog.fieldName.operateResult.1"/>
        </s:elseif><s:else>
          <s:text name="UserLog.fieldName.operateResult.others"/>
        </s:else>
      </td>
    </tr>
    <!-- /operateResult -->
    <!-- returnValue -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.returnValue"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.returnValue"/></td>
    </tr>
    <!-- /returnValue -->
    <!-- memo -->
    <tr>
      <td class="span3 verticalMiddle" width="120px"><s:text name="UserLog.fieldName.memo"/></td>
      <td class="span9 verticalMiddle"><s:property value="model.memo"/></td>
    </tr>
    <!-- /memo -->
  </table>
  
  <hr>
  <a href="${pageContext.request.contextPath}/admin/userLog/index" class="btn btn-info">
    <!-- cmis.UserLog.home -->
    <span class="glyphicon glyphicon-arrow-left"></span> <s:text name="UserLog.button.home"/>
    <!-- /cmis.UserLog.home -->
  </a>
  <!-- BLANK -->
  <br/><br>
  <!-- /BLANK -->
  <!-- /mainContent -->
  <!-- cmis.help -->
  <jsp:include page="../includes/help1.jsp" flush="true" />
    <h4 class="modal-title"><s:text name="UserLog.help.browseTitle"/></h4>
  <jsp:include page="../includes/help2.jsp" flush="true" />
    <p>
      <s:text name="UserLog.help.browse"/>
    </p>
  <jsp:include page="../includes/help3.jsp" flush="true" />
  <!-- /cmis.help -->
  <!-- 格式化json -->
  <script type="text/javascript" src="${pageContext.request.contextPath}/js/jsonFormat.js"></script>
</body>
</html>

另一种是浏览列表信息的页面logAll.jsp,或logDelete.jsp等等,如下所示:

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- cmis.title -->
  <title><s:text name="Application.name"/> - <s:text name="UserLog.page.title"/></title>
  <!-- /cmis.title -->
  <jsp:include page="../includes/beginning-notheme.jsp" flush="true" />  
</head>
<body>
  <h4>
    <!-- cmis.navigation -->
    <i class="fa fa-home"></i>&nbsp;
    <a href="${pageContext.request.contextPath}/"><s:text name="Navigation.application.homepage"/></a>
    &nbsp;<i class="fa fa-angle-right"></i>&nbsp;
    <a href="${pageContext.request.contextPath}/admin/index"><s:text name="Navigation.admin.homepage"/></a>
    &nbsp;<i class="fa fa-angle-right"></i>&nbsp;
    <s:text name="Navigation.admin.userLog.homepage"/>
    <!-- /cmis.navigation -->
  </h4>
  <!-- mainContent -->
  <!-- cmis.UserLog.title -->
  <h3><s:text name="UserLog.title.index"/></h3>
  <!-- /cmis.UserLog.title -->
  <hr>
  <s:actionmessage cssClass="alert alert-danger"/>
            
  <table id="dataTable" class="table table-condensed table-hover table-striped" style="width:100%;">
    <thead>
      <tr>
        <th><s:text name="UserLog.fieldName.userLogId"/></th>
        <th><s:text name="UserLog.fieldName.userLogName"/></th>
        <th><s:text name="UserLog.fieldName.userId"/></th>
        <th><s:text name="UserLog.fieldName.userName"/></th>
        <th><s:text name="UserLog.fieldName.userIp"/></th>
        <th><s:text name="UserLog.fieldName.logDate"/></th>
        <th><s:text name="UserLog.fieldName.visitUrl"/></th>
        <th><s:text name="UserLog.fieldName.operateAction"/></th>
        <th><s:text name="UserLog.fieldName.operateMethod"/></th>
        <th><s:text name="UserLog.fieldName.operateParameter"/></th>
        <th><s:text name="UserLog.fieldName.returnValue"/></th>
        <th><s:text name="UserLog.fieldName.memo"/></th>
        <th width="60px"><s:text name="DataTables.title.operate"/></th>
      </tr>
    </thead>
    <tfoot>
      <tr>
        <th><s:text name="UserLog.fieldName.userLogId"/></th>
        <th><s:text name="UserLog.fieldName.userLogName"/></th>
        <th><s:text name="UserLog.fieldName.userId"/></th>
        <th><s:text name="UserLog.fieldName.userName"/></th>
        <th><s:text name="UserLog.fieldName.userIp"/></th>
        <th><s:text name="UserLog.fieldName.logDate"/></th>
        <th><s:text name="UserLog.fieldName.visitUrl"/></th>
        <th><s:text name="UserLog.fieldName.operateAction"/></th>
        <th><s:text name="UserLog.fieldName.operateMethod"/></th>
        <th><s:text name="UserLog.fieldName.operateParameter"/></th>
        <th><s:text name="UserLog.fieldName.returnValue"/></th>
        <th><s:text name="UserLog.fieldName.memo"/></th>
        <th width="60px"><s:text name="DataTables.title.operate"/></th>
      </tr>
    </tfoot>
  </table>
  <script type="text/javascript" class="init">
    "use strict";
    $(document).ready(function() {
      $('#dataTable').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax" : {  
            "url" : "indexJson",  
            "type" : "POST",
        },
        "columns": [
            { "data": "userLogId" },
            { "data": "userLogName" },
            { "data": "userId" },
            { "data": "userName" },
            { "data": "userIp" },
            { "data": "logDate" },
            { "data": "visitUrl" },
            { "data": "operateAction" },
            { "data": "operateMethod" },
            { "data": "operateParameter" },
            { "data": "returnValue" },
            { "data": "memo" },
            null
        ],
        "order": [[ 0, "desc" ]],
        "language": {
            // 格式化数字显示格式
            "decimal":",",
            "thousands":".",
            // 语言国际化
            "lengthMenu": "每页 _MENU_ 条记录",
            "search": "搜索:",
            "zeroRecords": "没有找到记录",
            "info": "当前显示第 _START_ 至 _END_ 项(共 _TOTAL_ 项),正在第 _PAGE_ 页(共 _PAGES_ 页)",
            "infoEmpty": "无记录",
            "infoFiltered": "(从 _MAX_ 条记录过滤)",
            "processing": "处理中...",
            "emptyTable": "表中数据为空",
            "loadingRecords": "载入中...",
            "infoThousands": ",",
            "paginate": {
                "first": "首页",
                "previous": "上页",
                "next": "下页",
                "last": "末页",
                "jump": "跳转"
            }
          },
        // 隐藏列
        "columnDefs": [
          {
            "targets": [ 0, 2, 7, 8, 9, 10, 11 ],
            "visible": false,
            "searchable": false
          },
          {
            "targets": 12,
            "render": function ( data, type, row ) {
                return '<a href="browse?userLogId=' + row.userLogId + '" class="btn btn-default">' +
                       '<span class="glyphicon glyphicon-eye-open"></span> <s:text name="DataTables.title.browse"/>' +
                       '</a>';
            },
            "searchable": false
          }
        ]
      } );
    } );
  </script>
  <hr>
  <%-- <a href="${pageContext.request.contextPath}/admin/userLog/add" class="btn btn-primary">
    <span class="glyphicon glyphicon-file"></span> <s:text name="UserLog.button.add"/> 
  </a> --%>
  <!-- BLANK -->
  <br/><br>
  <!-- /BLANK -->
  <!-- /mainContent -->
  <!-- cmis.help -->
  <jsp:include page="../includes/help1.jsp" flush="true" />
    <h4 class="modal-title"><s:text name="UserLog.help.indexTitle"/></h4>
  <jsp:include page="../includes/help2.jsp" flush="true" />
    <p>
      <s:text name="UserLog.help.index"/>
    </p>
  <jsp:include page="../includes/help3.jsp" flush="true" />
  <!-- /cmis.help -->
</body>
</html>

运行

系统调整好后就可以部署运行了,项目是运行的截图:
日志浏览
单击浏览按钮,就可以查看日志的详细信息,如下图所示:
查看详情

发布了42 篇原创文章 · 获赞 15 · 访问量 5863

猜你喜欢

转载自blog.csdn.net/ZhangCurie/article/details/102715323