一、分页效果实现
1.自定义分页
1.controller接受参数返回分页pageBean 注意参数的初始化 @RequestMapping("/findAllProduct") public String findAllProduct(Model model, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize){ //传递参数调用service得到分页的对象 PageBean<Product> pb = productService.findAllProduct(pageNum,pageSize); model.addAttribute( "pb",pb ); return "product/productList"; } 2.service组装所有需要的属性分装pageBean返回 public PageBean<Product> findAllProduct(Integer pageNum, Integer pageSize) { //组装需要的pageBean对象返回给controller PageBean<Product> pb = new PageBean<Product>(); pb.setPageNum(pageNum); pb.setPageSize(pageSize); //调用dao得到分页使用的总记录数 Integer totalCount = productDao.findTotalCount(); pb.setTotalCount(totalCount); //计算得到总页数 Integer totalPage = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1; pb.setTotalPage(totalPage); /*查询得到分页的结果集合 mySql的分页 第二页每页4条 表product select * from product limit 4 , 4 startIndex pageSize * oracle的分页 第二页每页4条 表product * rownum的子查询实现 * select * from ( select rownum r ,p.* from product ) t where t.r > 4 and t.r <=8 * startIndex = (pageNum-1)*pageSize * endIndex = pageNum*pageSize * select * from ( select rownum r ,p.* from product where rownum<=8 ) t where t.r > 4 * */ Integer startIndex = (pageNum-1)*pageSize; Integer endIndex = pageNum*pageSize; List<Product> list = productDao.findAllProduct(startIndex,endIndex); pb.setList( list); return pb; } 3.dao统计记录数和 查询分页结果集 注意:多个基本类型参数用到注解@Param @Select("select * from " + "(select rownum r, p.* from product p where rownum <=#{endIndex} ) t " + " where t.r >#{startIndex} ") public List<Product> findAllProduct(@Param("startIndex") Integer startIndex, @Param("endIndex")Integer endIndex); 4.页面取值 统一获取pb中的属性 下拉框的回显需要根据条数回显 <option ${pb.pageSize==1?"selected":""}>1</option> <option ${pb.pageSize==3?"selected":""} >3</option> <option ${pb.pageSize==5?"selected":""}>5</option>
分页的弊端
1.分页数据的边界控制 2.分页的sql语句不方便
2.分页插件pageHelper实现
1.引入依赖 2.配置插件的拦截器 mySql配置文件 sqlMapConfig 的plugins节点 spring的配置文件 ApplicationContext.xml 配置在sessionFactory对象属性中 <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <!--配置拦截器插件的属性 指定数据库方言 合理化分页参数 <value> helperDialect=oracle reasonable=true </value> --> <property name="properties"> <props> <prop key="helperDialect">oracle</prop> <prop key="reasonable">true</prop> </props> </property> </bean> </array> </property> 3.使用插件做分页查询 在dao的查询方法之前调用分页插件的静态方法即可(只会将紧跟着方法分页) public PageInfo<Orders> findAllOrders(Integer pageNum, Integer pageSize){ //在调用查询方法之前开启分页的静态方法 PageHelper.startPage(pageNum,pageSize); //dao的查询方法 List<Orders> list = ordersDao.findAllOrders(); //使用查询的结果初始化框架的分页对象 PageInfo<Orders> pi = new PageInfo<Orders>(list); return pi; } 4.页面提取数据 动态下拉加载数据 <select id="selectSize" class="form-control" οnchange="javascript:toPage();"> function toPage() { //获取到下拉框的值 通过id $("#selectSize").val() var pageSize = document.getElementById("selectSize").value; window.location.href="${pageContext.request.contextPath}/order/findAllOrder?pageNum=1&pageSize="+pageSize; } 遍历导航页码 <c:forEach items="${pageInfo.navigatepageNums}" var="i"> <li><a href="${pageContext.request.contextPath}/order/findAllOrder?pageNum=${i}&pageSize=${pageInfo.pageSize}">${i}</a></li> </c:forEach>
二、springSecurity权限框架的使用
1.权限框架的介绍
springSecurity权限框架两个功能 认证 未登录不允许访问 授权 登录后权限不足不允许访问
2.入门示例
1.引入依赖
2.配置web.xml
1.spring的监听器 加载权限框架的配置文件 2.security权限框架的过滤器执行链 ------------------------------------------------------- <!--监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:security.xml</param-value> </context-param> <!--security的过滤器--> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ------------------------------------------------------------------
3.配置security框架的xml文件
<!--针对拦截的规则配置 auto-config="true 表示应用默认的框架配置 use-expressions="false" 关闭权限框架的表达式 intercept-url 拦截规则 pattern="/** 拦截所有 access="ROLE_USER" 必须拥有ROLE_USER角色的用户可以访问 --> <security:http auto-config="true" use-expressions="false"> <security:intercept-url pattern="/**" access="ROLE_USER"></security:intercept-url> </security:http> <!--验证节点的配置--> <security:authentication-manager> <security:authentication-provider> <!--权限框架默认的业务类--> <security:user-service> <!--初始化测试用的用户 用户名admin 密码 admin 授予角色ROLE_ADMIN 用户名user 密码 user 授予角色ROLE_USER {noop} 表示密码为明文验证123456 密文验证加密后的密码fadfa34fadfa3fadfadjkfads --> <security:user name="admin" password="{noop}admin" authorities="ROLE_ADMIN"></security:user> <security:user name="user" password="{noop}user" authorities="ROLE_USER"></security:user> </security:user-service> </security:authentication-provider> </security:authentication-manager>
3.引入权限框架到项目当中
1.引入依赖 2.配置文件 web.xml listener filter过滤器执行链 <!--spring的监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml,classpath:security.xml</param-value> </context-param> <!--security的过滤器--> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 3.security.xml文件
4.应用自定义的页面配置权限框架
1.登录页面 2.失败页面 3.权限不足页面 --------------------------------------------- <security:form-login login-page="/login.jsp" login-processing-url="/login" default-target-url="/index.jsp" authentication-failure-url="/failer.jsp"></security:form-login> <!--配置权限不足的页面--> <security:access-denied-handler error-page="/403.jsp"></security:access-denied-handler> <!--关闭跨域攻击的拦截--> <security:csrf disabled="true"></security:csrf>
5.使用真实用户验证用户登录
1.初始化用户表 CREATE TABLE sys_user( id NUMBER PRIMARY KEY , username VARCHAR2(50), email VARCHAR2(50) , PASSWORD VARCHAR2(80), phoneNum VARCHAR2(20), STATUS NUMBER(1) ); 2.创建用户的模块 domain dao service 3.在securit配置文件中 更改节点引入自定义的实现类 实现类接口必须继承框架的UserDetaisService <!--验证节点的配置--> <security:authentication-manager> <!--user-service-ref 表示应用一个自定义的用户业务类--> <security:authentication-provider user-service-ref="userService"> </security:authentication-provider> </security:authentication-manager> 4.user实现类必须覆写框架的方法loadUserByName目的得到验证的user对象 @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { //根据用户名查询得到数据库用户对象 SysUser sysUser = userDao.findUserByName(username); //创建集合存储用户拥有的角色权限 List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); //赋值用户角色ROLE_USER用于测试 authorities.add(new SimpleGrantedAuthority("ROLE_USER")); //明文密码验证 User user = new User(sysUser.getUsername(),"{noop}"+sysUser.getPassword(),authorities); return user; }