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 阅读(2034) 评论(0)  编辑  收藏 所属分类: struts

只有注册用户登录后才能发表评论。


网站导航: