Struts 2 Jquery implements AJAX functionality

Source: Internet
Author: User
Tags eval json
I use the Myeclips Tutorial e tool with the function of import jar, the default this jsonplugin has been imported, so only need to see in MyEclipse, is not already imported this package, if already have, then do not have to import.
Okay, back to the point. Last time we said this was to get a value, what if it was an object? What about a collection?
  
   
   
      
       
       
public class Book

{
Private String BookName;
Private String author;

The getter and setter of these two member variables are omitted here
Public book () {}
Public book (String bookname,string author)

{
This.bookname=bookname;
This.author=author;
}

}

Action Content
  
   
   
public class Testaction extends Actionsupport
{
Private book book;
Private list<book> Booklist;

Public String Execute ()
{
Booklist=new arraylist<book> ();
Book=new book ();
Book.setbookname ("30 days");
Book.setauthor ("Blak");

List.add (New book ("World First, etc", "yes yes");
List.add (new book ("Always Quiet", "Asan"));
List.add (new book ("Struts 2", "Me"));
}

}
Struts.xml
 
   
   
<package name= "ajax" extends= "Json-default" >
<actio n name= "Testjson" "class=" action. Testjsonaction ">
<result type=" JSON ">result</result>
</action>

& lt;/package>
page content
  
  
<title>testJson</title>
<script type= "Tex xt/Web Effects "src=" Js/jquery.js "></script>
<script type=" texxt/javascript ">
function Testjson () br>{
$.ajax ({
Type: "Post",
URL: "Testjson.action",
data:{},
DataType: "JSON",
Beforesend : function () {},
Success:function (JSON) {
Alert ("Success complete");
Alert ("BookName:" +json.book.bookname+ "Author:" +json.book.author);
var json=eval (json.booklist);
JSON = eval (json.list)
for (var i=0; i<json.length; i++)
{
Alert (json[i].bookname+ "" + json[i].au Thor)
}

}
});
}
</script>
<body>
This are my page!<br/>

<input type= button "value=" Test Ajax "onclick=" Testjson (); "/>
</body>
To access an object, use JSON directly. The name of the field.
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.