struts 2
Download
Skip this Video
Download Presentation
Struts 2

Loading in 2 Seconds...

play fullscreen
1 / 32

Struts 2 - PowerPoint PPT Presentation


  • 256 Views
  • Uploaded on

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:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Struts 2' - lefty


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
struts 2

Struts 2

Web Framework

slide2
提纲
  • struts基本概念
  • 标签tag
  • Ognl
  • 国际化
  • 验证
  • 类型转换
  • 拦截器
slide5
页面流转

Index.html:

-----------------------

hello

hello.jsp: -----------------------

Hello:

hello_form.jsp:

-----------------------

Say "Hello" to:

web xml
Web.xml

Struts2 Hello

struts2

org.apache.struts2.dispatcher.FilterDispatcher

struts2

/*

struts xml
Struts.xml

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

/WEB-INF/jsp/hello_form.jsp

/WEB-INF/jsp/hello.jsp

helloworld java
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;
  • }
  • }
slide9
直观感受:
  • Struts帮我们把Http请求填充到Action对象中
  • Struts帮我们把Http响应中的动态数据绑定到界面,提供一组标签来丰富web开发。
  • Struts接收所有请求然后根据配置交付给处理者(action),处理者扮演控制器装载数据并呈现页面。
  • Struts 提供页面流控制,避免直接访问jsp页面来装载数据,分离业务计算和页面呈现。(试试给name提供一个缺省值)
  • 有点复杂,后面我们会尝试简化部分编程模型。
struts web
基于Struts的web编程建议
  • 避免直接访问任意的jsp页面
  • 使用Struts标签替代html标签
  • 避免在页面上进行业务计算,而应由Action提供计算好的数据
slide12
主要配置文件

我们要参考的

  • struts2_core_xx.jar/org/apache/struts2/default.properties
  • struts2_core_xx.jar/struts-default.xml

我们要配置的

  • web.xml
  • struts.xml
s truts xml struts 2 0 dtd
struts.xml /struts-2.0.dtd

目前最关心的:

  • constant
  • include
  • package

action

result-types

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

register.jsp

register-result.jsp

public class RegisterAction extends ActionSupport{

public String execute(){

//检查…

return “success”;

}

}

result1
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);

com opensymphony xwork2 action
com.opensymphony.xwork2. Action预定义结果
  • String SUCCESS = "success";
  • String NONE = "none";
  • String ERROR = "error";
  • String INPUT = "input";
  • String LOGIN = "login";
result types struts default xml
ResultTypes(struts-default.xml)
  • Dispatcher Result
  • Redirect Result
  • Redirect Action Result

a nnotation xml
Annotation/ Xml/约定

开启约定配置:

  • struts.enable.DynamicMethodInvocation=true

当前的2个Annotation

  • Result
  • Results

开启约定配置的问题:

  • 包分布
  • 危险的方法探测?
  • 权衡
slide21
线程模型
  • Action不同于servlet
actioncontext
ActionContext
  • ActionContext
  • ServletActionContext
control tags
Control Tags
  • if/elseif/esle *
  • append
  • generator
  • iterator *
  • merge
  • sort
  • subset
data tags
Data Tags
  • a *
  • action
  • bean
  • date
  • debug *
  • i18n
  • include
  • param *
  • property *
  • push
  • set
  • text
  • url *
form ui tags
Form UI Tags
  • checkbox *
  • checkboxlist *
  • combobox *
  • doubleselect
  • head *
  • file *
  • form *
  • hidden *
  • label *
  • optiontransferselect
  • optgroup
  • password *
  • radio *
  • reset *
  • select *
  • submit *
  • textarea *
  • textfield *
  • token *
  • updownselect
non form ui tags
Non-Form UI Tags
  • actionerror *
  • actionmessage
  • component
  • div
  • fielderror
  • table
  • tabbedPanel
  • tree
  • treenode
ajax tags
Ajax Tags
  • a
  • autocompleter
  • bind
  • datetimepicker *
  • head
  • submit
slide29
问题
  • 各标签中容易混淆的id/name/var属性
  • Ajax tags
slide31
缺省验证器
  • Xwork_xx.jar/com/opensymphony/xwork2/validator/validators/default.xml
  • com.opensymphony.xwork2.validator.Validator接口
struts web1
基于Struts的web编程建议
  • 通过各种方法减少xml配置。
  • 避免Servletapi直接依赖:尽量使用ActionContext而非ServletActionContext
ad