Java-EE jquery page principle and implementation

Source: Internet
Author: User


It workers in Web applications must be familiar with pagination, but can we implement pagination in some special circumstances? ". Example one: The page has video playback and comments, we want to see the latest comments, we have to refresh the page, so the video will reload and play, this is not what we want, we need to use AJAX technology, dynamic access to server data, in order to solve this problem, Using jquery to do this, jquery is actually a further encapsulation of JavaScript, a better ability to manipulate DOM objects, and a more advanced technology than JavaScript, which is more convenient and concise with Ajax. The next step is to try to use jquery to implement dynamic paging.

Suppose: The page reads as follows

<table id= "TBL" >
     <tr>
              <td class= "user" style= "color:red"; > User name </td>
              <td style= "Color:green;" > Password </td>
 
              <!--use sturts1 tags  -->
              <bean:define id= "Listbean" name= "list"
     	      /> <logic:present name= "Listbean" >
		      <logic:iterate id= "item" name= "Listbean" >
			      <tr>
				 < !--table with styles and attributes  -->
				 <td style= "text-align:right;" >${item.userName}</td>
				 <td><a href= "index.jsp" target= "_blank" >${item.password}</a ></td>

				 <input type= "hidden" class= "token"/>
			      </tr>
		      </logic:iterate>
	      </logic:present>
              
</table>

Page content is two columns n rows of the table, first listed as the user name, the second column password, loop shows a page can show the most

Need to explain, which hides the role of text field token, later explained

The following is the jquery code

Execute
		function Page_go ()
		{
			var pagenow = $ ("#txtPageNow"). Val ();
			var pageSize = $ ("#txtPageSize"). Val ();
			$.ajax ({
                             URL: "List.action",
                             data: "method=setpagesize&pagenow=" +pagenow+ "&pagesize=" +pagesize,
                             type: "Post",
                             success:function (msg) {
               	              $ (". Token"). Parent (). Parent (). remove ();
               	              $ ("#tbl"). Append (msgs);
                             }
                           );
		

When the user clicks on the next or previous page, the method is triggered, the current page number and the page size are called and the action is requested as a parameter.

When the request succeeds, the server returns the data, and the token hidden text field plays a crucial role in helping us locate every piece of data that is already on the page, and we pass $ (". Token"). Parent (). Parent () remove (); You can delete all TR except the first tr. $ (". Token") gets all DOM objects that reference the token class style, and then we delete his parent node <td> parent node <tr>, so that the existing data is clear, $ ("#tbl"). Append (msgs); Then the server responds to the information appended to the ID of TBL, but the server will respond to what kind of information. Please see:

	/**
	 * jquery pagination
	 /public
	Actionforward setpagesize (actionmapping mapping, actionform form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	

		//dao layer query list return to action
		List List =petlistdao.getresult (Pagenow, pageSize);

		StringBuffer buffer = new StringBuffer ();
		For (object object:list) {
			Userinfo user = (Userinfo) Object;

			Buffer.append ("<tr>");
			Buffer.append ("<!--table--> with styles and attributes  ");
			Buffer.append ("<td style=\" text-align:right;\ ">"). Append (User.getusername). Append ("</td>")
			Buffer.append ("<td><a href=\" index.jsp\ "target=\" _blank\ ">"). Append (User.getpassword). Append (</ td>);
			Buffer.append ("<input type=\" hidden\ "class=\" token\ "/>");
			Buffer.append ("</tr>");
		}

		PrintWriter out = Response.getwriter ();
		Out.print (Buffer.tostring ());
		Out.flush ();
		Out.close ();
		return null;
	}


In the action, directly return the data tag together to the jquery callback function. 、

This enables the jquery paging, no refreshing effect on the client, greatly increasing the customer experience, very good

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.