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 are the ways to implement Ajax in Struts2

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly talks about "what are the ways to achieve Ajax in Struts2", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the ways to implement Ajax in Struts2?"

Ajax is essentially the same as a normal HTTP request, except that an ordinary HTTP request is for people to see, while an Ajax request is for JS code to use.

So the page of an Ajax request is generally simpler than the response content of an ordinary HTTP request, which may be part of a page, structured data such as xml, json, or a simple string.

Therefore, using Ajax,Action in Struts 2 generally does not invoke a jsp view for display (if the Ajax request content is part of the page, it may also be called jsp view), but in some other way.

Three methods are described below, which are used for Action to implement Ajax requests.

Method 1: rely on Servlet API

In the method of Action, the return NONE or return null representation is handled directly by Action without calling result, so there is no result tag in the action tag of the configuration file.

Here you get the HttpServletResponse object in Servlet directly and write a string through the output stream of response, similar to using Servlet without using Struts 2.

Method 2: use result type= "stream"

A simpler way than the previous method is recommended in the documentation for Struts 2, using result with type as stream. In this way, you can be independent of Servlet API, so unit testing is more convenient.

In the struts configuration file, the type for result is set to stream. It contains two parameters, the first is contentType, indicating the type of response, if there is Chinese, it is best to set the code, the second parameter is used to specify the corresponding input stream in Action, its default value is inputStream, so it can be omitted.

Method 3:struts 2 json plug-in

The content that can be returned by the above two methods is very random and can be any string.

Json strings are used in many Ajax requests because they can be easily converted to JavaScript objects.

You can easily generate json using struts2-json-plugin (it's also a good choice to call the json tool to generate json using the above two methods, so you don't have to use this plug-in).

In package, extends should be changed to json-default instead of struts-default,result 's type should be set to json.

Open the page through the browser and you can see the json:

{"age": 22, "friends": ["Yao Ming", "Lin Dan"], "name": "xxg"}

At this point, I believe you have a deeper understanding of "what are the ways to implement Ajax in Struts2?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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