Basic usage of struts2 + jquery Ajax

Source: Internet
Author: User
Due to development needs, jquery Ajax technology is recently used in the struts2 framework. Taking Form submission as an example, the following is a summary: 1. jsp file <SCRIPT type = "text/JavaScript" src = "JS/jquery-1.9.1.min.js"> </SCRIPT> <SCRIPT type = "text/JavaScript" >$ (function () {$ ("# btn1 "). click (function (){
$. Ajax ({
URL: 'ajaxactionname. action', // submit the target and process the action in the background.
Datatype: 'json ',
Type: 'post ',
Data: {txtstr: $ ("# TXT"). Val ()}, // obtain the value of the text box as a parameter
Success: showresult // set the action function after successful action execution}) ;}); function showresult (data) // data is the value returned by action {alert (data. result ); // result is a variable used as the return value in action} </SCRIPT> <input id = "TXT" type = "text"/> <br> <input id = "btn1" type = "button" value = "btn1"/> 2. struts. XML <package name = "test" extends = "JSON-Default"> <action name = "ajaxactionname" class = "com. btnaction "> <result type =" JSON "> </result> </Action> </package> 3. Action: btnaction. javapackage com; import COM. opensymphony. xwork2.actionsupport; public class btnaction extends actionsupport {private string result; private string txtstr; Public String getresult () {return this. result;} public void setresult (string TMP) {This. result = TMP;} Public String gettxtstr () {return this.txt STR;} public void settxtstr (string TMP) {This. btn1 = TMP;} Public String execute () throws exception {This. result = This. btn1; // obtain the value of the sent text box, assign it to the result variable, and return it to ajaxreturn success ;}}

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.