struts2 + jquery ajax 基本用法

來源:互聯網
上載者:User
由於開發工作需要,近期研磨了在Struts2架構下使用Jquery Ajax的技術。現以表單的提交為例,簡單現總結如下:1、jsp檔案<script type="text/javascript" src="JS/jquery-1.9.1.min.js"></script> <script type="text/javascript" >   $(function(){   $("#btn1").click(function(){  
   $.ajax({  
url:'ajaxActionName.action',  //提交目標,幕後處理action  
dataType:'json',  
type:'post',  
data:{ txtStr: $("#txt").val()}, //擷取文字框的值作為參數  
success:showResult //設定action執行成功後的動作函數   });   });});function showResult(data) //data為action所返回的值   {   alert(data.result); //result是action中用作傳回值的一個變數   }</script><input id="txt" type="text" /><br><input id="btn1" type="button" value="btn1" />   2、struts.xml<package name="test" extends="json-default">  <action name=" ajaxActionName " class="com.btnAction">    <result type="json"></result>  </action>  </package>3、action: btnAction.javapackage com;import com.opensymphony.xwork2.ActionSupport;public class btnAction extends ActionSupport{private String result; private String  txtStr ;public String getResult(){return this.result;}public void setResult(String tmp){this.result=tmp;}public String getTxtStr (){return this.txtStr ;}public void setTxtStr(String tmp){this.btn1=tmp;}public String execute() throws Exception{this.result=this.btn1;  //擷取所傳來的文字框的值,賦給result變數,返回給ajaxreturn SUCCESS;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.