`
senton
  • 浏览: 202186 次
  • 性别: Icon_minigender_1
  • 来自: 紫禁城
社区版块
存档分类
最新评论

WebWork 入门示例

阅读更多
准备工作:
    Eclipse3.2   MyEclipse5.0  Tomcat5.5.x    下载 WebWork 完整包
    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 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">
 <display-name>
 test</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 
    <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 我也是刚学了这么一点,以后再有什么研究成果会再次展示.
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics