http://prototype.conio.net/dist/
下載(對Ajax支持的prototype--js函數庫):
http://code.google.com/p/jsonplugin/downloads/list
下載(Struts2的JSON插件):
第一:手動建立項目結構(類似于MyEclipse創建Web Pro項目的后臺操作)
1、新建文件夾結構如下:
Struts2json
|______WEB-INF
|_______classes
|_______src
|_______lib
2、復制Tomcat里conf文件夾里的web.xml到WEB-INF文件夾下,并修改web.xml文件
web.xml文件:
- <?xml version="1.0" encoding="ISO-8859-1"?>
-
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
-
- </web-app>
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
</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 version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <struts>
-
- </struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
</struts>
5、新建并手寫一個build.xml(必須已經安裝了Ant工具),并將build.xml放置到WEB-INF文件夾下 (MyEclipse內置了Ant)
build.xml文件:
- <?xml version="1.0"?>
- <project name="struts" basedir="." default="">
-
- <path id="classpath">
- <fileset dir="lib">
- <include name="*.jar"/>
- </fileset>
- <pathelement path="."/>
- </path>
-
- <target name="compile" description="Compile all source code">
- <javac destdir="classes" debug="true"
- deprecation="false" optimize="false" failonerror="true">
- <src path="src"/>
- <classpath refid="classpath"/>
- </javac>
- </target>
-
- </project>
<?xml version="1.0"?>
<project name="struts" basedir="." default="">
<path id="classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement path="."/>
</path>
<target name="compile" description="Compile all source code">
<javac destdir="classes" debug="true"
deprecation="false" optimize="false" failonerror="true">
<src path="src"/>
<classpath refid="classpath"/>
</javac>
</target>
</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 version="1.0" encoding="ISO-8859-1"?>
-
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
-
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.Struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- </web-app>
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.Struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2、編寫表現層ajaxtest.jsp頁面并放在與WEB-INF同一級目錄下。
ajaxtest . jsp文件:
- <%@ page language="java" contentType="text/html; charset=GBK"%>
- <script src="prototype-1.4.0.js" type="text/javascript">
- </script>
- <script language="JavaScript">
- function gotClick()
- {
- //請求的地址
- var url = 'JSONExample.action';
- //將form1表單域的值轉換為請求參數
- var params = Form.serialize('form1');
- //創建Ajax.Request對象,對應于發送請求
- var myAjax = new Ajax.Request(
- url,
- {
- //請求方式:POST
- method:'post',
- //請求參數
- parameters:params,
- //指定回調函數
- onComplete: processResponse,
- //是否異步發送請求
- asynchronous:true
- });
- }
- function processResponse(request)
- {
- $("show").innerHTML = request.responseText;
- }
- </script>
- <html>
- <head>
- <title>使用JSON插件</title>
- </head>
- <body>
- <form id="form1" name="form1" method="post">
- <INPUT TYPE="text" name="field1" id="field1"/><br>
- <INPUT TYPE="text" name="field2" id="field2"/><br>
- <INPUT TYPE="text" name="field3" id="field3"/><br>
- <INPUT TYPE="button" value="提交" onClick="gotClick();"/>
- </form>
- <div id="show">
- </div>
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=GBK"%>
<script src="prototype-1.4.0.js" type="text/javascript">
</script>
<script language="JavaScript">
function gotClick()
{
//請求的地址
var url = 'JSONExample.action';
//將form1表單域的值轉換為請求參數
var params = Form.serialize('form1');
//創建Ajax.Request對象,對應于發送請求
var myAjax = new Ajax.Request(
url,
{
//請求方式:POST
method:'post',
//請求參數
parameters:params,
//指定回調函數
onComplete: processResponse,
//是否異步發送請求
asynchronous:true
});
}
function processResponse(request)
{
$("show").innerHTML = request.responseText;
}
</script>
<html>
<head>
<title>使用JSON插件</title>
</head>
<body>
<form id="form1" name="form1" method="post">
<INPUT TYPE="text" name="field1" id="field1"/><br>
<INPUT TYPE="text" name="field2" id="field2"/><br>
<INPUT TYPE="text" name="field3" id="field3"/><br>
<INPUT TYPE="button" value="提交" onClick="gotClick();"/>
</form>
<div id="show">
</div>
</body>
</html>
3、編寫POJO(Action)在src下新建文件夾org,在org下新建文件夾jee(這里是建立包名),并新建類JSONExample.java放置在src/org/jee文件夾下。
JSONExample . java文件:
- package org.jee;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import com.opensymphony.xwork2.Action;
- import com.googlecode.jsonplugin.annotations.JSON;
-
- public class JSONExample
- {
- private int[] ints = {10, 20};
- private Map map = new HashMap();
- private String customName = "custom";
-
- private String field1;
-
- private transient String field2;
-
- private String field3;
-
- public String execute()
- {
- map.put("name", "yeeku");
- return Action.SUCCESS;
- }
-
- public String getField1() {
- return field1;
- }
-
- public void setField1(String field1) {
- this.field1 = field1;
- }
- public String getField2() {
- return field2;
- }
-
- public void setField2(String field2) {
- this.field2 = field2;
- }
-
- public String getField3() {
- return field3;
- }
-
- public void setField3(String field3) {
- this.field3 = field3;
- }
-
- public int[] getInts() {
- return ints;
- }
-
- public void setInts(int[] ints) {
- this.ints = ints;
- }
-
- public Map getMap() {
- return map;
- }
-
- public void setMap(Map map) {
- this.map = map;
- }
-
- @JSON(name="newName")
- public String getCustomName()
- {
- return this.customName;
- }
- }
package org.jee;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.googlecode.jsonplugin.annotations.JSON;
public class JSONExample
{
private int[] ints = {10, 20};
private Map map = new HashMap();
private String customName = "custom";
private String field1;
//'transient'不會被序列化
private transient String field2;
//沒有setter和getter方法的字段不會被序列化
private String field3;
public String execute()
{
map.put("name", "yeeku");
return Action.SUCCESS;
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
public int[] getInts() {
return ints;
}
public void setInts(int[] ints) {
this.ints = ints;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
@JSON(name="newName")
public String getCustomName()
{
return this.customName;
}
}
4、在struts.xml里配置Action,修改classes文件
struts.xml文件:
- <?xml version="1.0" encoding="GBK"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.i18n.encoding" value="UTF-8"/>
- <package name="example" extends="json-default">
- <action name="JSONExample" class="org.jee.JSONExample">
- <result type="json"/>
- </action>
- </package>
-
- </struts>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="example" extends="json-default">
<action name="JSONExample" class="org.jee.JSONExample">
<result type="json"/>
</action>
</package>
</struts>
注意:
第一:配置
- <constant name="struts.i18n.encoding" value="UTF-8"/>
<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 閱讀(2058)
評論(0) 編輯 收藏 所屬分類:
struts