jQuery ajax requests struts action to implement asynchronous refresh example sharing

小云云
Release: 2018-01-17 14:56:20
Original
1150 people have browsed it

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  
Copy after login

Step 3:New struts.xml , jump to /WEB-INF/index.jsp under admin/ by default


        /WEB-INF/index.jsp   
Copy after login

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 list = new ArrayList(); list.add("张三"); list.add("李四"); //第一种方法:利用json-lib包中的JSONArray将List转换成JSONArray各式。 JSONArray jsonArray = JSONArray.fromObject(list); //第二周方法:利用goole的json包将List转换成Json对象。 Gson gson = new Gson(); String gsonList = gson.toJson(list); //获取相应Response HttpServletResponse response = ServletActionContext.getResponse(); //设置编码方式 response.setCharacterEncoding("UTF-8"); try { //将数据写到页面中 response.getWriter().println(jsonArray); } catch (IOException e) { e.printStackTrace(); } } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
        
Copy after login

Step 5:After updating the struts.xml file, configure the access path of AjaxRequestAction.java and add the following code


##

    
Copy after login

The final complete file of struts.xml is


       /WEB-INF/index.jsp        
Copy after login

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元素


请选择性别:

Copy after login
Such a simple ajax request has been completed.

Related recommendations:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!