JQuery ajax requests struts action for asynchronous refresh, jquerystruts
This example is a small example using JQuery ajax and struts. In this example, the list in java Util is converted into a json-supporting format in two ways, the first is to use the json-lib.jar jar package to convert, the second is to use the goole gson-2.1.jar to convert, You can import the corresponding jar package as needed, both jar packages are imported for testing. Start to enter the subject
Step 1:Import related jar package, this example needs to import struts related jar package, json-lib.jar, gson-2.1.jar can be any choice, But here need to import, because in order to do the test, both jar packages are used for conversion.
Step 2:Configure web. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "3.0" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name> </display-name> <! -- Declare the front-end controller of Struts2 --> <filter-name> struts2 </filter-name> <filter-class> org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <! -- Declare the ContextListener of Spring and create BeanFactory as soon as the context is loaded. --> <context-param> <! -- If applicationContext. xml is not placed in the WEB-INF or is not called, this parameter must be declared --> <param-name> contextConfigLocation </param-name> <param-value> classpath: applicationContext. xml </param-value> </context-param> </web-app>
Step 3:New struts. xml, jump to/WEB-INF/index. jsp under admin/by default
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- <% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> --> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://www.yxccc.com/news/"> <struts> <package name = "bg" namespace = "/" extends = "struts-default"> <default-action-ref name = "index"/> <! -- ========================== Basic jump ========================= ==--> <action name = "index"> <result>/WEB-INF/index. jsp </result> </action> </package> </struts>
Step 4:Write AjaxRequestAction. java file. Two types of requests are made here. One is to directly request a string, and the other is to request data in an array format, however, the data must be converted to an array supported by JSON, as shown below:
Package com. fengqi. action; import java. io. IOException; import java. util. arrayList; import java. util. list; import javax. servlet. http. httpServletResponse; import net. sf. json. JSONArray; import org. apache. struts2.ServletActionContext; import com. google. gson. gson; import com. opensymphony. xwork2.ActionSupport;/*** Creation Time:, ajax request action sample */public class AjaxRequestAction extends ActionSupport {private St Ring sex; @ Override public String execute () throws Exception {return super.exe cute ();}/*** ajax request, response Request in json format */public void ajaxString () {System. out. println (sex); // obtain the corresponding Response HttpServletResponse response = ServletActionContext. getResponse (); // sets the encoding method response. setCharacterEncoding ("UTF-8"); try {if (sex. equals ("nan") {response. getWriter (). write ("I Am a man");} else if (sex. equals ("nv") {resp Onse. getWriter (). write ("I am a female");} else {response. getWriter (). write ("neither men nor women");} // write data to the page} catch (IOException e) {e. printStackTrace () ;}/ *** ajax request to respond to the request in the form of a list. The list here is not the List of Util, instead, it is converted to List */public void ajaxList () {List <Object> list = new ArrayList <Object> (); list. add ("Zhang San"); list. add ("Li Si"); // Method 1: Use the JSONArray in the json-lib package to convert the List to different types of JSONArray. JSONArray jsonArray = JSONArray. fromObject (list); // Method for week 2: Convert the List to a json object using the goole Json package. Gson gson = new Gson (); String gsonList = gson. toJson (list); // obtain the corresponding Response HttpServletResponse response = ServletActionContext. getResponse (); // sets the encoding method response. setCharacterEncoding ("UTF-8"); try {// write data to response in the page. getWriter (). println (jsonArray);} catch (IOException e) {e. printStackTrace () ;}} public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex ;}}
Step 5:Under the struts. xml file update, configure the access path of AjaxRequestAction. java and add the following code:
<Package name = "ajax" namespace = "/ajax" extends = "struts-default"> <! -- ========================= Ajax request jump ============================ ===--> <action name = "ajax _ *" class = "com. fengqi. action. ajaxRequestAction "method =" ajax {1} "> </action> </package>
The complete struts. xml file is
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://www.yxccc.com"> <struts> <package name = "bg" namespace = "/admin" extends = "struts-default"> <default-action-ref name = "index"/> <! -- ========================== Basic jump ========================= ==--> <action name = "index"> <result>/WEB-INF/index. jsp </result> </action> </package> <package name = "ajax" namespace = "/ajax" extends = "struts-default"> <! -- ========================= Ajax request jump ============================ ===--> <action name = "ajax _ *" class = "com. fengqi. action. ajaxRequestAction "method =" ajax {1} "> </action> </package> </struts>
Step 6:Compile index. jsp file. Two types of requests are made here. One is to directly request a string, and the other is to request data in an array format, however, the data must be converted to an array supported by JSON, as shown below:
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
Such a simple ajax request has been completed.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.