hello. hello.jsp: ----------------------- Hello:. hello_form.jsp:">
Struts 2
This presentation is the property of its rightful owner.
Sponsored Links
1 / 32

Struts 2 PowerPoint PPT Presentation


  • 195 Views
  • Uploaded on
  • Presentation posted in: General

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:

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


Struts 2

提纲

  • struts基本概念

  • 标签tag

  • Ognl

  • 国际化

  • 验证

  • 类型转换

  • 拦截器


Hello

Hello


Struts

Struts 项目发布结构


Struts 2

页面流转

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>


Web xml

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>


Struts xml

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>


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;

  • }

  • }


Struts 2

直观感受:

  • Struts帮我们把Http请求填充到Action对象中

  • Struts帮我们把Http响应中的动态数据绑定到界面,提供一组标签来丰富web开发。

  • Struts接收所有请求然后根据配置交付给处理者(action),处理者扮演控制器装载数据并呈现页面。

  • Struts 提供页面流控制,避免直接访问jsp页面来装载数据,分离业务计算和页面呈现。(试试给name提供一个缺省值)

  • 有点复杂,后面我们会尝试简化部分编程模型。


Struts web

基于Struts的web编程建议

  • 避免直接访问任意的jsp页面

  • 使用Struts标签替代html标签

  • 避免在页面上进行业务计算,而应由Action提供计算好的数据


Struts 2

基本概念


Struts 2

主要配置文件

我们要参考的

  • 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


Struts 2

体系结构


Action struts

Action是Struts的核心

  • Mvc 2模型

  • Struts请求响应流程

  • 减少对ServletApi的直接依赖(参考ActionContext)

  • 页面和Action的数据关系(更多内容参考后面的ognl)

  • 更多的Action使用和配置范例。


Result

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

}

}


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 <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"/>


A nnotation xml

Annotation/ Xml/约定

开启约定配置:

  • struts.enable.DynamicMethodInvocation=true

    当前的2个Annotation

  • Result

  • Results

    开启约定配置的问题:

  • 包分布

  • 危险的方法探测?

  • 权衡


Struts 2

线程模型

  • Action不同于servlet


Actioncontext

ActionContext

  • ActionContext

  • ServletActionContext


Struts 2

Tag


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


Struts 2

问题

  • 各标签中容易混淆的id/name/var属性

  • Ajax tags


Validation

验证 validation


Struts 2

缺省验证器

  • Xwork_xx.jar/com/opensymphony/xwork2/validator/validators/default.xml

  • com.opensymphony.xwork2.validator.Validator接口


Struts web1

基于Struts的web编程建议

  • 通过各种方法减少xml配置。

  • 避免Servletapi直接依赖:尽量使用ActionContext而非ServletActionContext


  • Login