struts中實現ajax的配置資訊

來源:互聯網
上載者:User

標籤:servlet   com   方法   package   turn   商品   ltm   技術分享   efault   

基於Struts2架構下實現Ajax有兩種方式,第一種是原聲的方式,另外一種是struts2內建的一個外掛程式。

js部分調用方式是一樣的:

JS代碼:

 1 function testAjax() { 2  3     var $userNameInput = $("#ajax_username"); 4 var userName = $userNameInput.val(); 5  6  $.ajax({ 7 url : "originAjax.action", 8 type : "GET", 9 data : "ajaxField=" + userName,10 success : function(data, textStatus) {11  alert(data);12  }13  });14 }

第一種原生的實現方式:

Action中建立一個方法:

    private String username;    public String getUsername() {        return username; } public void setUsername(String username) { this.username = username; } public void checkUserName() throws IOException { HttpServletResponse response = ServletActionContext.getResponse(); PrintWriter writer = response.getWriter(); writer.print("hello " + username); writer.flush(); writer.close(); }

struts.xml中配置:

<action name="originAjax" class="TestAction" method="checkUserName" />  

這種方式不太推薦使用。

這裡重點講解下第二種方式:

1.引入struts2-json-plugin-2.5.2.jar 

2.Action中添加類似的如下代碼:

private String result;public String getResult() {        return result; } public void setResult(String result) { this.result = result; }/** * * AJAX for check MerchantAccount start * * */ private String merchantAccount; public String getMerchantAccount() { return merchantAccount; } public void setMerchantAccount(String merchantAccount) { this.merchantAccount = merchantAccount; } // AJAX for check Merchant public String checkMerchantAccountMethod() throws IOException { AppResultJsonBean ajaxResultJsonBean = new AppResultJsonBean(); if (StarCloudStringUtils.isEmpty(merchantAccount)) { ajaxResultJsonBean.setIsOK(false); ajaxResultJsonBean.setData(null); ajaxResultJsonBean.setResultCode(-1); ajaxResultJsonBean.setResultMessage("商家帳號不可為空"); ajaxResultJsonBean.setOther(null); JSONObject ajaxResultJsonData = JSONObject .fromObject(ajaxResultJsonBean); this.result = ajaxResultJsonData.toString(); return SUCCESS; } if (!StarCloudStringUtils.isMobile(merchantAccount)) { ajaxResultJsonBean.setIsOK(false); ajaxResultJsonBean.setData(null); ajaxResultJsonBean.setResultCode(-2); ajaxResultJsonBean.setResultMessage("商家帳號格式不合法"); ajaxResultJsonBean.setOther(null); JSONObject ajaxResultJsonData = JSONObject.fromObject(ajaxResultJsonBean); this.result = ajaxResultJsonData.toString(); return SUCCESS; } 。。。 MerchantBean checkMerchantBean = merchantIService.findMerchantByAccount(merchantAccount); if (checkMerchantBean != null) { ajaxResultJsonBean.setIsOK(true); ajaxResultJsonBean.setData(null); ajaxResultJsonBean.setResultCode(0); ajaxResultJsonBean.setResultMessage("商家帳號可用"); ajaxResultJsonBean.setOther(null); JSONObject ajaxResultJsonData = JSONObject.fromObject(ajaxResultJsonBean); this.result = ajaxResultJsonData.toString(); return SUCCESS; } else { ajaxResultJsonBean.setIsOK(false); ajaxResultJsonBean.setData(null); ajaxResultJsonBean.setResultCode(-3); ajaxResultJsonBean.setResultMessage("商家帳號不存在"); ajaxResultJsonBean.setOther(null); JSONObject ajaxResultJsonData = JSONObject.fromObject(ajaxResultJsonBean); this.result = ajaxResultJsonData.toString(); return SUCCESS; } } /** * * AJAX for check MerchantAccount start end * * */

Struts.xml中配置如下:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"        "http://struts.apache.org/dtds/struts-2.5.dtd"><struts> <!-- AJAX 1.引入Jar包 2.所在包必須要繼承自JSON-default 3.resultType是JSON 4.附加了一個參數excludeNullProperties,目的是不序列化Action裡為null的欄位。 5.<result>元素沒有name屬性,也沒有跳轉值 --> <package name="struts_web_product_ajax" extends="json-default"> <!-- 新增商品資訊檢查帳號 --> <action name="checkMerchantAccountAction" class="controllers.actions.web.product.PrepareAddProductAction" method="checkMerchantAccountMethod"> <result type="json"> <param name="excludeNullProperties">true</param> <param name="root">result</param> </result> </action> </package> </struts>

JS中接受返回結果:

返回JSON格式:

JS解析如下:

function checkMerchantAccountAjax() {    var $merchantAccount = $("#merchantAccount");    var merchantAccount = $merchantAccount.val(); $.ajax({ url : "checkMerchantAccountAction", type : "GET", data : "merchantAccount=" + merchantAccount, success : function(data, textStatus) { var resultJSONData = JSON.parse(data);//注意這裡必須有,因為之前返回的是result="json字串",但並類型不是JSON if(resultJSONData.isOK){ $merchantAccount.css("color", "black"); return true; }else{ $merchantAccount.css("color", "red"); layer.tips(resultJSONData.resultMessage,$merchantAccount, { tips : [3, ‘#3595CC‘], time : 4000 });//end tips return false; }//end else }//end success });//end ajax}// end js

struts中實現ajax的配置資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.