Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Ajax development method in Struts2

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the Ajax development method in Struts2". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First of all, regardless of the native support of Struts2, let's write an ajax example to request the action action directly using asynchronous requests:

InfoAction.java

Packagecn.codeplus.action;importcom.opensymphony.xwork2.ActionSupport; publicclassInfoAction extendsActionSupport {privatestaticfinallongserialVersionUID = 1359090410097337654L; publicString loadInfo () {returnSUCCESS;}}

InfoAction simply returns "success".

Index.jsp

Get functionloadInfo () {$("# info") .load ("loadInfo");}

The index.jsp contains a button that triggers an asynchronous request event when clicked.

Struts.xml

/ info.jsp

It can be seen that the result of the above asynchronous request will be to load info.jsp,info.jsp is just a simple web page, not listed.

The running effect is as follows:

After clicking get:

The source code of the page at this time:

Tags are nested in the tags, which do not conform to the specification. In fact, as long as we write no such tags in info.jsp, this will not happen.

The above asynchronous request is only applicable to request a single file. If we request dynamic data and the data needs to be returned in JSON format, the above method will seem inadequate, which is the native support of struts2.

With struts2's ajax, struts2-json-plugin-2.2.1.jar must be introduced into the project, and version 2.1.7 + is bundled in the struts2 distribution (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 loading comments:

Dto object, Comment.java

Packagecn.codeplus.po; publicclassComment {privatelongid;privateString nickname;privateString content;publiclonggetId () {returnid;} publicvoidsetId (longid) {this.id = id;} publicString getNickname () {returnnickname;} publicvoidsetNickname (String nickname) {this.nickname = nickname;} publicString getContent () {returncontent;} publicvoidsetContent (String content) {this.content = content;}}

New InfoAction.java

Packagecn.codeplus.action; importjava.util.ArrayList;importjava.util.List; importcn.codeplus.po.Comment; importcom.opensymphony.xwork2.ActionSupport; publicclassInfoAction extendsActionSupport {privatestaticfinallongserialVersionUID = 1359090410097337654L; privateListcomments = newArrayList (); / / attributes without the getter and setter method will not be serialized to JSON @ SuppressWarnings ("unused") privateString title words Attributes decorated with transient will also be serialized to JSONprivatetransientString content;publicString loadInfo () {title= "123wooden people"; content= "you are wooden people, ." ; loadComments (); returnSUCCESS;} / * load message * / privatevoidloadComments () {Comment com1 = newComment (); com1.setContent ("not bad"); com1.setId (1); com1.setNickname ("Nani"); Comment com2 = newComment (); com2.setContent ("Yo Xi Xi"); com2.setId (2); com2.setNickname ("Xiaoqiang"); comments.add (com1); comments.add (com2);} publicListgetComments () {returncomments } publicvoidsetComments (Listcomments) {this.comments = comments;} publicstaticlonggetSerialversionuid () {returnserialVersionUID;} publicString getContent () {returncontent;} publicvoidsetContent (String content) {this.content = content;}} index.jsp is the same index.jsp. (* ^ _ ^ *) hee hee. Struts.xml has changed a lot:

In struts.xml:

First of all, package extends is transformed from struts-default to json-default, which is necessary and is only included in json-default. The result type used below is json.

Then the result type needs to be displayed within the json,result tag, without indicating the interface to which the view points.

* is the result of the run:

After clicking the "get" button:

It can be seen that both comments objects and content objects have been serialized to JSON data. I don't know if it's a version problem. A lot of materials say that properties modified by transient will not be serialized to JSON.

In order to prevent the content object from being serialized to JSON, when its getter setter method cannot be discarded, we can add a note to the getter method of content like this: @ JSON (serialize=false)

... @ JSON (serialize=false) publicString getContent () {returncontent;} publicvoidsetContent (String content) {this.content = content;}...

The results are as follows:

There are many options for @ JSON and json types of result, but who to serialize and who not to serialize, and the MIME type that returns the data. Readers can refer to the relevant documentation.

This is the end of the content of "what is the Ajax development method in Struts2". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report