《黑马旅游网》综合案例十 旅游线路收藏
《黑马旅游网》综合案例十 旅游线路收藏
1.旅游线路收藏功能分析
判断当前登录用户是否收藏过该线路
当页面加载完成后,发送ajax请求,获取用户是否收藏的标记
根据标记,展示不同的按钮样式
public void isFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 获取线路id String rid = request.getParameter("rid"); //2. 获取当前登录的用户 user User user = (User) request.getSession().getAttribute("user"); int uid;//用户id if(user == null){ //用户尚未登录 uid = 0; }else{ //用户已经登录 uid = user.getUid(); } //3. 调用FavoriteService查询是否收藏 boolean flag = favoriteService.isFavorite(rid, uid); //4. 写回客户端 writeValue(flag,response); }
@Override public boolean isFavorite(String rid, int uid) { Favorite favorite = favoriteDao.findByRidAndUid(Integer.parseInt(rid), uid); return favorite != null;//如果对象有值,则为true,反之,则为false }
@Override public Favorite findByRidAndUid(int rid, int uid) { Favorite favorite = null; try { String sql = " select * from tab_favorite where rid = ? and uid = ?"; favorite = template.queryForObject(sql, new BeanPropertyRowMapper<Favorite>(Favorite.class), rid, uid); } catch (DataAccessException e) { e.printStackTrace(); } return favorite; }
$(function () { // 发送请求,判断用户是否收藏过该线路 var rid = getParameter("rid"); $.get("route/isFavorite",{rid:rid},function (flag) { if(flag){ // 用户已经收藏过 //<a class="btn already" disabled="disabled"> //设置收藏按钮的样式 $("#favorite").addClass("already"); $("#favorite").prop("disabled",disabled); }else{ // 用户没有收藏 } });
//设置收藏次数 $("#favoriteCount").html("已收藏"+route.count+"次");
//4. 查询收藏次数 int count = favoriteDao.findCountByRid(route.getRid()); route.setCount(count);
@Override public int findCountByRid(int rid) { String sql = "SELECT COUNT(*) FROM tab_favorite WHERE rid = ?"; return template.queryForObject(sql,Integer.class,rid); }
1.点击按钮收藏线路
编码 前台代码 $(function () { // 发送请求,判断用户是否收藏过该线路 var rid = getParameter("rid"); $.get("route/isFavorite",{rid:rid},function (flag) { if(flag){ // 用户已经收藏过 //<a class="btn already" disabled="disabled"> //设置收藏按钮的样式 $("#favorite").addClass("already"); $("#favorite").attr("disabled","disabled"); //删除按钮的点击事件 $("#favorite").removeAttr("onclick"); }else{ // 用户没有收藏 } }); }); //点击收藏按钮触发的方法 function addFavorite(){ var rid = getParameter("rid"); //1. 判断用户是否登录 $.get("user/findOne",{},function (user) { if(user){ //用户登录了 //添加功能 $.get("route/addFavorite",{rid:rid},function () { //代码刷新页面 location.reload(); }); }else{ //用户没有登录 alert("您尚未登录,请登录"); location.href="http://localhost/travel/login.html"; } }) } 后台代码 RouteServlet public void addFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 获取线路rid String rid = request.getParameter("rid"); //2. 获取当前登录的用户 User user = (User) request.getSession().getAttribute("user"); int uid;//用户id if(user == null){ //用户尚未登录 return ; }else{ //用户已经登录 uid = user.getUid(); } //3. 调用service添加 favoriteService.add(rid,uid); } FavoriteService @Override public void add(String rid, int uid) { favoriteDao.add(Integer.parseInt(rid),uid); } FavoriteDao @Override public void add(int rid, int uid) { String sql = "insert into tab_favorite values(?,?,?)"; template.update(sql,rid,new Date(),uid); }