Struts2 + JSON + Ajax

Source: Internet
Author: User

Implement instant communication between the foreground and the background.

1. It is best to pass the action in the URL in $. Ajax by the parameter.

Front-end onload = "load ('$ (cardaction )')";

<S: URL action = "cardaction" namespace = "/examjson" includeparams = "NONE" id = "cardaction"/>

JScript
Function Load (_ URL ){
$. Ajax ({
Type: " Post " ,
URL: _ URL,
Data: " Name = " + Name,
Error: Function () {Alert ( " Error " )},
Success: Function (MSG ){
Eval ( " Res = " + MSG );
VaR Cards = Res. cards;
For ( VaR I =   0 ; I < Cards. length; I ++ ){
$ ( " # Cardselect " ). Append ( " <Option value = " + Cards [I] + " > " + Cards [I] + " </Option> " )
}
}
});
}

 

 

2. Prepare struts. xml
< Package Name = "Jsonpack" Extends = "JSON-Default" Namespace = "/Examjson" >
<! -- Get card -->
< Action Name = "Cardaction" Class = "Cardaction" Method = "Cards" >
< Result Type = "JSON" >
< Param Name = "Excludeproperties" > Employeeservice </ Param >
</ Result >
</ Action >
  </ Package >  
Note: employeeservice is the object to be injected in cardaction. 3. applicationcontext. xml
< Bean ID = "Addemployeeaction" Class = "Com. ambow. examonlinesystem. entity. Exam. Action. addemployeeaction" >
< Property >
< Ref Bean = "Employee" />
</ Property >
</ Bean > Java
Package Com. ambow. examonlinesystem. entity. Exam. Action;

ImportCom. ambow. examonlinesystem. entity. Exam. Interfaces. iemployee;
ImportCom. opensymphony. xwork2.actionsupport;

Public   Class Cardaction Extends Actionsupport {
  Private   Static   Final   Long Serialversionuid =   163236660018615361l ;
  Private String name;
  Private Iemployee employeeservice;
String [] cards;

 PublicString [] getcards (){
ReturnCards;
}

 Public VoidSetcards (string [] cards ){
This. Cards=Cards;
}

 Public VoidSetemployeeservice (iemployee employeeservice ){
This. Employeeservice=Employeeservice;
}

Public   Void Setname (string name ){
This . Name = Name;
}
 
  Public String cards (){
String [] Str = Employeeservice. getcards (name );
Cards =   New String [Str. Length];
Cards = STR;
Return Actionsupport. success;
}
}
See the above cards for the data that can be obtained. The obtained method is in the load () method of the JS file.
You forgot to add jsonplugin. - 0.30 The. jar package .....

 

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.