Web2.0 's Drift, Ajax that is brilliant, STRUTS2 framework itself integrates the native support for Ajax (Struts 2.1.7+, the previous version can be implemented via plug-ins), the framework of the integration just makes the creation of JSON is very simple, And can be easily integrated into the STRUTS2 framework, of course, it is only when we need the JSON to be the color of the overflow.
First of all, we don't talk about Struts2 's native support, we write an Ajax example ourselves, and use an asynchronous request to request an action directly:
Infoaction.java
Package Cn.codeplus.action;import Com.opensymphony.xwork2.ActionSupport; Public class Infoaction extends Actionsupport { privatestaticlong 1359090410097337654L; Public String Loadinfo () { return SUCCESS; }}
Infoaction simply returns "Success".
index.jsp
<! DOCTYPE HTML Public"-//w3c//dtd HTML 4.01 transitional//en">Basehref="<%=basePath%>"> <title> get </title> <script type="Text/javascript"Src="Js/jquery.js"></script> <script type="Text/javascript">function Loadinfo () {$ ("#info"). Load ("Loadinfo");}</script> "Button"Value="Get"Id="Btnget"onclick="Loadinfo ()"/> <div id="Info"></div> </body>index.jsp contains a button that triggers an asynchronous request event when the button is clicked.
Struts.xml
<package name= ajaxdemo " Extends=" struts-default Span style= "color: #800000;" > " > <action name=" loadinfo class = " cn.codeplus.action.infoaction " Method= loadinfo " > <result name=" success >/info.jsp</result> </action></package
The result of the above asynchronous request will be the load info.jsp,info.jsp is just a simple Web page, not listed.
The results are as follows:
After you click Get:
The page source code at this point:
An initial analysis of STRUTS2 AjaxWeb2.0 's Drift, Ajax that is brilliant, STRUTS2 framework itself integrates the native support for Ajax (Struts 2.1.7+, the previous version can be implemented via plug-ins), the framework of the integration just makes the creation of JSON is very simple, And can be easily integrated into the STRUTS2 framework, of course, it is only when we need the JSON to be the color of the overflow.
First of all, we don't talk about Struts2 's native support, we write an Ajax example ourselves, and use an asynchronous request to request an action directly:
Infoaction.java
Press CTRL + C to copy the code<textarea style="width: 814.333px; height: 222.667px; font-family: Courier New; font-size: 12px; line-height: 1.5;"></textarea>Press CTRL + C to copy the codeInfoaction simply returns "Success".
index.jsp
Press CTRL + C to copy the code<textarea style="width: 814.333px; height: 309.067px; font-family: Courier New; font-size: 12px; line-height: 1.5;"></textarea>Press CTRL + C to copy the codeindex.jsp contains a button that triggers an asynchronous request event when the button is clicked.
Struts.xml
<PackageName= "Ajaxdemo"Extends= "Struts-default">
<ActionName= "Loadinfo"Class= "cn.codeplus.action.InfoAction" Method= "Loadinfo"
result name= " Success ">/info.jsp </result>
</action>
</package< Span style= "color: #0000ff;" >>
The result of the above asynchronous request will be the load info.jsp,info.jsp is just a simple Web page, not listed.
The results are as follows:
After you click Get:
The page source code at this point:
<div> tags nested <title> tags, do not conform to the specification, in fact, we just want to info.jsp write no <title> such tags, there will be no such situation.
The above-mentioned asynchronous request only applies to requesting a single file, and if we are requesting dynamic data and the data needs to be returned in JSON format, the above approach will be inadequate, and this is the native support of Struts2.
Using STRUTS2 Ajax, you must introduce Struts2-json-plugin-2.2.1.jar in the project, and the version 2.1.7+ is bound to the STRUTS2 release package (the previous version can be downloaded here). Remember, to introduce Struts2-json-plugin-2.2.1.jar.
This time we use another example to simulate the load comment:
DTO object, Comment.java
Package Cn.codeplus.po; Public classComment {Private LongID; PrivateString Nickname; PrivateString content; Public LonggetId () {returnID; } Public voidSetId (LongID) { This. ID =ID; } PublicString Getnickname () {returnnickname; } Public voidSetnickname (String nickname) { This. Nickname =nickname; } PublicString getcontent () {returncontent; } Public voidsetcontent (String content) { This. Content =content; }}
The new Infoaction.java
Package Cn.codeplus.action;import java.util.arraylist;import java.util.list;import cn.codeplus.po.Comment; Import Com.opensymphony.xwork2.ActionSupport; Public classInfoaction extends Actionsupport {Private StaticFinalLongSerialversionuid =1359090410097337654L; PrivateList<comment> comments =NewArraylist<comment>(); //No Getter and setter method properties are not serialized to JSON@SuppressWarnings ("Unused") PrivateString title; //!! Properties that are decorated with transient are also serialized to JSON Privatetransient String content; PublicString Loadinfo () {title="123 Wood man"; Content="you're a wood man, haha. "; Loadcomments (); returnSUCCESS; } /** * Load message message*/ Private voidloadcomments () {Comment com1=NewComment (); Com1.setcontent ("very nice ."); Com1.setid (1); Com1.setnickname ("Nani"); Comment COM2=NewComment (); Com2.setcontent ("Yo West yo West"); Com2.setid (2); Com2.setnickname ("Xiao Qiang"); Comments.add (COM1); Comments.add (COM2); } PublicList<comment>getcomments () {returncomments; } Public voidSetcomments (list<comment>comments) { This. Comments =comments; } Public Static LongGetserialversionuid () {returnSerialversionuid; } PublicString getcontent () {returncontent; } Public voidsetcontent (String content) { This. Content =content; } }
Index.jsp is still the index.jsp. (*^__^*) hehe ...
Struts.xml Change quite large:
<package name="Ajaxdemo"extends="Json-default"> <action name="Loadinfo" class="cn.codeplus.action.InfoAction"Method="Loadinfo"> <result name="Success"Type="JSON"></result> </action></package>
In the Struts.xml:
First, the package extends is converted from Struts-default to Json-default, which is required only in Json-default to contain the result type used below as JSON.
Then the result type needs to be displayed as indicated in the Json,result tag without having to indicate the interface to which the view is pointing.
Finally, the result is running:
After clicking on the "Get" button:
Visible comments objects and content objects are serialized to JSON data, not knowing if the issue is a version, and many of the materials say that properties that use transient adornments are not serialized to JSON.
In order for the content object to not be serialized to JSON, we can add annotations to the content getter method when it is not possible to discard its getter Setter method: @JSON (Serialize=false)
... @JSON (serialize=false) Public String getcontent () { return content;} Public void setcontent (String content) { this. Content= content;} ...
The results are as follows:
@JSON and JSON type of result have a lot of options, nothing more than serialization, who does not serialize, return the data MIME type, the reader can refer to the relevant documents themselves.
Get to JSON data, the next step is to use JS processing JSON data in the foreground, little brother JS not fine, like to use jquery parsing, if interested, and listen to tell jquery parsing JSON data.
An initial analysis of STRUTS2 Ajax