速動畫教程第二十三集 WebWork2 示例


下載地址: http://this.oksonic.cn
?

準備工作:

??? Eclipse3.1.1?? MyEclipse4.1.1?? Tomcat5.1.x

??? 下載 WebWork 完整包有 52M,現在最新版是 2.2.2

??? http://www.opensymphony.com/webwork/

?

新建工程 名稱為 test ,使用 j2ee 1.4

解壓包中的 webwork-2.2.2.jar 文件到項目的 lib 目錄

解壓包中的 lib\default\*.jar 文件到項目的 lib 目錄,注:如果使用的是2.1.x的包需要解壓的路徑為 lib\core\*.jar

?

配置 web.xml 文件,文件內容如下:

<? xml version = "1.0" encoding = "UTF-8" ?>

< web-app 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" >

??? < servlet >

?????? < servlet-name > webwork </ servlet-name >

?????? < servlet-class > com.opensymphony.webwork.dispatcher.ServletDispatcher </ servlet-class >

??? </ servlet >

??? < servlet-mapping >

?????? < servlet-name > webwork </ servlet-name >

?????? < url-pattern > *.action </ url-pattern >

??? </ servlet-mapping >

</ web-app >

WebWork 的Action是以 *.action 方式進行處理的,而 Struts 是以 *.do 方式處理

?

?

此示例需要使用到以下結構

?

模型: User.java???????

控制: LoginAction.java

視圖: index.jsp ok.jsp error.jsp

配置: xwork.xml

?

新建 User.java 文件,內容如下:

package com.test.model.bo;

?

public class User {

?

??? private String userName;

??? private String userPwd;

???

??? public String getUserName() {

??????? return userName;

??? }

??? public void setUserName(String userName) {

??????? this.userName = userName;

??? }

??? public String getUserPwd() {

??????? return userPwd;

??? }

??? public void setUserPwd(String userPwd) {

??????? this.userPwd = userPwd;

??? }

}

?

新建一個 class 文件名為: LoginAction ,并實現 com.opensymphony.xwork.Action 接口,此類和 Struts Action 相當相似,不過是以接口的方式實現,而 Struts 的是 Action 是以繼承的方式實現,它們都有一個 execute() 方法

? 代碼如下:

package com.test.web.action;

?

import com.opensymphony.xwork.Action;

import com.test.model.bo.User;

?

public class LoginAction implements Action {

?

??? // 此屬性一定要進行初始化操作

??? private User user = new User();

???

??? public String execute() throws Exception {

??????? // 可以在這里調用Service層來進行驗證,這里只驗證用戶名

??????? if (user.getUserName().equals("oksonic"))

??????????? return this.SUCCESS;

??????? else

??????????? return this.ERROR;

??? }

?

??? public User getUser() {

??????? return user;

??? }

?

}

?

綠色字體為新增的一個屬性,并創建它的 get 方法

?

src 目錄下新建一個 xwork.xml 文件,此文件相當于 Struts 的配置文件,文件內容如下:

<! DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd" >

< xwork >

??? < include file = "webwork-default.xml" />

??? < package name = "default" extends = "webwork-default" >

?????? < action name = "login" class = "com.test.web.action.LoginAction" >

?????????? <!--? 這里的意思是登錄成功后跳轉到 /ok.jsp 頁面 -->

?????????? < result name = "success" type = "dispatcher" >

????????????? < param name = "location" > /ok.jsp </ param >

?????????? </ result >

???????????????????? <!--? 登錄失敗后跳轉到 / error .jsp 頁面 -->

?????????? < result name = "error" type = "dispatcher" >

????????????? < param name = "location" > /error.jsp </ param >

?????????? </ result >

?????????? <!--? 此段代碼為作用是將 request 請求的參數傳遞到 action -->

?????????? < interceptor-ref name = "params" />

?????? </ action >

??? </ package >

</ xwork >

?

?

新建三個 jsp 文件

?

index.jsp 內容如下:

< html >

??? < head >

?????? < title > INDEX </ title >

??? </ head >

?

??? < body >

??????

?????? < DIV align = "center" >

?????????? LOGIN

?????? </ DIV >

?????? < FORM action = "login.action" method = "post" >

?????????? < DIV align = "center" >

????????????? < TABLE border = "1" cellpadding = "0" cellspacing = "0" >

????????????????? < TR >

???????????????????? < TD >

???????????????????????? username:

???????????????????? </ TD >

???????????????????? < TD >

???????????????????????? < INPUT type = "text" size = "10" name = "user.userName" >

???????????????????? </ TD >

????????????????? </ TR >

????????????????? < TR >

???????????????????? < TD >

???????????????????????? password:

???????????????????? </ TD >

???????????????????? < TD >

???????????????????????? < INPUT type = "text" size = "10" name = "user.userPwd" >

???????????????????? </ TD >

????????????????? </ TR >

??? ????????????? < TR >

???????????????????? < TD colspan = "2" align = "center" >

???????????????????????? < INPUT type = "submit" value = "submit" name = "submit" >

???????????????????????? < INPUT type = "reset" value = "reset" name = "reset" >

???????????????????? </ TD >

????????????????? </ TR >

????????????? </ TABLE >

?????????? </ DIV >

?????? </ FORM >

??? </ body >

</ html >

?

這個頁面使用的全是 html 的語法, user.userPwd 為 Action 中 User 對像 的 userPwd 屬性

?

ok.jsp 文件內容如下:

<%@ taglib prefix = "ww" uri = "/webwork" %>

< html >

??? < head >

?????? < title > OK </ title >

??? </ head >

??? < body >

?????? userName= < ww:property value = "user.userName" />

?????? < br >

?????? userPwd= < ww:property value = "user.userPwd" />

?????? < br >

??? </ body >

</ html >

?

這里使用了WebWork的標簽

?

error.jsp 文件內容如下:

< html >

??? < head >

?????? < title > ERROR </ title >

??? </ head >

??? < body >

?????? Error !!!

??? </ body >

</ html >

?

已經成功了!!!,對于 webwork 我也是剛學了這么一點,以后再有什么研究成果會再次展示給大家的!!