1.导航栏添加新的页面标题,添加一个新建页面
利用标签的href属性定位到后端controller层
controller返回一个ModelAndView类型的视图,就是返回的前端设计新页面“log_login.jsp”;
一个初步的简单页面添加成功!
2.对新建的空白页面进行初始的设置,添加自己要展示的内容
简单截取一部分,之后前端的知识用到另外详细记录!
初始化方法,将页面初始化,再对应另外的方法分别书写点击各个按钮所对应的功能。
3.后端对应数据库数据的填充!
@RequestMapping("/loginLog")
public ModelAndView getLoginLog(Integer pageSize, Integer currentPage, String uname, String ip, Integer type, String loginDateBegin,String loginDateEnd){
ModelAndView modelAndView = new ModelAndView("log_login");
if (pageSize==null||pageSize<=0){
pageSize = 10;
}
if (currentPage==null||currentPage<=0){
currentPage = 1;
}
if(type!=null && type.equals(0)){
//当code传过来是"0",则表示全部,则查询时不带如code的值
type = null;
}
if (uname!=null && !uname.equals("")){
uname = URLDecoder.decode(uname);
}
if (ip!=null && !ip.equals("")){
ip = URLDecoder.decode(ip);
}
AdminLoginLogEntity loginLog = new AdminLoginLogEntity();
loginLog.setUName(uname);
loginLog.setLoginType(type);
loginLog.setIp(ip);
loginLog.setLoginDateBegin(loginDateBegin);
loginLog.setLoginDateEnd(loginDateEnd);
//查询数据库
PageInfo<AdminLoginLogEntity> queryResult = adminLoginLogService.getRecordByPage(pageSize,currentPage,loginLog);
modelAndView.addObject("dataList",queryResult.getList());
modelAndView.addObject("dataTotal",queryResult.getTotal());
modelAndView.addObject("currentPage",currentPage);
modelAndView.addObject("pageSize",pageSize);
modelAndView.addObject("type",type);
modelAndView.addObject("uname",uname );
modelAndView.addObject("ip",ip);
modelAndView.addObject("loginDateBegin",loginDateBegin);
modelAndView.addObject("loginDateEnd",loginDateEnd);
modelAndView.addObject("barItem",6);
return modelAndView;
}
在controller的方法中直接传入前端需要输入查询的参数,在刚开始进入的初始化页面没有传参,对其进行未输入内容的判断通过URLDecoder.decode解码,这样可以保证得到原始数值,不会报错。在后续数据库查询时便没有条件限制,会得到全部的日志记录信息返回给下面定义的PageInfo集合queryResult。
接下来定义一个登录日志实体类的对象,用传入参数将其实例化,传入数据库查询方法;
PageInfo<AdminLoginLogEntity> queryResult = adminLoginLogService.getRecordByPage(pageSize,currentPage,loginLog);
@Override
public PageInfo<AdminLoginLogEntity> getRecordByPage(Integer pageSize, Integer currentPage, AdminLoginLogEntity loginLog) {
List<AdminLoginLogEntity> list = null;
try {
//利用PageHelper分页查询 注意:这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
PageHelper.startPage(currentPage,pageSize);
list = adminLoginLogDao.getRecordByPage(loginLog);
}catch (Exception e){
e.printStackTrace();
logger.error("AdminLoginLogServiceImpl-getRecordByPage:数据库获取最新的测试连接性数据出错!"+e.getMessage());
}
PageInfo<AdminLoginLogEntity> pageInfo = new PageInfo<>(list);
return pageInfo;
}
自此,dao调用mapper.xml中数据库查询方法得到一个完整地带有分页的集合。
<select id="getRecordByPage" parameterType="cn.****.****.entity.AdminLoginLogEntity" resultType="AdminLoginLogEntity">
SELECT t1.uName,t1.loginType,t1.ip,t1.loginTime FROM login_log t1
<where>
<if test="uName!= null and uName!=''">
and t1.uName like "%"#{uName}"%"
</if>
<!--模糊查询第二种书写方式 -->
<if test="ip!=null and ip!=''">
and t1.ip like "%"#{ip}"%"
</if>
<if test="loginType!=null and loginType!=0">
and t1.loginType = #{loginType}
</if>
<if test="loginDateBegin!=null">
and t1.loginTime >= #{loginDateBegin}
</if>
<if test="loginDateEnd!=null">
and #{loginDateEnd} >= t1.loginTime
</if>
</where>
</select>
然后回到controller的最后,对要返回的modleAndView进行设置,将得到的集合一并返回展示给页面!