后台分页的页码安全(超过总页数,页码为非数字)检查问题
2017-06-29 21:35
435 查看
后台分页的页码安全(超过总页数,页码为非数字)检查问题
解决办法使用校验器:
如果页码没大于接收页码参数类型的最大值,但是页码大于总页数。
就在业务层进行判断
如果当页码小于等于0并且是负数,那么就把当前页设置为第一页
如果页码为非数字,使用数据校验及类型转换器(我这是数校验并跳转到提示页面)
完整代码
实体:
package cn.dental.domain;
/**
* 用户实体类
*
* @author 吕小刚
* @version V1.0
*/
public class User {
private Long id;//编号,统一long类型
private String name;//姓名
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
分页实体:
Dao层
Service层
Action层
package cn.dental.action;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.dental.domain.User;
import cn.dental.service.UserService;
import cn.dental.utils.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* User的Action类
*
* @author 吕小刚
* @version V1.0
*
*/
@SuppressWarnings("serial")
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User> {
// =================== Service实例
@Resource
private UserService userService;
// =================== 创建模型驱动对象
private User model = new User();
private int page;// 接收页码
public User getModel() {
return model;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
// =================== Action处理方法
/** 查询所有信息 */
public String findAll() {
// List<User> userList = userService.findAll();
PageBean<User> pageBeanUser = userService.findAll(page);
// ActionContext.getContext().getValueStack().set("userList", userList);
ActionContext.getContext().getValueStack().set("pageBeanUser", pageBeanUser);
return "findAll";
}
}
jsp页面
解决办法使用校验器:
如果页码没大于接收页码参数类型的最大值,但是页码大于总页数。
就在业务层进行判断
// 如果当前页大于总页数,那么就把最后一页的就是当前页 if (page > pageBean.getTotalPage()) { page = pageBean.getTotalPage(); pageBean.setPage(page); } if (page <= 0) { page = 1; pageBean.setPage(page); }
如果当页码小于等于0并且是负数,那么就把当前页设置为第一页
// 如果当页码小于等于0并且是负数,那么就把当前页设置为第一页 if (page <= 0) { page = 1; pageBean.setPage(page); }
如果页码为非数字,使用数据校验及类型转换器(我这是数校验并跳转到提示页面)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <validators> <field name="page"> <field-validator type="conversion"> <message key="你输入的是无效的页码" /> </field-validator> </field> </validators>
完整代码
实体:
package cn.dental.domain;
/**
* 用户实体类
*
* @author 吕小刚
* @version V1.0
*/
public class User {
private Long id;//编号,统一long类型
private String name;//姓名
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
分页实体:
package cn.dental.utils; import java.util.ArrayList; import java.util.List; /** * 分页查询 * * @author 吕小刚 * @version V1.0 * @param <T> */ public class PageBean<T> { /** 当前页 */ private Integer page; /** 每页显示/条数 */ private Integer limit; /** 总页数 */ private Integer totalPage; /** 总记录数 */ private Long totalCount; /** 分页查询集合数据 */ private List<T> pageList = new ArrayList<T>(); // =============== getXxx()/setXxx() /** * 获取:当前页 */ public Integer getPage() { return page; } /** 设置:当前页 */ public void setPage(Integer page) { this.page = page; } /** 获取:每页显示/条数 */ public Integer getLimit() { return limit; } /** 设置:每页显示/条数 */ public void setLimit(Integer limit) { this.limit = limit; } /** 获取:总页数 */ public Integer getTotalPage() { return totalPage; } /** 设置:总页数 */ public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } /** 获取:总记录数 */ public Long getTotalCount() { return totalCount; } /** 设置:总记录数 */ public void setTotalCount(Long totalCount) { this.totalCount = totalCount; } /** 获取:分页查询集合数据 */ public List<T> getPageList() { return pageList; } /** 设置:分页查询集合数据 */ public void setPageList(List<T> pageList) { this.pageList = pageList; } }
Dao层
package cn.dental.dao; import java.util.List; import cn.dental.domain.User; /** * User持久层接口 * * @author 吕小刚 * @version V1.0 */ public interface UserDao { /** * 查询所有用户信息 * * @return 返回用户 */ public List<User> findAll(); /** * 分页查询 * * @param page * 页码(当前页) * @param limit * 每页显示条数 * @return 返回分页查询出来的数据 */ public List<User> findAll(Integer page, Integer limit); /** 查询总记录数 */ public Long findCount(); }
package cn.dental.dao.impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Repository; import cn.dental.dao.UserDao; import cn.dental.domain.User; /** * User持久层实现类 * * @author 吕小刚 * @version V1.0 */ @Repository("userDaoImpl") @SuppressWarnings("unchecked") public class UserDaoImpl implements UserDao { @Resource private SessionFactory sessionFactory; protected Session getSession() { return sessionFactory.getCurrentSession(); } public List<User> findAll() { return getSession().createQuery("from User u").list(); } public List<User> findAll(Integer page, Integer limit) { if (page == null) { return null; } List<User> list = getSession().createQuery(// "from User u")// .setFirstResult(page)// .setMaxResults(limit)// .list(); if (list != null && list.size() > 0) { return list; } return null; } public Long findCount() { Long count = (Long) getSession().createQuery(// "select count(u) from User u")// .uniqueResult(); if (count != null) { return count; } return null; } }
Service层
package cn.dental.service; import java.util.List; import cn.dental.domain.User; import cn.dental.utils.PageBean; /** * User服务层接口 * * @author 吕小刚 * @version V1.0 */ public interface UserService { /** 查询所有用户信息 */ public List<User> findAll(); /** * * 分页查询 * * @param page * 页码(当前页) * @param limit * 每页显示条数 * @return 返回分页查询出来的数据集合 */ public PageBean<User> findAll(Integer page); }
Action层
package cn.dental.action;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.dental.domain.User;
import cn.dental.service.UserService;
import cn.dental.utils.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* User的Action类
*
* @author 吕小刚
* @version V1.0
*
*/
@SuppressWarnings("serial")
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User> {
// =================== Service实例
@Resource
private UserService userService;
// =================== 创建模型驱动对象
private User model = new User();
private int page;// 接收页码
public User getModel() {
return model;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
// =================== Action处理方法
/** 查询所有信息 */
public String findAll() {
// List<User> userList = userService.findAll();
PageBean<User> pageBeanUser = userService.findAll(page);
// ActionContext.getContext().getValueStack().set("userList", userList);
ActionContext.getContext().getValueStack().set("pageBeanUser", pageBeanUser);
return "findAll";
}
}
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <validators> <field name="page"> <field-validator type="conversion"> <message key="你输入的是无效的页码" /> </field-validator> </field> </validators>
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>分页查询</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> </head> <body> <div align="center"> <table border="1"> <tr> <td>编号</td> <td>姓名</td> </tr> <s:iterator var="pageBeanUserList" value="pageBeanUser.pageList"> <tr> <td><s:property value="#pageBeanUserList.id" /> </td> <td><s:property value="#pageBeanUserList.name" /></td> </tr> </s:iterator> </table> <p> 第 <s:property value="pageBeanUser.page" /> /共 <s:property value="pageBeanUser.totalPage" /> 页 </p> <form action="user_findAll.action?page=${page}" enctype="multipart/form-data" method="post"> <input type="text" name="page" value=""> <input type="submit" value="添加"/> </form> </div> </body> </html>
<%@page import="com.opensymphony.xwork2.ActionContext"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>错误提示</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> </head> <body> <h1>错误</h1> <!-- 数据校验提示 --> <s:fielderror fieldName="page"/> </body> </html>
相关文章推荐
- Android开发安全问题之防止页面被其他程序覆盖监听,提示用户程序转移后台运行
- 当ListView控件在后台绑定数据源时,用DataPager控件分页需要注意的问题
- 总结目前用Jquery-Easyui做后台,遇到datagrid分页及数据刷新问题
- 面试问题2:给一个5G的大文件,保存的数据为32位的整型,找到所有出现次数超过两次的数字
- Hashtable与ArrayListTest编译问题:使用了未经检查或不安全的操作;请使用 -Xlint:unchecked 重新编译
- springMVC分页页码过多的问题
- 后台分页控件重复查询数据问题
- 关于统计课本页数出现数字的问题【举一反三】
- 关于数组的问题(连续子数组的最大和、数组中出现次数超过一半的数字、调整数组顺序使奇数位于偶数之前)
- 分页条中显示数字页码的计算方法
- python计算书页码的统计数字问题实例
- kkpager分页插件的Ajax数据变化但是页码不变的问题
- Element-UI分页组件连续点击后页码循环跳转问题解决方案
- 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
- Java Web 一些特殊字符的过滤(appscan检查的安全问题)
- 讨论一下js获取响应中后台传回来的BigInteger类型的数字时,后几位会自动变为0的问题
- ecshop安全问题:ecshop修改后台登录路径
- DWZ (JUI) 分页判断是否超过最大页数
- (转)AspNetPager查询分页问题(点击页码,不再是查询后的数据集)viewstate解决
- 关于数组的问题(连续子数组的最大和、数组中出现次数超过一半的数字、调整数组顺序使奇数位于偶数之前)