Example of using jqery to implement simple Ajax

Source: Internet
Author: User

I. Introduction

The Project Manager assigns a job to implement a function using Ajax. I have never touched Ajax before. I only know that jquery provides Ajax interfaces, but I am still familiar with jquery. So now I want to learn how to use jquery to implement Ajax functions.

Ii. Function Overview

The main function of AJAX is to "change the page content without refreshing a new page" or "dynamically refresh the page". I think it should be a concept like a fat client. The current technology is to enrich ie functions and reduce the chance for users to wait for page refresh, including the concept of ActionScript. This is my understanding.

The function I want to implement is very simple, that is, there is a drop-down list, there are three types of books in the list, select one, you can display the details of this type of books, you do not need to refresh the page or convert the page.

Iii. Example

1. database table structure (MySQL database)

1) Table creation statement:

  1. Create
  2. Table catlog
  3. (
  4. Catlogid int (11) not null auto_increment,
  5. Catlogname varchar (20) not null,
  6. Mainbook text,
  7. Supplycompany text,
  8. Amount int (11 ),
  9. Primary key using btree (catlogid)
  10. )
  11. Engine = InnoDB default charset = utf8

2) The data in the table is:

1 computer operating system, Java Development, database technology Beijing book printing company 100
2 ecnomic macro-economy, micro-economy, marketing, people's post and telecommunications press 200
3 Life Fu shen Xinhua Bookstore 300

2. the JSP page is:

  1. <% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %>
  2. <% @ Taglib prefix = "ww" uri = "/webwork" %>
  3. <% String webapp = request. getcontextpath (); %>
  4. <HTML>
  5. <Head>
  6. <Link href = "<% = webapp %>/etc/CSS/common.css" rel = "stylesheet" type = "text/CSS">
  7. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  8. <Script language = "JavaScript" src = "<% = webapp %>/etc/JS/jquery. js"> </SCRIPT>
  9. <Title> online bookstore </title>
  10. <SCRIPT type = "text/JavaScript">
  11. Function setvalue (){
  12. // Document. getelementbyid ("detail"). Style = "visibility: visible ;"
  13. $ ('# List'). Show ();
  14. $ ('# Mainbook'). ATTR ("value", "$ {catalog. mainbook }");
  15. $ ('# Supplycompany'). ATTR ("value", "$ {catalog. supplycompany }");
  16. $ ('# Amount'). ATTR ("value", "$ {catalog. Amount }");
  17. }
  18. Function viewdetail (name ){
  19. $. Getjson (
  20. "Getcataloginfo. Action ",
  21. {Catalogname: name },
  22. Function (MSG ){
  23. $ ('# Mainbook'). ATTR ("value", MSG. mainbook );
  24. $ ('# Supplycompany'). ATTR ("value", MSG. supplycompany );
  25. $ ('# Amount'). ATTR ("value", MSG. amount );
  26. }
  27. );
  28. }
  29. </SCRIPT>
  30. </Head>
  31. <Body>
  32. <Div id = "ajax" class = "list" style = "display: Block">
  33. <Form ID = "frm" Action = "getcataloginfo. Action">
  34. <Table>
  35. <Tr>
  36. <TD> select a book type:
  37. <Select onchange = "viewdetail (this [selectedindex]. Text)">
  38. <WW: iterator id = "cataloglist" value = "cataloglist">
  39. <Option value = "$ {catlogname}" >$ {catlogname} </option>
  40. </WW: iterator>
  41. </SELECT>
  42. </TD>
  43. </Tr>
  44. </Table>
  45. <Table>
  46. <Tr> <TD> main books: <input id = "mainbook" type = "text" value = ""/> </TD> </tr>
  47. <Tr> <TD> major suppliers: <input id = "supplycompany" type = "text" value = ""/> </TD> </tr>
  48. <Tr> <TD> Number of Books owned by this bookstore: <input id = "amount" type = "text" value = ""/> </TD> </tr>
  49. </Table>
  50. </Form>
  51. </Div>
  52. </Body>
  53. </Html>

3. Action Code

  1. Public String getcataloginfo (){
  2. Catalog = icatalogservice. getcatlogbyname (catalogname );
  3. Jsonobject JSON = new jsonobject ();
  4. Try {
  5. JSON. Put ("mainbook", catalog. getmainbook ());
  6. JSON. Put ("supplycompany", catalog. getsupplycompany ());
  7. JSON. Put ("amount", catalog. getamount ());
  8. } Catch (jsonexception e ){
  9. E. printstacktrace ();
  10. Return Error;
  11. }
  12. Catalogname = JSON. tostring ();
  13. Return success;
  14. }

4. Service Layer Code:

  1. Public catlog getcatlogbyname (string name ){
  2. Return icatalogdao. getcatlog (name );
  3. }

 

5. pojo

  1. Package CN. resoft. cm. VO;
  2. Import java. util. hashset;
  3. Import java. util. Set;
  4. Public class catlog implements java. Io. serializable {
  5. Private integer catlogid;
  6. Private string catlogname;
  7. Private string mainbook;
  8. Private string supplycompany;
  9. Private integer amount;
  10. Private set books = new hashset (0 );
  11. Public catlog (){
  12. }
  13. Public catlog (string catlogname ){
  14. This. catlogname = catlogname;
  15. }
  16. Public catlog (string catlogname, set books ){
  17. This. catlogname = catlogname;
  18. This. Books = books;
  19. }
  20. Public integer getcatlogid (){
  21. Return this. catlogid;
  22. }
  23. Public void setcatlogid (integer catlogid ){
  24. This. catlogid = catlogid;
  25. }
  26. Public String getcatlogname (){
  27. Return this. catlogname;
  28. }
  29. Public void setcatlogname (string catlogname ){
  30. This. catlogname = catlogname;
  31. }
  32. Public set getbooks (){
  33. Return this. books;
  34. }
  35. Public void setbooks (set books ){
  36. This. Books = books;
  37. }
  38. Public String getmainbook (){
  39. Return mainbook;
  40. }
  41. Public void setmainbook (string mainbook ){
  42. This. mainbook = mainbook;
  43. }
  44. Public String getsupplycompany (){
  45. Return supplycompany;
  46. }
  47. Public void setsupplycompany (string supplycompany ){
  48. This. supplycompany = supplycompany;
  49. }
  50. Public integer getamount (){
  51. Return amount;
  52. }
  53. Public void setamount (integer amount ){
  54. This. Amount = amount;
  55. }
  56. }

 

 

Well, this function is implemented.

Related Article

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.