Struts2 Ajax form asynchronously submits form requests

Source: Internet
Author: User
Tags i18n

Use Ajax forms
With the support of struts2 Ajax forms, form requests can be submitted asynchronously.
Set the Ajax topic of a form
Using an Ajax form is not much different from using a normal form. You only need to set the form subject to Ajax. When submitting this form, the system submits the request in Ajax mode.
To use Ajax forms, you must use the <s: Head theme = "ajax"/> label to import the Ajax header.
There is an important advantage of using Ajax technology: the current page will not be submitted, and the server responds to data rather than content. Therefore, the server response data is only used to update part of the page content.
So, which part of the content is used to update the response of an Ajax form request and how to update it? This is controlled through two parts.
In the first part, the struts2 tag should be used in the Ajax form to generate the submit button to specify the content of the updated response of the form request. You can specify
A targets attribute is used to specify all parts of the form update. To update multiple parts, multiple parts are separated by commas.
The second part is how to update. By default, when the server responds, the server outputs the server response in the HTML element specified by the targets attribute by default, overwriting the content in the original HTML element.
If you want to execute the JavaScript code returned by the server on this page, you can specify the executescripts = "true" attribute for the submit button.
The following is the sample code for using the Ajax form remoteform. jsp:
<% @ Page contenttype = "text/html; charset = GBK" Language = "Java" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<HTML>
<Head>
<Title> remote form </title>
<S: Head theme = "ajax"/>
</Head>
<Body>

<Div id = 'show' style = "background-color: # bbbbbb; width: 360px; Height: 80px"> original static text </div>

Fill in another Div using the return value of the form request. <Br/>
<S: form. ID = 'theform1'
Cssstyle = "border: 1px solid black ;"
Action = 'axaxtest'
Method = 'post'
Theme = "ajax">
<S: textfield name = 'data' label = "Enter your favorite books"/>
<S: Submit value = "Modify the static text above" targets = "show"/>
</S: Form>

Fill the form with the return value of the form request <br/>
<S: form. ID = 'theform2'
Cssstyle = "border: 1px solid black ;"
Action = 'axaxtest'
Method = 'post'
Theme = "ajax">
<S: textfield name = 'data' label = "Enter your favorite books"/>
<S: Submit value = "Modify form itself" targets = "theform2"/>
</S: Form>

Run remote JavaScript directly (by specifying executescripts = "true") <br/>
<S: form. ID = 'theform3'
Cssstyle = "border: 1px solid black ;"
Action = 'test3'
Method = 'post'
Theme = "ajax">
<S: textfield name = 'data' label = "Enter your favorite books"/>
<S: Submit value = "execute remote JS" executescripts = "true" targets = "show"/>
</S: Form>

</Body>
</Html>

The ajaxtestaction. Java code is as follows:
Package Lee;

Import com. opensymphony. xwork2.action;

Import java. Io. serializable;

Public class ajaxtestaction implements action, serializable {

Private Static int counter = 0;
Private string data;

Public long getservertime (){
Return System. currenttimemillis ();
}

Public int getcount (){
Return ++ counter;
}

Public String getdata (){
Return "server prompt:" + data;
}

Public void setdata (string data ){
This. Data = data;
}

Public String execute () throws exception {
Return success;
}
}

The configuration file struts. XML Code is as follows:
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd>

<Struts>

<Constant name = "struts. Custom. i18n. Resources" value = "messageresource"/>
<Constant name = "struts. i18n. encoding" value = "GBK"/>

<Package name = "ajax" extends = "struts-Default">
<Action name = "ajaxtest" class = "Lee. ajaxtestaction">
<Result>/ajaxresult. jsp </result>
</Action>
<Action name = "test3">
<Result>/testjs. jsp </result>
</Action>
</Package>

</Struts>

Server Response page (the server responds to the data to be output ):
The ajaxresult. JSP code is as follows:
<% @ Page contenttype = "text/html; charset = GBK" Language = "Java" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<%
Request. setattribute ("decorator", "NONE ");
// Force the browser not to cache the content of this page
Response. setheader ("cache-control", "No-Cache"); /// HTTP 1.1
Response. setheader ("Pragma", "No-Cache"); // HTTP 1.0
// Prevent the browser from directly obtaining the content of this page from the Proxy Server
Response. setdateheader ("expires", 0); // prevents caching at the Proxy Server
%>

Server counter: <s: property value = "count"/> <br>
Current Time: <s: property value = "servertime"/> <br>
The server returns the following message: <s: property value = "data"/>

The testjs. JSP code is as follows:
<% @ Page contenttype = "text/html; charset = GBK" Language = "Java" %>
<%
Request. setattribute ("decorator", "NONE ");
Response. setheader ("cache-control", "No-Cache"); /// HTTP 1.1
Response. setheader ("Pragma", "No-Cache"); // HTTP 1.0
Response. setdateheader ("expires", 0); // prevents caching at the Proxy Server
%>

<Script. Language = "JavaScript" type = "text/JavaScript">
Alert ('spring2. 0 baodian ');
</SCRIPT>
Lightweight J2EE Enterprise Application Practice
<Script. Language = "JavaScript" type = "text/JavaScript">
Alert ('j2ee-based Ajax! ');
</SCRIPT>

Http://topic.csdn.net/u/20080731/10/abf33850-e24f-4910-aabb-0a58a5bedbe6.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.