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

How does Struts2 handle AJAX requests

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

Share

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

In this article, the editor introduces in detail "how Struts2 handles AJAX requests". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how Struts2 handles AJAX requests" can help you solve your doubts.

Struts2 handles AJAX requests

There are two ways for Struts2 to integrate AJAX:

Using the type= "stream" type

Use the JSON plug-in

Get text using the type= "stream" type

Front-end student number: name: query results

("# btn") .click (function () {$.ajax ({url: "HandlerAction", type: "get", data: {"no": $("# no"). Val (), "name": $("# name"). Val ()}, dataType: "text", error:function () {console.log ("ajax request failed!") }, success:function (data) {$("# score") .text (data);}})})

Url should correspond to the name of action in struts.xml and the namespace of package.

Action

Public class HandlerAction extends ActionSupport {private int no; private String name; private InputStream inputStream; public int getNo () {return no;} public void setNo (int no) {this.no = no;} public String getName () {return name;} public void setName (String name) {this.name = name;} public InputStream getInputStream () {return inputStream } public void setInputStream (InputStream inputStream) {this.inputStream = inputStream;} @ Override public String execute () throws Exception {/ / the default connection database query total score here String result = name + "classmate, your total score is: 680"; / / set the data to be returned. The data we send to the browser contains Chinese, and we need to set utf-8 encoding to solve the Chinese garbled inputStream=new ByteArrayInputStream (result.getBytes ("utf-8")); return SUCCESS;}}

The front end sends two fields to the background: no and name

Action needs to set two member variables with the same name and provide the corresponding getter and setter methods to receive the data from the frontend.

A member variable of type InputStream is required, and the corresponding getter and setter are provided to return data to the browser.

You need a method to process the request (execute) to set the data returned to the browser.

Struts.xml

Text/html inputStream process analysis

The front end sends an ajax request to the background, passing no and name2 fields

JVM creates an action instance, and calls the setter method corresponding to no and name to assign the value passed from the front end to the member variable (which will be automatically converted to the target type) to complete the initialization of action.

JVM calls execute, the business processing method of action, to set the data returned to the browser

JVM acquires the InputSteam according to the method specified in struts.xml (getInputStream) and passes the data in it to the browser.

Get json using the type= "stream" type

Front-end student ID: query student information $("# btn") .click (function () {$.ajax ({url: "HandlerAction", type: "post", data: {"no": $("# no"). Val ()}, dataType: "json", error:function () {console.log ("ajax request failed!") }, success:function (data) {$("# show"). Append ("name:" + data.name+ ","); $("# show"). Append ("age:" + data.age+ "); $(" # show "). Append (" grades: "+ data.score+". ") Actionpublic class HandlerAction extends ActionSupport {private int no; private InputStream inputStream; public int getNo () {return no;} public void setNo (int no) {this.no = no;} public InputStream getInputStream () {return inputStream;} public void setInputStream (InputStream inputStream) {this.inputStream = inputStream } @ Override public String execute () throws Exception {/ / the default connection database query here gets the student information Student student = new Student (1, "Zhang San", 20100); String jsonStr = JSON.toJSONString (student); / / sets the data to be returned inputStream=new ByteArrayInputStream (jsonStr.getBytes ("utf-8")); return SUCCESS;}}

Using Ali's fastjson.jar, you need to download and import it yourself.

Struts.xml

Configuration is the same as above

Using JSON plug-in to implement AJAX

Front-end student ID: query student information $("# btn") .click (function () {$.ajax ({url: "HandlerAction", type: "post", data: {"no": $("# no"). Val ()}, dataType: "json", error:function () {console.log ("ajax request failed!") }, success:function (data) {$("# show"). Append ("name:" + data.student.name+ ","); $("# show"). Append ("age:" + data.student.age+ "); $(" # show "). Append (" grades: "+ data.student.score+". ") Actionpublic class HandlerAction extends ActionSupport {private int no; private Student student; public int getNo () {return no;} public void setNo (int no) {this.no = no;} public Student getStudent () {return student;} public void setStudent (Student student) {this.student = student } @ Override public String execute () throws Exception {/ / here default connection database query to get student information student = new Student (1, "Zhang San", 20100); return SUCCESS;}}

You need to set a member variable with the same name and provide getter and setter methods to receive the data from the front end.

In this way, the JSON plug-in serializes the action object into a string in JSON format and passes it to the browser. Browsers can directly access all member variables of action (essentially calling the corresponding getter method).

We only need to encapsulate the data to be requested by ajax as member variables of action and provide the corresponding getter and setter methods. The requested data needs to be assigned before the return statement of the main tone method (execute).

Success:function (data) {$("# show") .append ("name:" + data.student.name+ ","); $("# show") .append ("age:" + data.student.age+ "); $(" # show ") .append (" score: "+ data.student.score+". ") ;}

The data received by the browser data itself is an instance of action, which can be obtained through. Access member variables.

Struts.xml true text/html description

The JSON plug-in struts2-json-plugin.jar needs to be added manually.

The above compression contains all the jar packages for struts, including struts2-json-plugin.jar.

The following compressed package has only 8 jar packages of the struts core.

After reading this, the article "how Struts2 handles AJAX requests" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

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