Ajax allows you to add Forum sections to favorites and return prompt information asynchronously.

Source: Internet
Author: User

Ajax allows you to add Forum sections to favorites and return prompt information asynchronously.

I. Description

On the forum page, you can click this hyperlink to add the forum to favorites. However, to prevent repeated collections and collection errors without logon, we use Ajax to return the favorites prompt asynchronously.

 

II. Implementation Details

1. Click "add to Favorites" on the jsp page to execute the method in its onclick event, instead of redirecting to a new page.

2. Use Ajax technology in the boardCollect. js file to perform operations on favorite sections in the background and asynchronously write back the prompt information.

3. display the corresponding prompt control on the page based on the returned prompt information.

 

Iii. Source Code

1. boardCollect. js

 

// Create a request var http_request; function createXMLHttpRequest () {http_request = false; if (window. XMLHttpRequest) {http_request = new XMLHttpRequest (); // initialize http_requestif (http_request.overrideMimeType) {http_request.overrideMimeType (text/html);} else if (window. activeXObject) {try {http_request = new ActiveXObject (Msxml2.XMLHTTP); // create an XMLHttpRequest object in IE, new version IE} catch (e) {try {http_request = new ActiveXObject (Microsoft. XML HTTP); // create an old version of the XMLHttpRequest object IE} catch (e) {}} if (! Http_request) {window. alert (you cannot create an XMLHttpRequest object instance); return false ;}} function boardCollect (id) {showCollects (boardCollect. action? BoardId = + id);} function showCollects (url) {// call createXMLHttpRequest to create http_requestcreateXMLHttpRequest (); // The onreadystatechange status changes and the processRequest method is returned to process response = processRequest; http_request.open (GET, url, true); http_request.send (null);} // function for processing returned information processRequest () {if (http_request.readyState = 4) {if (http_request.status = 200) {// returns normally. Obtain the returned responseText data var com = eval ('+ http_request.responseText +'); var feedback = com. feedback; if (feedback = true) {// successfully added the document to the favorites folder. getElementById (ntcwin ). style. display = block; function remove () {document. getElementById (ntcwin ). style. display = none;} // setInterval (remove, 1.5) disappears after 1500 seconds of display;} else if (feedback = already collect) {// already added to the Favorites list and cannot be added to the Favorites list, display the text in the document. getElementById (fwin_a_favorite ). style. display = block;} else {// Add to favorites without logon. Modify the error message document displayed in errorInfo on the page. getElementById (errorInfo ). innerHTML = sorry, you do not have this permission for Logon. Please log on and try again; document. getElementById (fwin_a_favorite ). style. display = block ;}}}// hide the divfunction hideWindow (tcl) {document. getElementById (tcl ). style. display = none ;}

 

2. board. jsp (some css code is not uploaded, and the display format may be incorrect)

 

<%@ page language=java pageEncoding=UTF-8%><%@ taglib uri=/struts-tags prefix=s%><%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=c%><%@ taglib uri=http://java.sun.com/jsp/jstl/fmt prefix=fmt%><%String path = request.getContextPath();String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;%>

 

3. Back-end processing of the java source code of the favorite Forum

 

/***** Add to favorites */public String boardCollect () {System. out. println (enter boardCollect try boardId = + boardId); try {HttpServletResponse response; response = ServletActionContext. getResponse (); response. setContentType (text/json; charset = UTF-8); response. setHeader (Cache-Control, no-cache); response. setHeader (Pargma, no-cache); PrintWriter out; out = response. getWriter (); // mcpForumPost = mcpForumPostDAO. findByI D (postId); JSONObject js = new JSONObject (); ScpnUser u = (ScpnUser) this. getSession (). get (user); if (u! = Null) {// determine whether the user has added the String exitBoardCollect = select count (*) FROM mcp_forum_board_collect where user_id = + u. getUserId () + and board_id = + boardId; System. out. println (exitBoardCollect: + exitBoardCollect); Integer count = jdbcTemplate. queryForInt (exitBoardCollect); if (count> 0) {js. put (feedback, already collect);} else {Timestamp currentTime = new Timestamp (System. currentTimeMillis (); SimpleDateFormat sdf = new SimpleDateFormat (yyyy-MM-dd HH: mm: ss); String time = sdf. format (currentTime); // INSERT the favorites record INTO the Forum favorites table String insertBoardCollect = insert into mcp_forum_board_collect (board_id, user_id, board_collect_time) VALUES (+ boardId +, + u. getUserId () +, '+ time +'); System. out. println (insertPostCollect: + insertBoardCollect); jdbcTemplate. update (insertBoardCollect); // UPDATE the number of collections in the Forum String updateColNum = update mcp_forum_board SET board_collectNum = 1 where board_id = + boardId; System. out. println (updateColNum: + updateColNum); jdbcTemplate. update (updateColNum); userLogger. insert (UserLogger. UPDATE, u. getAccount () + Add to favorites); js. put (feedback, true) ;}} else {js. put (feedback, not login);} out. write (js. toString (); out. close ();} catch (IOException e) {e. printStackTrace ();} catch (JSONException e) {e. printStackTrace ();} return null ;}


 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.