For example, the model class is as follows: Teacher, Public class teacher {
Private integer ID;
Priavte string name;
Private School;
}
And school, public class school {
Private integer ID;
Priavte string name;
}
The above teacher, school saves the getter and setter methods.
The action class is as follows:
Public class teacheraction extends actionsupport implents modeldriven <teacher> {
Public String saveteacher (){
If (. Teacher. getschool ()! = NULL & teacher. getschool (). GETID ()! = NULL ){
Teacher. setschool (schooldao. Load (teacher. getschool (). GETID ()));
}
Teacher = teacherdao. Save (teacher );
Return success;
}
}
The configuration of this action is as follows:
<Package name = "ajax" extends = "JSON-Default">
<Action name = teacher _ "*" class = "com. zxt. Action. teacheraction method = {1}">
<! -- Set the return type to JSON -->
<Result type = "JSON"> </result>
</Action>
</Package>
The JSP format is as follows:
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
<Title> test </title>
<SCRIPT type = "text/javas example" src = "include/JS/jquery-1.4.2.min.js"> </SCRIPT>
<SCRIPT type = "text/javas example">
$ (Function (){
$ ("# TJ"). Click (function (){
// The submitted parameters. Name and inch are the receiving variables corresponding to the struts action.
VaR Params = {
"School. ID": $ ("# XM"). Val (),
"Name": $ ("# xm1"). Val (
};
$. Ajax ({
Type: "Post ",
URL: "teacher_findteacherbyscholl.action ",
Data: Params,
Datatype: "text", // set the Ajax return value to text (it can be returned in JSON format, and can be printed or set to JSON)
Success: function (JSON ){
VaR OBJ = $. parsejson (JSON); // use this method to parse JSON
VaR state_value = obj. Teacher // result corresponds to the get method of the result variable defined in action.
Alert (state_value );
},
Error: function (JSON ){
Alert ("JSON =" + JSON );
Return false;
}
});
});
});
</SCRIPT>
</Head>
<Body>
<Span> School: </span> <input id = "XM" type = "text">
<Br/>
<Span> name: </span> <input id = "xm1" type = "text">
<Br/>
<Input type = "button" value = "Submit" id = "TJ">
</Body>
</Html>