功能说明
在用户登录的情况下,点击收藏之后会将线路添加到用户收藏中
功能分析
- 在route_detail.html加载完成之后会触发isFavorite,查找是否已收藏,并根据回调数据修改收藏按钮
- 给收藏按钮添加单击事件,触发favorite函数。
- 在函数内发送Ajax请求,携带rid,并在回调函数中判断返回值,做出响应。
- FavoriteServlet中获取session中用户数据,如果为null,则直接设置ResultInfo中flag为false,写回数据;如果有值,则获取rid,调用service,携带rid和uid
- 在FavoriteService中船舰Date对象,并使用SimpleDateFormat对象设置日期格式,调用dao方法,存入数据
代码实现
前端
function isFavorite(rid){
$.get("route/isFavorite",{
rid:rid},function(info){
if(!info.flag){
$("#favorite").html('<a class="btn" οnclick="favorite('+rid+')"><i class="glyphicon glyphicon-heart-empty"></i>点击收藏</a>');
}else{
$("#favorite").html('<a class="btn already" disabled="disabled"><i class="glyphicon glyphicon-heart-empty"></i>点击收藏</a>');
}
});
}
function favorite(rid){
$.get("favorite/addFavorite",{
rid:rid},function(info){
if(info.flag){
location.reload();
}else{
location.href="login.html";
}
});
}
Servlet
/**
* 查看是否已收藏
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
public void isFavorite(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
String ridstr = req.getParameter("rid");
int rid = Integer.parseInt(ridstr);
ResultInfo info = new ResultInfo();
User user = (User)req.getSession().getAttribute("user");
if(user == null){
info.setFlag(false);
writeValue(resp,info);
return;
}
Favorite favorite = routeService.findFavoriteByUid(rid,user.getUid());
if(favorite == null){
info.setFlag(false);
writeValue(resp,info);
return;
}
info.setFlag(true);
writeValue(resp,info);
}
@WebServlet("/favorite/*")
public class FavoriteServlet extends BaseServlet {
private FavoriteService favoriteService = new FavoriteServiceImpl();
public void addFavorite(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user = (User) req.getSession().getAttribute("user");
ResultInfo info = new ResultInfo();
if(user == null){
System.out.println("ppx");
info.setFlag(false);
writeValue(resp,info);
return;
}
String ridstr = req.getParameter("rid");
int rid = Integer.parseInt(ridstr);
favoriteService.addFavorite(rid,user.getUid());
info.setFlag(true);
writeValue(resp,info);
}
}
Service
/**
* 根据rid和uid查询收藏
* @param rid
* @param uid
* @return
*/
@Override
public Favorite findFavoriteByUid(int rid,int uid) {
Favorite favorite = favoriteDao.findFavoriteByUid(rid,uid);
return favorite;
}
public class FavoriteServiceImpl implements FavoriteService {
private FavoriteDao favoriteDao = new FavoriteDaoImpl();
/**
* 添加收藏
* @param rid
* @param uid
*/
@Override
public void addFavorite(int rid,int uid) {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
String simpleDate = dateFormat.format(date);
favoriteDao.addFavorite(rid,simpleDate,uid);
}
}
Dao
public class FavoriteDaoImpl implements FavoriteDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* 根据rid和uid查询收藏
* @param rid
* @param uid
* @return
*/
@Override
public Favorite findFavoriteByUid(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(Exception e){
e.printStackTrace();
}
return favorite;
}
@Override
public void addFavorite(int rid, String date, int uid) {
String sql = "insert into tab_favorite values(?,?,?)";
template.update(sql, rid, date, uid);
}
}
完成