旅游线路收藏_判断是否收藏
当页面加载完成后,发送ajax请求,获取用户是否收藏的标记
根据标记,展示不同的按钮样式
分析:
后台代码
创建 FavoriteDao 接口
public interface FavoriteDao { /** * 根据rid和uid查询收藏信息 * @param rid * @param uid * @return */ public Favorite findByRidAndUid(int rid, int uid); }
创建实现类进行实现 FavoriteDaoImpl
public class FavoriteDaoImpl implements FavoriteDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @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; } }
创建 FavoriteServlet 接口
public interface FavoriteServlet { /** * 判断是否收藏 * @param rid * @param uid * @return */ public boolean isFavorite(String rid,int uid); }
创建实现类进行实现 FavoriteServletImpl
public class FavoriteServletImpl implements FavoriteServlet { private FavoriteDao favoriteDao = new FavoriteDaoImpl(); @Override public boolean isFavorite(String rid, int uid) { Favorite favorite = favoriteDao.findByRidAndUid(Integer.parseInt(rid), uid); return favorite!=null;//如果对象有值则为true,反之则为false } }
RouteServlet:进行添加
/** * 判断当前登录用户是否收藏过该线路 * @param request * @param response * @throws ServletException * @throws IOException */ public void isFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取线路id String rid = request.getParameter("rid"); //获取当前登录的用户user User user = (User) request.getSession().getAttribute("user"); int uid;//用户id if (user==null){ //用户尚未登录 uid=0; }else { //用户已经登录 uid = user.getUid(); } //调用FavoriteService查询是否收藏 boolean flag = favoriteServlet.isFavorite(rid, uid); //写回客户端 writeValue(flag,response); }
前台代码
在下面 添加一个函数
$(function () { //发送请求,判断用户是否收藏过该线路 var rid = getParameter("rid"); $.get("route/isFavorite",{rid:rid},function (flag) { if (flag){ //用户已经收藏过 //class="btn already" disabled="disabled" //设置收藏按钮样式 $("#favorite").add("already"); // $("#favorite").prop("disabled",disabled); $("#favorite").attr("disabled", "disabled")
//删除按钮的点击事件
$("#function").removeAttr("onclick"); }else { //用户没有收藏 } }); });
收藏次数动态展示
在FavoriteDao接口中添加
/** * 根据rid查询收藏次数 * @param rid * @return */ int findCountByRid(int rid);
在这个实现类实现方法 FavoriteDaoImpl
@Override public int findCountByRid(int rid) { String sql = "select count(*) from tab_favorite where rid = ?"; return template.queryForObject(sql,Integer.class,rid); }
在RouteServiceImpl 中编写
private FavoriteDao favoriteDao = new FavoriteDaoImpl();
@Override public Route findOne(String rid) { //根据id去route表中查询route对象 Route route = routeDao.findOne(Integer.parseInt(rid)); //根据route的id查询图片集合信息 List<RouteImg> routeImgList = routeImgDao.findByRid(route.getRid()); //将集合设置到route对象 route.setRouteImgList(routeImgList); //根据route的id查询卖家的信息 Seller seller = sellerDao.findById(route.getSid()); route.setSeller(seller); //查询收藏次数 int count = favoriteDao.findCountByRid(route.getRid()); route.setCount(count); return route; }
route_detail.html:
标签:uid,int,route,收藏,public,旅游网,rid,黑马 From: https://www.cnblogs.com/yu3304/p/17140187.html