页面阅读量点击加一功能(js+java)

基于核格平台的点击量加一功能:
页面html部分:
 
   
  1. class="operate_div">
    • 来源:%{tableTr.source}
    • 作者:%{tableTr.author}
    • 审核:%{tableTr.news_check}
    • 日期:%{tableTr.creation_date}
    • 点击数:%{tableTr.click_num+1}
注:①click_num是数据库中点击量的字段,给click_num先加一是为了加上当前阅读人的点击量;
       ②在页面中应当引用相应的js文件。

js部分代码:
 
   
  1. $(function(){
  2. var location = window.location.search;
  3. var index = location.indexOf("=");
  4. var id = location.slice(index+1);
  5. $.ajax({
  6. url:'/CDKQ/loadClickNum.svt',
  7. type:'post',
  8. data:{"cdkql_news_id":id},
  9. success:function(){
  10. }
  11. });
  12. })

java部分代码( loadClickNumServlet.java ):
 
   
  1. import java.io.IOException;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.hibernate.SQLQuery;
  9. import com.sunsheen.jfids.system.base.composite.data.query.QueryParameterImpl;
  10. import com.sunsheen.jfids.system.database.IdsSession;
  11. import com.sunsheen.jfids.system.servlet.Servlet;
  12. import com.sunsheen.jfids.util.IdsDataBaseUtil;
  13. @SuppressWarnings("serial")
  14. @Servlet(value="/loadClickNum.svt", anonymous=true)
  15. public class loadClickNumServlet extends HttpServlet {
  16. /*该servlet用于浏览量的增加*/
  17. private static final long serialVersionUID = 1L;
  18. @Override
  19. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  20. throws ServletException, IOException {
  21. //获取前台参数
  22. String cdkql_news_id = req.getParameter("cdkql_news_id");
  23. //初始化sql变量
  24. String sql = null;
  25. //将前台参数保存在map中
  26. Map<String, String> map = new HashMap<String, String>();
  27. map.put("cdkql_news_id", cdkql_news_id);
  28. sql ="update cdkql_news set click_num=click_num+1 WHERE cdkql_news_id=:cdkql_news_id";//增加浏览量
  29. IdsSession session = IdsDataBaseUtil.getHibernateSession();
  30. try {
  31. session.beginTransaction();//开启事务
  32. SQLQuery query = session.createSQLQuery(sql);
  33. new QueryParameterImpl().initParameter(query, map);
  34. query.executeUpdate();
  35. session.commit();
  36. } catch (Exception e) {
  37. session.rollback();//回到初始状态
  38. } finally{
  39. session.close();//关闭事务
  40. }
  41. }
  42. @Override
  43. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  44. throws ServletException, IOException {
  45. doGet(req, resp);
  46. }
  47. }
注: 平台的版本不同相应的创建session名字不同。

你可能感兴趣的:(web前端小知识)