1、在Eclipse WTP中建立一個Dynamic Web Project,命名為ww2example1,Target Runtime選擇配置好的Apache Tomcat 5.5。Project Facets頁選擇Web Module Version為2.4(其實就是Servlet版本),Java Version為5.0。WebDoclet不選。路徑設定使用預設值。
2、将WebWork2.2.4的庫檔案拷貝到項目的WebContent/WEB-INF/lib目錄下。拷貝的jar檔案在webwork目錄和子目錄lib下,這個入門程式隻需要lib子目錄下的default子目錄中的jar檔案和webwork目錄下的webwork-2.2.4.jar檔案。
3、修改WebContent/WEB-INF/web.xml檔案,增加WebWork需要的Filter。修改後的web.xml檔案内容為:
xml 代碼
xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--2.2.4版本真的很不同了呀,我以前一直用2.1.7的
最不同的地方是WEB.XML裡的控制器不是Servlet了,而是Filter
-->
<filter>
<filter-name>webwork</filter-name>
<filter-class>
com.opensymphony.webwork.dispatcher.FilterDispatcher
<filter-class>
filter>
<filter-mapping>
<filter-name>webworkfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
編寫一個Action:example.FirstAction,擴充com.opensymphony.xwork.Action接口,這個Action什麼都不做,execute方法隻是直接傳回SUCCESS常量。代碼如下:
java 代碼
package example;
import com.opensymphony.xwork.Action;
public class FirstAction implements Action {
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
在工程的源碼路徑(src)中建立一個xwork.xml檔案。這個檔案主要用來定義Action和Interceptor,本練習的xwork.xml檔案内容如下:
xml 代碼
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<default-action-ref name="welcome" />
<action name="welcome" class="example.FirstAction">
<result>/welcome.jspresult>
action>
package>
xwork>
在項目的WebContent目錄下建立一個welcome.jsp頁面,内容如下:
xml 代碼
<html>
<head>
<title>Welcome to WebWork</title>
</head>
<body>
<h3>Welcome to WebWork!</h3>
</body>
</html>
在Eclipse中啟動 ApacheTomcat5.5,打開浏覽器,在位址欄輸入:
java 代碼
http://localhost:8080/ww2example1/welcome.action
出現期望的運作結果。
OK,第一個WebWork例子運作成功。