posts - 41,  comments - 8,  trackbacks - 0

          http://prototype.conio.net/dist/

          下載(對Ajax支持的prototype--js函數庫):

           

          http://code.google.com/p/jsonplugin/downloads/list
          下載(Struts2的JSON插件):

            jsonplugin-0.25.jar Struts 2 JSON Plugin 0.25


          第一:手動建立項目結構(類似于MyEclipse創建Web Pro項目的后臺操作)


          1、新建文件夾結構如下:
            Struts2json
            |______WEB-INF
                         |_______classes
                         |_______src
                         |_______lib

          2、復制Tomcat里conf文件夾里的web.xml到WEB-INF文件夾下,并修改web.xml文件
          web.xml文件:

          Xml代碼 復制代碼
          1. <?xml version="1.0" encoding="ISO-8859-1"?>  
          2.   
          3. <web-app 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.     version="2.5">  
          8.        
          9. </web-app>  

           

          3、將剛才下載解壓后Struts2下的lib文件夾里
          commons-logging-1.0.4.jar
          freemarker-2.3.8.jar
          ognl-2.6.11.jar
          struts2-core-2.0.11.1.jar
          xwork-2.0.4.jar

          拷貝到Struts2json下lib文件夾里,并將
          jasonplugin-0.25.jar 也拷貝到Struts2json/WEB-INF/lib文件夾下。
          prototype-1.4.0.js 拷貝到Struts2json文件夾下。

          4、找到Strust2里src\apps\showcase\src\main\resources(就是解壓后里面的實例)的struts.xml文件復制到Struts2json下classes文件夾下,并修改struts.xml文件 ,或直接在classes文件夾下新建一個
          struts.xml文件:

          Xml代碼 復制代碼
          1. <?xml version="1.0" encoding="UTF-8" ?>  
          2. <!DOCTYPE struts PUBLIC   
          3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
          4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
          5.   
          6. <struts>  
          7.   
          8. </struts>  

           

          5、新建并手寫一個build.xml(必須已經安裝了Ant工具),并將build.xml放置到WEB-INF文件夾下 (MyEclipse內置了Ant)
          build.xml文件:

          Xml代碼 復制代碼
          1. <?xml version="1.0"?>  
          2. <project name="struts" basedir="." default="">  
          3.   
          4.     <path id="classpath">  
          5.         <fileset dir="lib">  
          6.             <include name="*.jar"/>  
          7.         </fileset>  
          8.         <pathelement path="."/>  
          9.     </path>  
          10.   
          11.     <target name="compile" description="Compile all source code">  
          12.         <javac destdir="classes" debug="true"  
          13.             deprecation="false" optimize="false" failonerror="true">  
          14.             <src path="src"/>  
          15.             <classpath refid="classpath"/>  
          16.         </javac>  
          17.     </target>  
          18.   
          19. </project>  

           

           

          總結:目錄結構如下
            Struts2t
            |______WEB-INF
                         |_______classes
                          
                       |______struts.xml
                         |_______src
                         |_______lib
                                         |_______ commons-logging-1.0.4.jar
                                         |_______ freemarker-2.3.8.jar
                                         |_______ ognl-2.6.11.jar
                                         |_______ struts2-core-2.0.11.1.jar
                                         |_______ xwork-2.0.4.jar              
                                         |_______ jsonplugin-0.25.jar
                         |_______web.xml
                         |_______build.xml
           |______prototype-1.4.0.js

          ----------------------------------------------------------

          第二:編寫核心代碼

          1、Struts2核心就是控制器,為Struts2添加核心Filter配置在web.xml文件中(攔截所有Web請求并由FilterDispatcher初始化)
          web.xml文件:

          Xml代碼 復制代碼
          1. <?xml version="1.0" encoding="ISO-8859-1"?>  
          2.   
          3. <web-app 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.     version="2.5">  
          8.        
          9.     <filter>  
          10.         <filter-name>struts2</filter-name>  
          11.         <filter-class>org.apache.Struts2.dispatcher.FilterDispatcher</filter-class>  
          12.     </filter>  
          13.        
          14.     <filter-mapping>  
          15.         <filter-name>struts2</filter-name>  
          16.         <url-pattern>/*</url-pattern>  
          17.     </filter-mapping>  
          18.        
          19. </web-app>  




          2、編寫表現層ajaxtest.jsp頁面并放在與WEB-INF同一級目錄下。
          ajaxtest . jsp文件:

          Html代碼 復制代碼
          1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
          2. <script src="prototype-1.4.0.js" type="text/javascript">  
          3. </script>  
          4. <script language="JavaScript">  
          5.     function gotClick()   
          6.     {   
          7.         //請求的地址   
          8.         var url = 'JSONExample.action';   
          9.         //將form1表單域的值轉換為請求參數   
          10.         var params = Form.serialize('form1');   
          11.         //創建Ajax.Request對象,對應于發送請求   
          12.         var myAjax = new Ajax.Request(   
          13.         url,   
          14.         {   
          15.             //請求方式:POST   
          16.             method:'post',   
          17.             //請求參數   
          18.             parameters:params,   
          19.             //指定回調函數   
          20.             onComplete: processResponse,   
          21.             //是否異步發送請求   
          22.             asynchronous:true   
          23.         });   
          24.     }   
          25.     function processResponse(request)   
          26.     {   
          27.         $("show").innerHTML = request.responseText;   
          28.     }      
          29. </script>  
          30. <html>  
          31. <head>  
          32. <title>使用JSON插件</title>  
          33. </head>  
          34. <body>  
          35. <form id="form1" name="form1" method="post">  
          36. <INPUT TYPE="text" name="field1" id="field1"/><br>  
          37. <INPUT TYPE="text" name="field2" id="field2"/><br>  
          38. <INPUT TYPE="text" name="field3" id="field3"/><br>  
          39. <INPUT TYPE="button" value="提交" onClick="gotClick();"/>  
          40. </form>  
          41. <div id="show">  
          42. </div>  
          43. </body>  
          44. </html>  

           

          3、編寫POJO(Action)在src下新建文件夾org,在org下新建文件夾jee(這里是建立包名),并新建類JSONExample.java放置在src/org/jee文件夾下。

          JSONExample . java文件:

          Java代碼 復制代碼
          1. package org.jee;   
          2.   
          3. import java.util.HashMap;   
          4. import java.util.Map;   
          5.   
          6. import com.opensymphony.xwork2.Action;   
          7. import com.googlecode.jsonplugin.annotations.JSON;   
          8.   
          9. public class JSONExample   
          10. {   
          11.     private int[] ints = {1020};   
          12.     private Map map = new HashMap();   
          13.     private String customName = "custom";   
          14.   
          15.     private String field1;   
          16.     //'transient'不會被序列化   
          17.     private transient String field2;   
          18.     //沒有setter和getter方法的字段不會被序列化   
          19.     private String field3;   
          20.   
          21.     public String execute()   
          22.     {   
          23.         map.put("name""yeeku");   
          24.         return Action.SUCCESS;   
          25.     }   
          26.   
          27.     public String getField1() {   
          28.         return field1;   
          29.     }   
          30.   
          31.     public void setField1(String field1) {   
          32.         this.field1 = field1;   
          33.     }   
          34.     public String getField2() {   
          35.         return field2;   
          36.     }   
          37.   
          38.     public void setField2(String field2) {   
          39.         this.field2 = field2;   
          40.     }   
          41.   
          42.     public String getField3() {   
          43.         return field3;   
          44.     }   
          45.   
          46.     public void setField3(String field3) {   
          47.         this.field3 = field3;   
          48.     }   
          49.   
          50.     public int[] getInts() {   
          51.         return ints;   
          52.     }   
          53.   
          54.     public void setInts(int[] ints) {   
          55.         this.ints = ints;   
          56.     }   
          57.   
          58.     public Map getMap() {   
          59.         return map;   
          60.     }   
          61.   
          62.     public void setMap(Map map) {   
          63.         this.map = map;   
          64.     }   
          65.   
          66.     @JSON(name="newName")   
          67.     public String getCustomName()    
          68.     {   
          69.         return this.customName;   
          70.     }   
          71. }  



          4、在struts.xml里配置Action,修改classes文件
          struts.xml文件:

          Xml代碼 復制代碼
          1. <?xml version="1.0" encoding="GBK"?>  
          2. <!DOCTYPE struts PUBLIC   
          3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
          4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
          5. <struts>  
          6.     <constant name="struts.i18n.encoding" value="UTF-8"/>  
          7.     <package name="example"  extends="json-default">  
          8.         <action name="JSONExample" class="org.jee.JSONExample">  
          9.             <result type="json"/>  
          10.         </action>  
          11.     </package>  
          12.   
          13. </struts>  

          注意:

          第一:配置

          Xml代碼 復制代碼
          1. <constant name="struts.i18n.encoding" value="UTF-8"/>  

          不使用GBK編碼,而使用UTF-8編碼,因為Ajax的POST請求都以UTF-8的方式進行編碼的。

          第二:配置包時,繼承了json-default包,不再繼承默認的default包,因為只有在json-default包下才有json類型的Result。



          5、將Struts2t整個文件夾拷貝到Tomcat/webapps文件夾下

          總結:目錄結構如下
            Struts2t
            |______WEB-INF
                         |_______classes
                                        |______org
                                                     |_____jee
                                                                |______JSONExample.class
                                        |______struts.xml

                         |_______src
                                        |______org
                                        |_____jee
                                                    |______JSONExample.java
                          |_______lib
                                        |_______ commons-logging-1.0.4.jar
                                        |_______ freemarker-2.3.8.jar
                                        |_______ ognl-2.6.11.jar
                                        |_______ struts2-core-2.0.11.1.jar
                                        |_______ xwork-2.0.4.jar  
                                        |_______ jsonplugin-0.25.jar   
                         |_______web.xml
                         |_______build.xml
           |______prototype-1.4.0.js
           |______ajaxtest.jsp

           

          ----------------------------------------------------------

          三、測試

          1、啟動Tomcat6

          2、http://localhost:8080/struts2t (進行測試)

          posted on 2008-10-04 14:19 Loy Fu 閱讀(2061) 評論(0)  編輯  收藏 所屬分類: struts
          主站蜘蛛池模板: 滁州市| 清新县| 安阳市| 鄂伦春自治旗| 鄢陵县| 阳江市| 淮滨县| 旺苍县| 大洼县| 府谷县| 无锡市| 达孜县| 抚顺市| 谷城县| 梨树县| 三亚市| 常德市| 舟山市| 额尔古纳市| 衡南县| 宝清县| 游戏| 北海市| 金坛市| 九寨沟县| 婺源县| 鹤庆县| 会昌县| 留坝县| 永城市| 志丹县| 吉林市| 昆明市| 平凉市| 灵丘县| 文安县| 齐齐哈尔市| 修文县| 云浮市| 汉川市| 武平县|