1 / 29

WebWork 培训

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

maine
Download Presentation

WebWork 培训

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WebWork培训 • 学员要求:掌握Java、Jsp、JavaServlet,并有一定的Web编程经验 • 课程目的:理解WebWork的核心原理,掌握WebWork开发的相关知识,并能使用WebWork进行实际开发 作者:moxie(安子)

  2. 目录 • WebWork入门 • WebWork原理 • WebWork标签库和EL • WebWork实例

  3. WebWork原理 MVC模式 • MVC最初是在Smalltalk-80中被用来构建用户界面 • Model实现了应用领域的业务模型 • View用来展现模型中的数据和内部状态 • Control也称为Dialog,它协调Model与View,把用户请求翻译成系统识别的事件

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

  5. WebWork概述 WebWork 2 Web WebWork 1 XWork 1 Non-web

  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; }

  7. 例子-用户注册 register.jsp register.action registerSuccess.jsp xwork.xml

  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

  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

  10. 二、WebWork原理 XWork的层次: ActionProxy ActionInvocation Action ActionProxy:管理Action的生命周期,它是设置和执行Action的起始点。 ActionInvocation:在ActionProxy层之下,它表示了Action的执行状态。它持有Action实例和所有的Interceptor

  11. 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

  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>

  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 thisWillRunNextInterceptor thisWillRunFirstInterceptor

  15. ValueStack • 由OGNL框架实现 • 可以把它简单的看作一个List • Stack Object:放入stack中的对象,一般是action。 • Stack Context(map):stack上下文,它包含一些列对象,包括request/session/attr/application map等。 • EL:存取对象的任意属性,调用对象的方法,遍历整个对象结构图。

  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>

  17. 三、WebWork标签库和EL • <ww:property value=“”/>取值输出 • <ww:iterator value=“” status=“”/>迭代标签,可以输出Collection, Iterator, Enumeration, Map, array, XML Node, or XML NodeList status:用来取得迭代器的状态:getCount(), getIndex(), isFirst(), isLast(), isEven(), isOdd() • <ww:if test=“”> </ww:if> <ww:elseif test=“”></ww:eleseif> <ww:else></ww:else>

  18. Expression Language 例子-员工登记

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

  20. 验证 • XWork验证框架 • 验证Action的属性 • 减弱验证与Action之间的耦合 验证信息存储在独立的xml文件中 验证出错信息放置在Action中 • 可插拔的验证类 • 验证机制有Interceptor实现

  21. 已提供的验证类

  22. 例子-注册验证 registerSuccess.jsp register.jsp register.action xwork.xml 验证出错 register.jsp RegisterAction-validation.xml

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

  24. 一个Action类多方法 • 方法要求:无参数,返回一个字符串类型,抛出Exception,例如: public String doDefault() throws Excetpion{ return INPUT; } • 定义方式 1、直接访问,actionName!methodName.action。例如:register!doDefault.action 2、在XWork中定义,使用method属性。例如: <action name="registerDefault" class="com.skyon.demo.register.RegisterAction" method=“doDefault”> …………

  25. 文件上传 fileUpload.jsp fileUpload.action uploadSuccess.jsp xwork.xml

  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

  27. 进度条显示 wait.jsp waitInput wait.action xwork.xml waitSuccess.jsp

  28. 总结 • 重用(松耦合/无侵入):Action接口、Action与Web无关、Interceptor • 功能强大:EL、验证框架 • 灵活:FormBean/数据Model、多视图支持

  29. 结束 谢谢各位!

More Related