About the Ajax call for an action that inherits the modeldriven Interface

Source: Internet
Author: User

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>


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.