简单的实现方法,数据库部分就不展示了
1.Syslog.java 实体类(实体类放在Po包中)
package com.hotel.po;
/**
* 系统日志实体类
* @author Administrator
*
*/
public class Syslog {
private int logid;
private String logname;
private String logurl;
private String staffname;
private String description;
private String logtime;
public int getLogid() {
return logid;
}
public void setLogid(int logid) {
this.logid = logid;
}
public String getLogname() {
return logname;
}
public void setLogname(String logname) {
this.logname = logname;
}
public String getLogurl() {
return logurl;
}
public void setLogurl(String logurl) {
this.logurl = logurl;
}
public String getStaffname() {
return staffname;
}
public void setStaffname(String staffname) {
this.staffname = staffname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLogtime() {
return logtime;
}
public void setLogtime(String logtime) {
this.logtime = logtime;
}
}
2.Loglink.java另一张表的实体类
package com.hotel.po;
public class Loglink {
private int linkId;
private String logurl;
private String logname;
private String description;
public int getLinkId() {
return linkId;
}
public void setLinkId(int linkId) {
this.linkId = linkId;
}
public String getLogurl() {
return logurl;
}
public void setLogurl(String logurl) {
this.logurl = logurl;
}
public String getLogname() {
return logname;
}
public void setLogname(String logname) {
this.logname = logname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
3.SyslogDaoImpl.java该方法用于后台数据操作
package com.hotel.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.hotel.dao.ISyslogDao;
import com.hotel.po.Syslog;
import com.hotel.util.MyBatisUtil;
public class SyslogDaoImpl implements ISyslogDao {
public SyslogDaoImpl() {
}
private static ISyslogDao logDao = new SyslogDaoImpl();
public static ISyslogDao getInstance() {
return logDao;
}
SqlSession sqlSession = null;
public List<Syslog> getAllLog() {
sqlSession = MyBatisUtil.getSqlSession();
List<Syslog> logList = sqlSession.getMapper(ISyslogDao.class)
.getAllLog();
MyBatisUtil.closeSqlSession(sqlSession);
return logList;
}
public boolean saveLog(Syslog log) {
boolean flag = false;
sqlSession = MyBatisUtil.getSqlSession();
try{
flag = sqlSession.getMapper(ISyslogDao.class).saveLog(log);
sqlSession.commit();
}catch (Exception e) {
sqlSession.rollback();
}
MyBatisUtil.closeSqlSession(sqlSession);
return flag;
}
public int getSyslogCount() {
sqlSession = MyBatisUtil.getSqlSession();
int logCount = sqlSession.getMapper(ISyslogDao.class).getSyslogCount();
MyBatisUtil.closeSqlSession(sqlSession);
return logCount;
}
public List<Syslog> getPageSyslog() {
sqlSession = MyBatisUtil.getSqlSession();
List<Syslog> logList = sqlSession.getMapper(ISyslogDao.class).getPageSyslog();
MyBatisUtil.closeSqlSession(sqlSession);
return logList;
}
}
4.ISyslogDao.java以上方法的接口
public List<Syslog> getAllLog();
public int getSyslogCount();
public boolean saveLog(Syslog log);
public List<Syslog> getPageSyslog();
5.Loglink 同理,只需要写public Loglink getNameByLink(String linkurl);即可
6.Syslog.xml
<mapper namespace="com.hotel.dao.ISyslogDao">
<select id="getSyslogById" resultType="com.hotel.po.Syslog">
select * from syslog where logId=#{logId}
</select>
<insert id="saveLog" parameterType="com.hotel.po.Syslog">
insert into syslog(logname,logurl,logtime,staffname,description) values(#{logname},#{logurl},#{logtime},#{staffname},#{description})
</insert>
<select id="getAllLog" resultType="com.hotel.po.Syslog">
select * from syslog
</select>
<select id="getSyslogCount" resultType="com.hotel.po.Syslog">
select count(*) from syslog
</select>
<select id="getPageSyslog" resultType="com.hotel.po.Syslog">
select * from syslog group by logid desc limit #{page},#{size}
</select>
</mapper>
7.SyslogServiceImpl.java(该方法用于前台调用)
package com.hotel.service.impl;
import java.util.List;
import com.hotel.dao.ISyslogDao;
import com.hotel.dao.impl.SyslogDaoImpl;
import com.hotel.po.Syslog;
import com.hotel.service.ISyslogService;
public class SyslogServiceImpl implements ISyslogService {
public SyslogServiceImpl()
{
}
private static ISyslogService logservice= new SyslogServiceImpl();
public static ISyslogService getInstance()
{
return logservice;
}
ISyslogDao logdao=SyslogDaoImpl.getInstance();
public boolean saveLog(Syslog log)
{
return logdao.saveLog(log);
}
public List<Syslog> getAllLogList()
{
return logdao.getAllLog();
}
public int getSyslogCount()
{
return logdao.getSyslogCount();
}
public List<Syslog> getPageSyslog()
{
return logdao.getPageSyslog();
}
}
8.以上方法的接口
ISyslogService
package com.hotel.service;
import java.util.List;
import com.hotel.po.Syslog;
public interface ISyslogService {
public boolean saveLog(Syslog log);
public List<Syslog> getAllLogList();
public int getSyslogCount();
public List<Syslog> getPageSyslog();
}
9.Filter
package com.hotel.filter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hotel.po.Loglink;
import com.hotel.po.Syslog;
import com.hotel.service.ILoglinkService;
import com.hotel.service.ISyslogService;
import com.hotel.service.impl.LoglinkServiceImpl;
import com.hotel.service.impl.SyslogServiceImpl;
public class SyslogFilter implements Filter {
public void destroy() {
System.out.println("SyslogFilter:OFF");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("------------------SyslogFilter--------------");
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
//Staff staff = (Staff) req.getSession().getAttribute("staff");
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
ILoglinkService logservice=LoglinkServiceImpl.getInstance();
Loglink link = logservice.getNameByLink(req.getServletPath());
Syslog slog = new Syslog();
slog.setLogname(link.getLogname());
slog.setDescription(link.getLogname());
slog.setLogtime(sdf.format(new Date()));
slog.setLogurl(req.getServletPath());
//slog.setStaffname(staff.getStaffname());
slog.setStaffname("admin");
ISyslogService slogservice=SyslogServiceImpl.getInstance();
if(slogservice.saveLog(slog)){
chain.doFilter(req, res);
}else{
System.out.println("操作失败");
chain.doFilter(req, res);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("SyslogFilter:ON");
}
}
10.web.xml配置
<!-- 日志过滤器 -->
<filter>
<filter-name>SyslogFilter</filter-name>
<filter-class>com.hotel.filter.SyslogFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SyslogFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>