1 / 32

Struts 2

Struts 2. Web Framework. 提纲. struts 基本概念 标签 tag Ognl 国际化 验证 类型转换 拦截器. Hello. Struts 项目发布结构. 页面流转. Index.html: ----------------------- <a href =" HelloWorld.action ">hello</a>. hello.jsp: ----------------------- Hello:<s:property value="name"/>. hello_form.jsp:

lefty
Download Presentation

Struts 2

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. Struts 2 Web Framework

  2. 提纲 • struts基本概念 • 标签tag • Ognl • 国际化 • 验证 • 类型转换 • 拦截器

  3. Hello

  4. Struts 项目发布结构

  5. 页面流转 Index.html: ----------------------- <a href="HelloWorld.action">hello</a> hello.jsp: ----------------------- Hello:<s:property value="name"/> hello_form.jsp: ----------------------- <h3>Say "Hello" to: </h3> <s:form action="HelloWorld"> <s:textfield label="Name" name="name" /> <s:submit/> </s:form>

  6. Web.xml <web-app id="StrutsHello" 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>Struts2 Hello</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

  7. Struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="example" namespace="/" extends="struts-default"> <action name="HelloWorld" class="hello.HelloWorld"> <result name="input">/WEB-INF/jsp/hello_form.jsp</result> <result name="success">/WEB-INF/jsp/hello.jsp</result> </action> </package> </struts>

  8. HelloWorld.java • public class HelloWorld extends ActionSupport { • private String name=""; • public String execute() throws Exception { • if (isInvalid(getName())) return INPUT; • return SUCCESS; • } • private boolean isInvalid(String value) { • return (value == null || value.length() == 0); • } • public String getName() { • return name; • } • public void setName(String name) { • this.name = name; • } • }

  9. 直观感受: • Struts帮我们把Http请求填充到Action对象中 • Struts帮我们把Http响应中的动态数据绑定到界面,提供一组标签来丰富web开发。 • Struts接收所有请求然后根据配置交付给处理者(action),处理者扮演控制器装载数据并呈现页面。 • Struts 提供页面流控制,避免直接访问jsp页面来装载数据,分离业务计算和页面呈现。(试试给name提供一个缺省值) • 有点复杂,后面我们会尝试简化部分编程模型。

  10. 基于Struts的web编程建议 • 避免直接访问任意的jsp页面 • 使用Struts标签替代html标签 • 避免在页面上进行业务计算,而应由Action提供计算好的数据

  11. 基本概念

  12. 主要配置文件 我们要参考的 • struts2_core_xx.jar/org/apache/struts2/default.properties • struts2_core_xx.jar/struts-default.xml 我们要配置的 • web.xml • struts.xml

  13. struts.xml /struts-2.0.dtd 目前最关心的: • constant • include • package action result-types

  14. 体系结构

  15. Action是Struts的核心 • Mvc 2模型 • Struts请求响应流程 • 减少对ServletApi的直接依赖(参考ActionContext) • 页面和Action的数据关系(更多内容参考后面的ognl) • 更多的Action使用和配置范例。

  16. Result <action name="register" class="example.register.RegisterAction" > <result name=“input“ >register.jsp</result><!—默认结果类型就是dispatcher --> <result name="success" type="dispatcher">register-result.jsp</result> </action> public class RegisterAction extends ActionSupport{ public String execute(){ //检查… return “success”; } }

  17. Result • Result 是一个Action方法返回的字符串 • Result被设置为一种视图 • Result 类型是com.opensymphony.xwork2. Result的实现类 • type=“dispatcher” : org.apache.struts2.dispatcher.ServletDispatcherResult: 最终会执行类似下面代码: request.getRequestDispatcher(“register.jsp”).include(request, response); 或: request.getRequestDispatcher(“register.jsp”).forward(request, response);

  18. com.opensymphony.xwork2. Action预定义结果 • String SUCCESS = "success"; • String NONE = "none"; • String ERROR = "error"; • String INPUT = "input"; • String LOGIN = "login";

  19. ResultTypes(struts-default.xml) • Dispatcher Result <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> • Redirect Result <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> • Redirect Action Result <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

  20. Annotation/ Xml/约定 开启约定配置: • struts.enable.DynamicMethodInvocation=true 当前的2个Annotation • Result • Results 开启约定配置的问题: • 包分布 • 危险的方法探测? • 权衡

  21. 线程模型 • Action不同于servlet

  22. ActionContext • ActionContext • ServletActionContext

  23. Tag

  24. Control Tags • if/elseif/esle * • append • generator • iterator * • merge • sort • subset

  25. Data Tags • a * • action • bean • date • debug * • i18n • include • param * • property * • push • set • text • url *

  26. Form UI Tags • checkbox * • checkboxlist * • combobox * • doubleselect • head * • file * • form * • hidden * • label * • optiontransferselect • optgroup • password * • radio * • reset * • select * • submit * • textarea * • textfield * • token * • updownselect

  27. Non-Form UI Tags • actionerror * • actionmessage • component • div • fielderror • table • tabbedPanel • tree • treenode

  28. Ajax Tags • a • autocompleter • bind • datetimepicker * • head • submit

  29. 问题 • 各标签中容易混淆的id/name/var属性 • Ajax tags

  30. 验证 validation

  31. 缺省验证器 • Xwork_xx.jar/com/opensymphony/xwork2/validator/validators/default.xml • com.opensymphony.xwork2.validator.Validator接口

  32. 基于Struts的web编程建议 • 通过各种方法减少xml配置。 • 避免Servletapi直接依赖:尽量使用ActionContext而非ServletActionContext

More Related