Structs2入门一

一、Struts2框架介绍
1、三大框架 : 是企业主流 JavaEE 开发的一套架构
Struts2 + Spring + Hibernate

2、 什么是框架?为什么学习框架?
框架 是 实现部分功能的代码 (半成品),使用框架简化企业级软件开发
提高开发效率,降低代码的耦合度等。

3、 什么是Struts2 ?
Struts2 是一款优秀MVC框架

MVC:是一种思想,是一种模式,将软件分为 Model模型、View视图、Controller控制器
* MVC由来是web开发

JavaEE软件三层结构 : web层(表现层)、业务逻辑层、数据持久层 (sun提供JavaEE开发规范)
JavaEE开发更强调三层结构, web层开发注重MVC

struts2 就是 web层开发框架,符合MVC模式
* struts1 、webwork 、jsf 、SpringMVC 都是MVC

4、 Struts2 和 Struts1 关系
没有关系, Struts2 全新框架,引入WebWork很多技术和思想,Struts2 保留Struts1 类似开发流程
* Struts2 内核 webwork

Xwork提供了很多核心功能:前端拦截机(interceptor),运行时表单属性验证,类型转换,强大的表达式语言(OGNL – the Object Graph Navigation Language),IoC(Inversion of Control反转控制)容器等
Struts2快速入门
1、 下载开发包:以 struts2 3.15.1 为例

2、 Struts2目录结构
apps : struts2官方demo
docs : 文档
lib : jar包
src : 源码

3、 导入jar包到开发工程
只需要导入 apps/struts2-blank.war 中所有jar包 —- 13 个jar包
不需要记住是几个jar包,每个版本的struct2的jar包也许都不一样,只需要去 apps/struts2-blank.war 找,就是需要的最少的jar包

4、 编写页面
hello.jsp 请求页面
访问struts2入门

success.jsp 结果页面

5、在web.xml 配置struts2 前端控制器 (Filter)

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

6、执行struts2过滤器后,读取struts2配置文件,将请求分发
在src下创建struts.xml 







“`

7、执行目标Action中execute方法

8、在Action的execute方法中返回 字符串,在struts.xml中配置字符串与页面对应关系
/demo1/success.jsp 完成结果页面跳转
Struts2流程分析与工具配置
1、 运行流程
请求 —- StrutsPrepareAndExecuteFilter 核心控制器 —– Interceptors 拦截器(实现代码功能 ) —– Action 的execuute — 结果页面 Result
* 拦截器 在 struts-default.xml定义
* 执行拦截器 是 defaultStack 中引用拦截器

—- 通过源代码级别断点调试,证明拦截器是执行

2、 配置struts.xml 提示问题
如果安装Aptana编辑器 ,请不要用Aptana自带xml编辑器 编写struts2配置文件
struts.xml提示来自于 DTD约束,
“-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”
“http://struts.apache.org/dtds/struts-2.3.dtd”>
如果可以上网,自动缓存dtd,提供提示功能
如果不能上网,也可以配置本地DTD提示

* 导入DTD时,应该和配置DTD版本一致

3、 关联struts2源码
关联 zip包

4、 Config Brower 插件使用
提供在浏览器中查看 struts2 配置加载情况

将解压struts2/lib/struts2-config-browser-plugin-2.3.7.jar 复制WEB-INF/lib下

访问 http://localhost:8080/struts2_day1/config-browser/index.action 查看 struts2配置加载情况
Struts2配置
学习路径
1)、 struts.xml常量配置(配置文件顺序)、Action访问(Servlet API)、结果集 (使用Struts2 编写简单案例)
2)、 请求数据
3)、 响应页面生成
Struts2配置文件加载顺序
struts2 配置文件 由核心控制器加载 StrutsPrepareAndExecuteFilter (预处理,执行过滤)
init_DefaultProperties(); // [1] ———- org/apache/struts2/default.properties
init_TraditionalXmlConfigurations(); // [2] — struts-default.xml,struts-plugin.xml,struts.xml
init_LegacyStrutsProperties(); // [3] — 自定义struts.properties
init_CustomConfigurationProviders(); // [5] —– 自定义配置提供
init_FilterInitParameters() ; // [6] —– web.xml
init_AliasStandardObjects() ; // [7] —- Bean加载

结论 :
default.properties 该文件保存在 struts2-core-2.3.7.jar 中 org.apache.struts2包里面 (常量的默认值)
struts-default.xml 该文件保存在 struts2-core-2.3.7.jar (Bean、拦截器、结果类型 )
struts-plugin.xml 该文件保存在struts-Xxx-2.3.7.jar (在插件包中存在 ,配置插件信息 )

struts.xml 该文件是web应用默认的struts配置文件 (实际开发中,通常写struts.xml ) ********************
struts.properties 该文件是Struts的默认配置文件 (配置常量 )
web.xml 该文件是Web应用的配置文件 (配置常量 )

  • 后加载文件中struts2 常量会覆盖之前加载文件 常量内容
    Struts2的Action配置
    1)必须要为元素 配置元素 (struts2 围绕package进行Action的相关配置 )
    配置package 三个常用属性

    name 包名称,在struts2的配置文件文件中 包名不能重复 ,name并不是真正包名,只是为了管理Action
    namespace 和 的name属性,决定 Action的访问路径 (以/开始 )
    extends 继承哪个包,通常开发中继承 struts-default 包 (struts-default包在 struts-default.xml定义 )
    * 继承struts-default包后,可以使用 包中定义拦截器和结果类型
    2)Action的通过元素配置

    的name 和 的namespace属性 共同决定 Action的访问路径 !!!!!!!!

    例如 :


    访问路径 /user/hello.action
    3) 元素配置默认值
    的namespace 默认值 “”
    的class 默认值 ActionSupport 类
    的 name 默认值 success

默认Action 和 Action的默认处理类
1) 默认Action , 解决客户端访问Action不存在的问题 ,客户端访问Action, Action找不到,默认Action 就会执行

2) 默认处理类 ,客户端访问Action,已经找到匹配元素,但是元素没有class属性,执行默认处理类

* 在struts-default.xml 配置默认处理类 ActionSupport
Struts2的常量配置
1) struts2 默认常量 在 default.properties 中配置
2) 开发者自定义常量
struts.xml (要求)
格式 :
struts.properties (要求)
格式 : struts.devMode = true
web.xml
格式 :

struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts.devMode
true

3) 常用常量
—– 相当于request.setCharacterEncoding(“UTF-8”); 解决post请求乱码
— 访问struts2框架Action访问路径 扩展名 (要求)
* struts.action.extension=action,, 默认以.action结尾扩展名 和 不写扩展名 都会分发给 Action
false不缓存,true浏览器会缓存静态内容,产品环境设置true、开发环境设置false
提供详细报错页面,修改struts.xml后不需要重启服务器 (要求)
struts2 配置文件分离
通过 将struts2 配置文件 拆分
Action
HTTP请求 提交 Struts2 StrutsPrepareAndExecuteFilter 核心控制器 —— 请求分发给不同Action
Action书写的的三种格式
第一种 Action可以是 POJO ((PlainOldJavaObjects)简单的Java对象) —- 不需要继承任何父类,实现任何接口
* struts2框架 读取struts.xml 获得 完整Action类名
* obj = Class.forName(“完整类名”).newInstance();
* Method m = Class.forName(“完整类名”).getMethod(“execute”); m.invoke(obj); 通过反射 执行 execute方法

第二种 编写Action 实现Action接口
Action接口中,定义默认五种 逻辑视图名称
public static final String SUCCESS = “success”; // 数据处理成功 (成功页面)
public static final String NONE = “none”; // 页面不跳转 return null; 效果一样
public static final String ERROR = “error”; // 数据处理发送错误 (错误页面)
public static final String INPUT = “input”; // 用户输入数据有误,通常用于表单数据校验 (输入页面)
public static final String LOGIN = “login”; // 主要权限认证 (登陆页面)

  • 五种逻辑视图,解决Action处理数据后,跳转页面

第三种 编写Action 继承ActionSupport (推荐)
在Action中使用 表单校验、错误信息设置、读取国际化信息 三个功能
Action的配置method(通配符)
1) 在配置 元素时,没有指定method属性, 默认执行 Action类中 execute方法

2) 在 元素内部 添加 method属性,指定执行Action中哪个方法
执行 RegistAction 的regist方法
* 将多个请求 业务方法 写入到一个Action 类中

3) 使用通配符* ,简化struts.xml配置
添加客户
删除客户

struts.xml
<action name="customer_*" class="cn.itcast.struts2.demo4.CustomerAction" method="{1}"></action>   ---  {1}就是第一个* 匹配内容

动态方法调用
访问Action中指定方法,不进行配置
1) 在工程中使用 动态方法调用 ,必须保证 struts.enable.DynamicMethodInvocation = true 常量值 为true
2) 在action的访问路径 中 使用 “!方法名”
页面
添加商品
配置

执行 ProductAction 中的 add方法
“`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章