1.定义
struts就是控制层的一个框架(相当于自定义MVC中的action层)
以前com.zking.framework中的东西就是struts中的一些东西
2.struts有两个非常重要的特性(特点):
①ognl
②struts拦截器
3.步骤:
①导jar包
②配中央控制器
③导资源(配置)文件
4.环境搭建
4.1 jar
4.2 web.xml
4.3 struts.xml
struts.xml(核心配置文件)
5.开发
5.1 Action
5.1.1 不需要指定父类(ActionSupport)
5.1.2 业务方法的定义
public String xxx();//execute
5.1.3 Action是多例模式(注:在spring中的配置中一定要注意)
Action用来接收参数
5.2 参数赋值
5.2.1 Action中定义属性,并提供get/set方法
userName, getUserName/setUserName
5.2.2 ModelDriven
返回实体,不能为null,不需要提供get/set方法
5.2.3 ModelDriven返回实体和Action中属性重名,ModelDriven中优先级更高
注:ognl,ActionContext学完就知道了
5.3 与J2EE容器交互
5.3.1 非注入
5.3.1.2 耦合
ServletActionContext
5.3.1.2 解耦(建立使用解耦模式)
ActionContext
5.3.2 注入
5.3.2.1 耦合
作业:找出struts2中其它的几个注入接口(XxxAware)
5.3.2.2 解耦
–src
–struts.xml(核心配置文件)
–struts.properties(全局属性文件)
6. 核心文件配置
6.1 include 包含文件
file
6.2 package 包
name 包名
extends 继承
namespace 虚拟路径
abstract 通常用来被继承
6.3 action 子控制器
name:helloAction,helloAction_*
class 全限定名
method:execute,{1}
注1:动态方法调用,新版本中已禁用,可自行开启或关闭
注2:子控制器的访问路径:名称空间+"/"+子控制器名字_xxx+".action"
6.4 result
name
type:dispatcher|redirect
注:
问题:配置文件引入dtd不生效,无提示
解决办法:
①将struts-2.5.dtd文件本地化
②window --> Preferences中搜索xml -->找到XML Catalog --> add -->把本地的拷进去(点击file System去本地找)
–> Key type中选URI
③最终去校验一下(选中你的项目右键–>找到Validate)
mapped 映射