案例中的数据库表格有
- 产品表
- 订单表
- 会员表
- 旅客表
- 用户表
- 角色表
- 资源权限表
其中订单表
- 多个订单对应一个会员(导游)
- 一个订单对应一个产品
- 一个订单可以对应多个旅客,一个旅客可以对应多个订单,多对多的关系
PageHelper
从侧栏访问订单
- page,pageSize属性
</a></li>
<li id="system-setting"><a
href="${pageContext.request.contextPath}/orders/findAll.do?page=1&pageSize=3"> <i
class="fa fa-circle-o"></i> 订单管理
</a></li>
控制类,配置分页器
每一页的PageInfo都有以下值
- pageSize
- pages
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "size", required = true, defaultValue = "4") int size) throws Exception {
ModelAndView mv = new ModelAndView();
List<Orders> ordersList = ordersService.findAll(page, size);
//PageInfo就是一个分页Bean
PageInfo pageInfo = new PageInfo(ordersList);
mv.addObject("pageInfo", pageInfo);
mv.setViewName("orders-page-list");
return mv;
}
Service里调用
@Override
public List<Orders> findAll(int page, int size) throws Exception {
//参数pageNum 是页码值 参数pageSize 代表是每页显示条数
PageHelper.startPage(page, size);
return ordersDao.findAll();
}
订单展示
- 在order-page-list里
<div class="box-footer">
<div class="pull-left">
<div class="form-group form-inline">
总共2 页,共14 条数据。 每页
<select class="form-control" id="changePageSize" onchange="changePageSize()">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select> 条
</div>
</div>
<div class="box-tools pull-right">
<ul class="pagination">
<li>
<a href="${pageContext.request.contextPath}/orders/findAll.do?page=1&size=${pageInfo.pageSize}" aria-label="Previous">首页</a>
</li>
<li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pageNum-1}&size=${pageInfo.pageSize}">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.pages}" var="pageNum">
//var="pageNum"
<li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageNum}&size=${pageInfo.pageSize}">${pageNum}</a></li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pageNum+1}&size=${pageInfo.pageSize}">下一页</a></li>
<li>
<a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pages}&size=${pageInfo.pageSize}" aria-label="Next">尾页</a>
</li>
</ul>
</div>
</div>
script
- 下拉选择,一页展示多少条数据
function changePageSize() {
//获取下拉框的值
var pageSize = $("#changePageSize").val();
//向服务器发送请求,改变没页显示条数
location.href = "${pageContext.request.contextPath}/orders/findAll.do?page=1&size="
+ pageSize;
}
用户表、角色表
SpringSecurity
- 使用数据库去登录
- SpringSecurity.xml文件配置登录,登录成功、失败页面信息
- 配置userService类,利用@Service(“userService”)注解跳转,该类的作用是验证授权
<security:form-login
login-page="/login.jsp"
login-processing-url="/login.do"
default-target-url="/index.jsp"
authentication-failure-url="/failer.jsp"
authentication-success-forward-url="/pages/main.jsp"
/>
- 通过中转login.do,授权成功去到/pages/main.jsp,失败去到/failer.jsp
授权配置
<!-- 切换成数据库中的用户名和密码 -->
<security:authentication-manager>
<security:authentication-provider user-service-ref="userService">
<!-- 配置加密的方式 -->
<!-- <security:password-encoder ref="passwordEncoder"/>-->
</security:authentication-provider>
</security:authentication-manager>
接口
public interface IUserService extends UserDetailsService {
}
类
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
UserInfo userInfo=null;
//通过s=username找到userInfo
try {
userInfo=userDao.findByUsername(s);
} catch (Exception e) {
e.printStackTrace();
}
//处理自己用户对象封装成UserDetails
//第5个参数是权限
//返回值时会做密码,权限的检验
User user=new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(),userInfo.getStatus()==0?false:true,true,true,true,getAuthority(userInfo.getRoles()));
return user;
}
public List<SimpleGrantedAuthority> getAuthority(List<Role> roles){
List<SimpleGrantedAuthority> list=new ArrayList<>();
for(Role role:roles)
{
list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
}
return list;
}
}