Struts2 interacts with Ajax data, and Struts2Ajax data
Preface:
Ajax requests are often used in projects. Today we will summarize the data transmission and interaction between foreground pages and background actions when ajax is used to request actions in Struts2.
Here I mainly record several methods I have mastered. You can select a project based on your needs.
1. Use stream-type result
This type allows the action in Struts2 to generate a text response to the client browser.
Example:
Jsp page:
<% @ Taglib prefix = "s" uri = "/struts-tags" %> <% @ page contentType = "text/html; charset = UTF-8 "language =" java "%>
Action for processing logic:
/*** Description: eleven. action * Author: Eleven * Date: 2018/1/26 18:09 */public class LoginAction extends ActionSupport {private String username; private String psw; // binary stream of the output result private InputStream inputStream; public String login () throws Exception {if (username. equals ("tom") & psw. equals ("123") {inputStream = new ByteArrayInputStream ("congratulations, logon successful ". getBytes ("UTF-8");} else {inputStream = new ByteArrayInputStream ("sorry, Logon Failed ". getBytes ("UTF-8");} return SUCCESS;} // provides the get method public InputStream getInputStream () {return inputStream;} public String getUsername () {return username ;} public void setUsername (String username) {this. username = username;} public String getPsw () {return psw;} public void setPsw (String psw) {this. psw = psw ;}}
In addition to the user name and password passed on the page, the action also contains an InputStream member variable and provides the corresponding get method. The binary stream returned by the get method is directly output to the client browser.
Struts. xml configuration:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name = "struts. enable. dynamicMethodInvocation "value =" false "/> <constant name =" struts. devMode "value =" true "/> <package name =" default "namespace ="/"extends =" struts-default "> <action name =" login "class =" eleven. action. loginAction "method =" login "> <r Esult type = "stream"> <! -- Specify the Data Type of the response generated by the stream --> <param name = "contentType"> text/html </param> <! -- Specify the method in the action to output the InputStream variable --> <param name = "inputName"> inputStream </param> </result> </action> </package> </struts>
Browse the page in the browser, enter the relevant information, and then submit it. You can see that the background action directly returns the message data to the page, and the page does not need to be refreshed, instead, it is directly displayed locally, which uses ajax to send requests asynchronously. Note: In this method, you need to configure the stream type in the struts. xml file, set the inputName attribute, and provide the get method corresponding to InputStream in the action.
Run:
2. Use a result of the json type
There is a jar package struts2-json-plugin-2.3.16.3.jar that can add a JSON plug-in for Struts2, that is, when the type of result in action is set to json, you can also asynchronously call action in client js, in addition, the data returned in the action can be directly serialized into a JSON string by the json plug-in and returned to the client browser.
Example:
Jsp page:
<% @ Taglib prefix = "s" uri = "/struts-tags" %> <% @ page contentType = "text/html; charset = UTF-8 "language =" java "%>
Action Code:
public class LoginAction extends ActionSupport{ private String username; private String psw; private int age; public String login() throws Exception{ age = 18; return SUCCESS; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
Configuration in struts. xml:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default,json-default"> <action name="login" class="eleven.action.LoginAction" method="login"> <result type="json"> <param name="noCache">true</param> <param name="contentType">text/html</param> </result> </action> </package></struts>
Browse the page in the browser, enter the relevant information, and then submit it. You can see that the background action directly returns the message data to the page, and the page does not need to be refreshed, instead, it is directly displayed locally, which uses ajax to send requests asynchronously. Note: In this method, you must configure the package to inherit json-default in the struts. xml file, set the result type to json, and provide the corresponding get method to pass data in the action. Of course, the premise is to add a struts2-json-plugin-2.3.16.3.jar, otherwise struts2 will not automatically convert the data to json format data.
Effect:
Therefore, we can summarize the steps to set the result type to json:
1. Import jar package: struts2-json-plugin-2.3.7.jar
2. Configure the result set view returned by struts to set type = json
3. Set the package of the corresponding action to inherit from json-default.
4. Provide the get method for the data to be returned
5. Set the format of returned data in struts. xml.
You can set the format of the returned data according to the needs of your project. Here is a simple example. complex data is not used. If a List set is returned, you can set the data format as follows:
<Result name = "test" type = "json"> <! -- Set the data source to get from a certain data --> <! -- Filter data from the gtmList set, and obtain only the name of the object in the set, and the uuid attribute -->
<Param name = "root"> gtmList </param> <param name = "includeProperties"> \ [\ d + \] \. name, \ [\ d + \] \. uuid </param> </result>
In addition to the above method, there is also the following method
<Result name = "ajaxGetBySm" type = "json"> <! -- This method is generally used to first use the source filter action to obtain all the values from the entire action by default (provided that the getAction () method is not available in this action), but root is not used for convenience: action, and then use the include settings for filtering settings --> <param name = "root"> action </param> <param name = "includeProperties"> gtmList \ [\ d + \] \. name, gtmList \ [\ d + \] \. uuid </param> </result>
The preceding two methods are used to set data to be obtained from the gtmList set, and only the attributes of the object are name and uuid. Here is only a simple example. You can study it on your own.
The json-type Result can be specified with common parameters:
In addition to the ajax supported by struts2, response. getWrite () can be used to allow the server to interact with the client browser.
PrintWriter printWriter =response.getWriter();printWriter.print("success");
Which method is used?
For me, if it is only a flag that determines whether the addition, deletion, and modification functions are successful, you can select response first. getWriter (). print ("xxx") and set the result type to stream. However, if a large amount of object data needs to be returned, the data is received on the page and then displayed. For example, the page uses ajax requests, to return a list set from the background action, you must select the json type.