分析
后台代码
RouteServlet类:
/** * 添加收藏 * @param request * @param response * @throws ServletException * @throws IOException */ 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; if (user == null){ //用户未登录 return; }else { //用户已经登录 uid = user.getUid(); } //3、调用service添加 favoriteService.add(rid,uid); }
FavoriteService接口:
/** * 添加收藏 * @param rid * @param uid */ void add(String rid, int uid);
FavoriteServiceImpl实现类:
@Override public void add(String rid, int uid) { favoriteDao.add(Integer.parseInt(rid),uid); }
FavoriteDao接口:
void add(int rid, int uid);
FavoriteDaoImpl实现类:
@Override public void add(int rid, int uid) { String sql = "insert into tab_favorite values (?,?,?)"; template.update(sql,rid,new Date(),uid); }
前台代码
route_detail.html页面:
//点击收藏按钮触发方法 function addFavorite() { var rid = getParameter("rid"); $.get("user/findOne",{},function (user) { if (user){ //用户登录了 //添加功能 $.get("route/addFavorite",{rid:rid},function () { }) }else { //用户没有登录 alert("您尚未登陆,请登录"); location.href = "http://localhost/travel/login.html"; } }); }
前台效果优化
route_detail.html页面:
$(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"); $.get("user/findOne",{},function (user) { if (user){ //用户登录了 //添加功能 $.get("route/addFavorite",{rid:rid},function () { //代码刷新页面 location.reload(); }) }else { //用户没有登录 alert("您尚未登陆,请登录"); location.href = "http://localhost/travel/login.html"; } }); }
标签:function,uid,int,收藏,点击,user,按钮,rid From: https://www.cnblogs.com/xjw12345/p/16633497.html