day27_struts2のPPT学习1

来源:互联网 发布:希捷官方数据恢复 编辑:程序博客网 时间:2024/05/17 04:12

day27_struts2のPPT学习1

01 Struts2开发入门.ppt

什么是Struts2

  • Struts2 是一个非常优秀的MVC框架,基于Model2 设计模型
  • 由传统Struts1WebWork两个经典框架发展而来

  • Strust2 核心功能

    1. 允许POJO(Plain Old Java Objects)对象 作为Action
    2. Action的execute 方法不再与Servlet API**耦合,更易测试**
    3. 支持更多视图技术(JSP、FreeMarker、Velocity
    4. 基于Spring AOP思想的拦截器机制,更易扩展
    5. 更强大、更易用输入校验功能
    6. 整合Ajax支持

Struts2的下载和安装及目录结构

  • http://struts.apache.org/download.cgi 去下载Struts2 最新版

  • struts2目录结构:

    1. apps 该文件夹包含了基于struts2 的示例应用,这些示例应用对于学习者是非常有用的
    2. docs 该文件夹下包含了struts2 相关文档,包括struts2 快速入门、struts2的文档以及API文档等
    3. lib 该文件夹下包含了Struts2框架和核心类库,以及struts2第三方插件类库
    4. src 该文件夹下包含了Struts2框架的全部源代码

开发时没必要将lib目录下jar文件全部复制到项目中

Struts2的入门理论

用户请求——>Web容器(web.xml)——>StrutsPrepareAndExecuteFilter(struts.xml)——>n多个Action

第一个Struts2应用入门例子步骤

1.创建WEB 工程2.导入必要jar包3.编写JSP 页面4.编写Action 服务器端处理逻辑5.进行框架配置web.xml、struts.xml6.运行测试

导入必要jar包到web工程

Struts运行必要jar包

   struts2-core-2.3.1.1.jar:Struts 2框架的核心类库   xwork-core-2.3.1.1.jar:Command模式框架,WebWork和Struts2都基于xwork    ognl-3.0.3.jar:对象图导航语言(Object Graph Navigation Language),                            struts2框架通过其读写对象的属性   freemarker-2.3.18.jar:Struts 2的UI标签的模板使用FreeMarker编写   commons-logging-1.1.x.jar:ASF出品的日志包,Struts 2框架使用这个日志                                                包来支持Log4J和JDK 1.4+的日志记录。   commons-fileupload-1.2.2.jar: 文件上传组件,2.1.6版本后需要加入此文件   commons-io-2.0.1.jar:传文件依赖的jar包   commons-lang-2.5.jar:对java.lang包的增强

开发中为了方便导入,可以使用app/struts2-blank.war 携带jar包

编写JSP页面

helloword.jsp (发起请求页面)

<a href="${pageContext.request.contextPath}/hello.action"> helloworld</a>

添加对Struts2 框架的访问链接,默认情况下框架接受以.action请求,并进行处理

success.jsp (结果页面)

<h1>你好,Struts2<h1>

结果页面显示 struts2框架访问成功

编写Action处理访问Struts2框架请求

public class HelloAction {    public String execute(){        System.out.println("hello world");        return "success"; // 结果页面命名    }}

struts2 的Action类似以前编写的Servlet程序,可以处理用户提交请求,但是Struts2的Action可以POJO对象

配置Struts2核心控制器

web.xml

<filter>    <filter-name>struts2</filter-name>    <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter          </filter-class></filter><filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern></filter-mapping>

过滤器配置/* , 但是struts2 默认处理.action结尾请求,分发到相应Action类

配置struts.xml

  • 在src目录下建立struts.xml
  • 参考doc下 hello-world-using-struts-2.html
<?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.devMode" value="true"/>    <package name="basicstruts2" extends="struts-default">        <action name="index">            <result>/index.jsp</result>        </action>    </package></struts>

Struts2的处理流程

用户请求——>StrutsPrepareAndExecuteFilter                    |                    V                Interceptor(Struts2内置的一些拦截器或用户自定义拦截器)                    |                    V                  Action(用户编写的action类,类似struts1中的Action                    |                    V                  Result(类似struts1中的forward)                    |                    V        响应  <—— Jsp/htmlStruts2请求流程1、客户端发送请求2、请求先通过ActionContextCleanUp-->FilterDispatcher3、FilterDispatcher通过ActionMapper来决定这个Request需要调用哪个Action4、如果ActionMapper决定调用某个Action,FilterDispatcher把请求的处理交给ActionProxy,这儿已经转到它的Delegate--Dispatcher来执行5、ActionProxy根据ActionMapping和ConfigurationManager找到需要调用的Action类6、ActionProxy创建一个ActionInvocation的实例7、ActionInvocation调用真正的Action,当然这涉及到相关拦截器的调用8、Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果result。* 如果要在返回result之前做些什么,可以实现PreResultListener。

拦截器概述(struts-default.xml)

<struts>    <package name="struts-default" abstract="true">        <interceptors>            <!--声明拦截器-->            <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>            <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>            <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>            <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>            <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>            <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>            ......            <!--声明拦截器栈,可以把拦截器栈看成一个List集合,拦截器栈中的拦截器按声明顺序执行 -->            <interceptor-stack name="defaultStack">                <interceptor-ref name="i18n"/>                <interceptor-ref name="prepare"/>                <interceptor-ref name="chain"/>                <interceptor-ref name="modelDriven"/>                <interceptor-ref name="fileUpload"/>                <interceptor-ref = name="params">                    <param name="excludeParam">dojo\..*,^struts\..*</param>                </interceptor-ref>            </interceptor-stack>        </interceptors>        <!--配置执行那个拦截器栈,这里默认执行defaultStack拦截器栈 -->        <default-interceptor-ref name="defaultStack"/>        <default-class-ref class="com.opensymphony.xwork2.ActionSupport"/>    </package</struts>

拦截器概述(演示过滤器和拦截器的执行顺序)

使用如下三个拦截器演示struts的执行流程(断点演示)默认的是defaultStack<interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/><interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/><interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>

配置文件struts.xml无提示问题解决

  1. 方案一 连接网络
    myeclipse会自动下载缓存 struts-2.3.dtd
  2. 方案二 无网络
    在struts2-core-2.3.7.jar中含有 struts-2.3.dtd
    将其复制到硬盘任意位置 例如:G:\struts-2.3.dtd
    myeclipse - window - preferences - 搜索xml catalog - Add
    注意key的类型要选用url

Config Brower插件使用

需要 struts2-config-browser-plugin-2.3.7.jar
浏览器访问ConfigBrowser插件首页地址
http://localhost:8080/struts01/config-browser/index.action

0 0
原创粉丝点击