This article mainly introduces JQuery ajax to request struts action to achieve asynchronous refresh. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
This example is a small example using JQuery ajax and struts. In this example, two methods are used to convert the list in java Util into json format. The first one is to use The json-lib.jar jar package is used for conversion. The second method is to use goole's gson-2.1.jar for conversion. You can import the corresponding jar package as needed. Here, both jar packages are imported for testing. Now let’s get to the point
Step one:Import related jar packages. In this example, you need to import struts related jar packages. You can choose json-lib.jar or gson-2.1.jar at will. But you need to import both here, because for testing, both jar package conversion methods are used.
Step 2:Configure web.xml
struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* contextConfigLocation classpath:applicationContext.xml
Step 3:New struts.xml , jump to /WEB-INF/index.jsp under admin/ by default
/WEB-INF/index.jsp
Step 4:Write the AjaxRequestAction.java file, do it here Two requests are made, one is to directly request a string, and the other is to request a set of data in array format, but the data must be converted into an array supported by JSON, as follows
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; /** * 创建时间:2014-10-24,ajax请求的action样例 */ public class AjaxRequestAction extends ActionSupport{ private String sex; @Override public String execute() throws Exception { return super.execute(); } /** * ajax请求,以json格式的字符串响应请求 */ public void ajaxString(){ System.out.println(sex); //获取相应Response HttpServletResponse response = ServletActionContext.getResponse(); //设置编码方式 response.setCharacterEncoding("UTF-8"); try { if(sex.equals("nan")){ response.getWriter().write("我是男的"); }else if(sex.equals("nv")){ response.getWriter().write("我是女的"); }else{ response.getWriter().write("男女都不是"); } //将数据写到页面中 } catch (IOException e) { e.printStackTrace(); } } /** * ajax请求,以list的形式响应请求,主要这里的list并不是Util的List,而是经过转换成指出json格式的List */ public void ajaxList(){ List
Step 5:After updating the struts.xml file, configure the access path of AjaxRequestAction.java and add the following code
##
/WEB-INF/index.jsp
Step 6:Writing the index.jsp file, two requests are made here, one One is to directly request a string, and the other is to request a set of data in array format, but the data must be converted into an array supported by JSON, as follows
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>ajax异步刷新样例测试
这里是ajax请求Demo,该实例是请求Struts中的action
这里是p元素
请选择性别:
Example method of handwritten Ajax to achieve asynchronous refresh
php+jQuery+Ajax to simply implement asynchronous page refresh_php Example
Code to implement asynchronous refresh based on JQuery (reproduced)_jquery
The above is the detailed content of jQuery ajax requests struts action to implement asynchronous refresh example sharing. For more information, please follow other related articles on the PHP Chinese website!