国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jQuery ajax請求struts action實現異步刷新

2024-05-06 15:18:23
字體:
來源:轉載
供稿:網友

這個樣例是用JQuery ajax和struts來做的一個小樣例,在這個樣例中采用兩種方式將java Util中的list轉換成支json的格式,第一種是用json-lib.jar這個jar包來轉換,第二種是采用goole的gson-2.1.jar來轉換,大家可以根據需要導入相應的jar包,在這里為了做測試將兩種jar包都導入了。下面開始進入正題

第一步:導入相關jar包,本樣例需導入struts相關jar包,json-lib.jar,gson-2.1.jar可以任意選擇,但是這里需要都導入,因為為了做測試,兩種jar包的轉換方式都用到了。

第二步:配置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>  <!-- 聲明Struts2的前端控制器 --> <filter> <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>  <!-- 聲明Spring的ContextListener,負責上下文一加載立即創建BeanFactory --> <context-param> <!-- 若applicationContext.xml沒有放在WEB-INF下或者不叫這個名字,必需聲明此參數 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value>  </context-param></web-app>

第三步:新建struts.xml,默認admin/下跳轉到/WEB-INF/index.jsp

<?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"/> <!-- =================基礎跳轉====================== --> <action name="index">  <result>/WEB-INF/index.jsp</result> </action> </package></struts>

第四步:編寫AjaxRequestAction.java文件,這里做了兩種請求,一種是直接請求到字符串,另一種是請求到一組數組格式的數據,但該數據必須要轉換成JSON支持的數組,具體如下

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<Object> list = new ArrayList<Object>(); 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; } }            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 巫溪县| 信宜市| 洛川县| 陇川县| 秀山| 广德县| 镇平县| 江油市| 和田县| 内黄县| 南召县| 饶阳县| 屏边| 丹凤县| 龙州县| 深州市| 德庆县| 墨江| 武威市| 内黄县| 西乡县| 林口县| 洞口县| 嘉义市| 鸡西市| 抚松县| 勃利县| 宁夏| 昌邑市| 民勤县| 沈丘县| 奈曼旗| 牙克石市| 长丰县| 隆林| 福清市| 西平县| 古蔺县| 长寿区| 岑溪市| 济阳县|