29
1 WebWork 培培 培培moxie( 培培 ) 学学学学培培 Java Jsp JavaServlet 培培培 培培 ,一 Web 培培培培 学学学学 培培 WebWork 培培培培培 培培 WebWork 培培培培培培培 培培培培 WebWork 培培培培培培

WebWork 培训

  • Upload
    maine

  • View
    143

  • Download
    0

Embed Size (px)

DESCRIPTION

WebWork 培训. 学员要求 : 掌握 Java 、 Jsp 、 JavaServlet ,并有一定的 Web 编程经验 课程目的 :理解 WebWork 的核心原理,掌握 WebWork 开发的相关知识,并能使用 WebWork 进行实际开发. 作者: moxie( 安子). 目录. WebWork 入门 WebWork 原理 WebWork 标签库和 EL WebWork 实例. WebWork 原理. MVC 模式 MVC 最初是在 Smalltalk-80 中被用来构建用户界面 Model 实现了应用领域的业务模型 - PowerPoint PPT Presentation

Citation preview

Page 1: WebWork 培训

1

WebWork 培训作者: moxie( 安子 )

学员要求:掌握 Java 、 Jsp 、 JavaServlet ,并有一定的 Web编程经验

课程目的:理解 WebWork 的核心原理,掌握 WebWork 开发的相关知识,并能使用 WebWork 进行实际开发

Page 2: WebWork 培训

2

目录 WebWork 入门

WebWork 原理

WebWork 标签库和 EL

WebWork 实例

Page 3: WebWork 培训

3

WebWork 原理MVC 模式

MVC 最初是在 Smalltalk-80 中被用来构建用户界面 Model 实现了应用领域的业务模型 View 用来展现模型中的数据和内部状态 Control 也称为 Dialog ,它协调 Model 与 View ,

把用户请求翻译成系统识别的事件

Page 4: WebWork 培训

4

J2ee Web Framework 之现状

WebWork: 最灵活、简单的 Web 框架 Spring Web Framework :最全面的 Web 框架 Struts: 资源最丰富的 Web 框架 Tapestry: 组件化最完美的 Web 框架 JSF: 最接近 asp.net 的 Web 框架 Portal: 最适合集成的 Web 框架

Page 5: WebWork 培训

5

WebWork 概述

XWork 1

WebWork 1

WebWork 2 Web

Non-web

Page 6: WebWork 培训

6

Action 请求的动作都对应于一个相应的 Action 一个 Action 是一个独立的工作单元和控制命令 它必需要实现 XWork 里的 Action 接口

public interface Action extends Serializable { public static final String SUCCESS = "success"; public static final String NONE = "none"; public static final String ERROR = "error"; public static final String INPUT = "input"; public static final String LOGIN = "login"; public String execute() throws Exception;}

Page 7: WebWork 培训

7

例子-用户注册

register.jsp

xwork.xml

registerSuccess.jspregister.action

Page 8: WebWork 培训

8

ActionSupport

提供 Action 常用功能的一个基类

错误消息的支持action and field specific errors

field errors are automatically supported by views

国际化支持1 resource bundle per action

pervasive UI support for retrieving messages

Page 9: WebWork 培训

9

ActionContext 一次 Action 调用都会创建一个 ActionContext 调用: ActionContext context = ActionContext.getContext()

在 WebWork 中,它通过包装提供了对 Servlet 的访问: HttpSession —— context.getSession()

HttpServletRequest parameters —— context.getParameters()

ServletActionContext ,继承 ActionContext 。可以直接访问 servlet 相关的 API :PageContext , HttpServletRequest , HttpServletResponse , ServletConfig , ServletContext

Page 10: WebWork 培训

10

二、 WebWork 原理XWork 的层次:

ActionProxy ActionInvocation Action

ActionProxy :管理 Action 的生命周期,它是设置和执行 Action的起始点。

ActionInvocation :在 ActionProxy 层之下,它表示了 Action 的执行状态。它持有 Action 实例和所有的 Interceptor

Page 11: WebWork 培训

11

WebWork 原理

Page 12: WebWork 培训

12

Interceptors "Practical AOP"

very simple, no external dependencies allows you to intercept action invocations

Help decouple and componentize your code

Interceptors are organized into 'stacks' lists of interceptors applied in sequence. applied to any action or package of actions

WebWork is mostly implemented as a series of XWork interceptors

Page 13: WebWork 培训

13

例子- TimerInterceptor public class TimerInterceptor implements Interceptor {

. . .

public String intercept(ActionInvocation dispatcher) ...{long startTime = System.currentTimeMillis(); String result = dispatcher.invoke(); long exTime = System.currentTimeMillis() - startTime; log.info(dispatcher.getProxy().getActionName() + " ran in "

+ exTime + "ms."); return result;

} }

xwork.xml<interceptors>

<interceptor name="timer" class="com.opensymphony.xwork.interceptor.TimerInterceptor"/>

</interceptors>

Page 14: WebWork 培训

14

Interceptor 的执行顺序 Interceptor 截获 Action 的执行,并在它的之前或之

后调用相应的方<interceptor-stack name="xaStack">

<interceptor-ref name="thisWillRunFirstInterceptor"/><interceptor-ref name="thisWillRunNextInterceptor"/><interceptor-ref name="thisWillRunLastInterceptor"/>

</interceptor-stack>

thisWillRunFirstInterceptor thisWillRunNextInterceptor thisWillRunLastInterceptor MyAction1 MyResult (result) thisWillRunLastInterceptor thisWillRunNextInterceptorthisWillRunFirstInterceptor

Page 15: WebWork 培训

15

ValueStack 由 OGNL 框架实现 可以把它简单的看作一个 List

Stack Object :放入 stack 中的对象,一般是 action 。 Stack Context ( map ): stack 上下文,它包含一

些列对象,包括 request/session/attr/application map 等。

EL :存取对象的任意属性,调用对象的方法,遍历整个对象结构图。

Page 16: WebWork 培训

16

ResultType Result

它是 Action 执行之后返回的一个字符串常量它表示 Action 执行完成的状态。

ResultType它是一个类它在 Action 执行,并返回 result 之后调用它用来决定 WebWork 使用什么方式展现界面

<result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/>

<result name="success" type="dispatcher">/register/registerSuccess.jsp</result>

Page 17: WebWork 培训

17

三、 WebWork 标签库和 EL <ww:property value=“”/> 取值输出

<ww:iterator value=“” status=“”/> 迭代标签,可以输出Collection, Iterator, Enumeration, Map, array, XML Node, or XML NodeListstatus: 用来取得迭代器的状态: getCount(), getIndex(), isFirst(), isLast(), isEven(), isOdd()

<ww:if test=“”> </ww:if><ww:elseif test=“”></ww:eleseif><ww:else></ww:else>

Page 18: WebWork 培训

18

Expression Language

OGNL Expression Result

employee.name getEmployee().getName()

employee.toString getEmployee().toString()

employee.relations[0] First element of the Relations collection

name in {null, "Rick"} True is name is null or Rick

employee.relations.{name} Calls getName() on each relations in the collection returning a new collection

例子-员工登记

Page 19: WebWork 培训

19

四、实例 验证 多模块处理 一个 Action 类多方法 文件上传 防止重复提交 进度条显示

Page 20: WebWork 培训

20

验证 XWork 验证框架

验证 Action 的属性 减弱验证与 Action 之间的耦合

验证信息存储在独立的 xml 文件中验证出错信息放置在 Action 中

可插拔的验证类 验证机制有 Interceptor 实现

Page 21: WebWork 培训

21

已提供的验证类Validator Result

RequiredField field != null

RequiredString field != null & string.length() > 0

IntRange Integer is in a given range

DateRange Date is in a given range

Email Valid email field

URL Valid URL field

Expression/FieldExpression Any OGNL expression evaluates to trueeg. pet.name != "dog"Allows you to create very powerful validations using just XML and your existing model

Page 22: WebWork 培训

22

例子-注册验证

register.jsp

xwork.xml

registerSuccess.jspregister.action

RegisterAction-validation.xml

register.jsp验证出错

Page 23: WebWork 培训

23

多模块 Include: 使用多个 XWork配置文件 Package:package 之间可以继承 Namespace :用来区分不同 package 中的 action

Page 24: WebWork 培训

24

一个 Action 类多方法 方法要求:无参数,返回一个字符串类型,抛出

Exception ,例如:public String doDefault() throws Excetpion{

return INPUT;}

定义方式1 、直接访问, actionName!methodName.action 。例如: register!doDefault.action2 、在 XWork 中定义,使用 method 属性。例如:

<action name="registerDefault" class="com.skyon.demo.register.RegisterAction" method=“doDefault”>

…………

Page 25: WebWork 培训

25

文件上传

fileUpload.jsp

xwork.xml

uploadSuccess.jspfileUpload.action

Page 26: WebWork 培训

26

防止重复提交( double click ) 在页面中设置 <ww:token />

<input type="hidden" name="webwork.token.name" value="webwork.token"/><input type="hidden" name="webwork.token" value="5J573PYGWC131FIM39Y03PYZX5P2Z0JC"/>

session.setAttribute(tokenName, token);

配置拦截器 TokenInterceptor TokenSessionStoreInterceptor :保存了上次操作

的 Invocation

Page 27: WebWork 培训

27

进度条显示

waitInput

xwork.xml waitSuccess.jsp

wait.actionwait.jsp

Page 28: WebWork 培训

28

总结 重用(松耦合 /无侵入): Action 接口、 Action 与

Web无关、 Interceptor

功能强大: EL 、验证框架

灵活: FormBean/ 数据 Model 、多视图支持

Page 29: WebWork 培训

29

结束

谢谢各位!