struts2 +jquery+ajax簡單小例子

首先寫頁面,主要代碼爲:

[plain] view plaincopy
  1. <script type="text/javascript" src="js/jquery-1.6.1.js"></script>  
  2.  <script type="text/javascript">  
  3.  $(document).ready(function(){  
  4.   $("input").click(function(){  
  5.    
  6.  $.ajax({  
  7.  url:'<%=basePath%>strutsJ.do',  
  8.  error:function(){  
  9.  alert("this has errors!!");  
  10.  },  
  11.  success:function(data){  
  12.  alert(data);  
  13.  }  
  14.  });  
  15.  });  
  16.  });  
  17.  </script>  
  18.   </head>  
  19.     
  20.   <body>  
  21.    <input type="submit" value="submit"/>  
  22.   </body>  


 
然後是action,代碼爲import java.io.IOException;

[plain] view plaincopy
  1. import org.apache.struts2.ServletActionContext;  
  2.   
  3. public class TestAction {  
  4.   
  5.     private String result;  
  6.   
  7.   
  8.     // ajax返回結果  
  9.     public String getResult() {  
  10.         return result;  
  11.     }  
  12.   
  13.     public String execute() {  
  14.         this.result = "Hello! ";  
  15.         System.out.println("this is action here!!");  
  16.         try {  
  17.    ServletActionContext.getResponse().getWriter().print(result);  
  18.   } catch (IOException e) {  
  19.    // TODO Auto-generated catch block  
  20.    e.printStackTrace();  
  21.   }   
  22.   
  23.         return null;  
  24.     }  
  25.   
  26. }  


其次是struts.xml,代碼爲:

[plain] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE struts PUBLIC  
  4.         "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"  
  5.         "http://struts.apache.org/dtds/struts-2.1.dtd">  
  6.   
  7. <struts>  
  8.  <!--指定web應用的默認編碼集,相當於調用HttpServletRequest.setCharacterEncoding方法 -->  
  9.  <constant name="struts.i18n.encoding" value="UTF-8" />  
  10.  <!-- 指定需要struts2處理的請求後綴,默認值爲action. 如果用戶需要指定多個請求後綴,則多個後綴之間以英語逗號(,)隔開 -->  
  11.  <constant name="struts.action.extension" value="do" />  
  12.   
  13.    
  14.   
  15.  <package name="struts-platform" extends="struts-default" namespace="/">  
  16.   <!--自定議返回類型 -->  
  17.   <action name="strutsJ" class="lqx.TestAction">  
  18.     
  19.   </action>  
  20.     
  21.  </package>  
  22. </struts>    


最後是web.xml,代碼爲:

[plain] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.  xmlns="http://java.sun.com/xml/ns/javaee"   
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.     
  11.   <filter>  
  12.   <filter-name>struts2</filter-name>  
  13.   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  14.  </filter>  
  15.  <filter-mapping>  
  16.   <filter-name>struts2</filter-name>  
  17.   <url-pattern>*.do</url-pattern>  
  18.  </filter-mapping>  
  19. </web-app>  


結束語:這是一個關於jquery ajax和struts2結合的一個小例子,只是一個最簡單的流程,但卻是最基本,最關鍵的一步。

發佈了14 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章