[Javaweb Basics] Introduction to 017.STRUTS2 and Ajax interactions

Source: Internet
Author: User

In the development of Web pages, in order to make real-time updates to only one chunk of the page, and not to refresh other content that does not need to be updated, to improve responsiveness and save traffic , we used the page's Asynchronous refresh Technology Ajax, then our STRUTS2 framework in this aspect is how to achieve, in fact, is also very simple, below I take everyone to understand next.

1. First we need to import two dependency packages json-lib and Struts2-json-plugin, which is the parsing package for JSON support.

2. We need to create an action class that is the same as the others

Package Com.babybus.sdteam.action;import Java.util.list;import Com.babybus.sdteam.bo.manageservlet;import Com.babybus.sdteam.vo.student;import Com.opensymphony.xwork2.action;import Com.opensymphony.xwork2.ActionSupport ;p ublic class Queyuserajaxaction extends Actionsupport implements Action {/** * */private static final long serialversion UID = 1l;private string searchname;private list<student> resultlist;public string Getsearchname () {return Searchname;} public void Setsearchname (String searchname) {this.searchname = Searchname;} Public list<student> getresultlist () {return resultlist;} public void Setresultlist (list<student> resultlist) {this.resultlist = resultlist;} Public String Loaduserinfo () throws Exception {Student querystudent = new Student (); Querystudent.setstudentname ( Searchname); Manageservlet Manageservlet = new Manageservlet ();//query result set this.resultlist = Manageservlet.getstudentbycondition (  querystudent); return "UserInfo"; }}

2. Configuration struts.xml, the first thing to note is that the package extends to change to Json-default, the return of the result if JSON -formatted

<package name= "MyWebProject1" namespace= "/" extends= "json-default" >     <action name= " Queyuserajax "class=" com.babybus.sdteam.action.QueyUserAjaxAction "method=" Loaduserinfo ">        type=" JSON " ></result>    </action> </package>

3. Ajax calling method for front-end pages

function Queryuserinfo () {var queryname = $ ("#searchText"). Val (); $.Ajax({URL: ' Queyuserajax.action ',type: ' Post ',Data: "Searchname=" + queryname,DataType: ' JSON ',Success: function (data) {var innerHTML = "<tr><th>uid</th><th> Student name </th><th>                Age </th><th> Class </th><th> operation </th></tr> "; Ifdata.resultlist && data.resultList.length > 0) {$ ("#resultTable"). HTML ("");                      $ (data.resultlist). Each (function (i, value) {innerHTML + = "<tr>";                      innerHTML + = "<td>" + value.id + "</td>";                      innerHTML + = "<td>" + value.studentname+ "</td>";                      innerHTML + = "<td>" + value.age+ "</td>";                      innerHTML + = "<td>" + value.classname+ "</td>";                      innerHTML + = "<td><a href= ' adduser.jsp?id=" + value.id + "' > Modify </a>|"; innerHTML + = "<a href= ' mangage?method=del&userid=" + value.id + "' onclick= ' return confirm (' OK to delete?                      ') '}> delete </a></td> ';                    innerHTML + = "</tr>";                 });          } $ ("#resultTable"). HTML (innerHTML);   }        }); }

4. From the above steps we can complete the interaction between Ajax and Struts2.

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )

Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4805790.html

[Javaweb Basics] Introduction to 017.STRUTS2 and Ajax interactions

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.