###1.Strust概念 2.导包 (见手写笔记和图)
###strusts文件配置:
!必须以struts.xml文件名在src目录下配置
#创建strusts2主配置文件
<struts>
<!-- 开启开发模式 -->
<!-- package元素:封装Action配置.方便项目的模块开发.
name属性:给包起一个名字.名称随意.不能重复.
namespace属性:命名空间.指定Action资源名前的访问路径.
/hello: /struts2_day01/hello/HelloAction
/:/struts2_day01/HelloAction
不写该属性:相当于填写"/"
该属性值可以重复.
extends属性:继承另一个包.会将配置信息继承. 必须继承struts-default.
-->
<package name="hello" namespace="/" extends="struts-default" >
<!-- action元素:配置action对象
name属性:指定访问Action时填写的资源名称
class属性:填写Action完整类名.用于struts2框架创建action对象
method属性:定位类中处理请求的方法,填写方法名
-->
<action name="HelloAction" class="cn.itcast.a_hello.HelloAction"
method="hello" >
<!-- result元素:定义结果
name属性:对应Action中方法的返回值
type属性:指定交个哪个结果处理器来处理.
|- 转发(默认值) dispatcher
|- 重定向 redirect
标签体:指定跳转的资源路径.相对项目WebContent目录
-->
<result name="success" type="dispatcher" >/hello.jsp</result>
</action>
</package>
##配置核心入口过滤器: 把请求交给strusts来处理
在web.xml中添加:
<filter>
<filter-name>struts2</filter-name>
//ctrl+shift+ 找StrutsPrepareAndExcuteFilter ,打开找到原文件完整类名复制到filter-class元素里
<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>
##常量配置 core.apache.strust2 -> static -> default.properties 中有常量信息
-在struts.xml中:
<constant name="常量名" value="常量值"></constant>
!action类创建时推荐继承ActionSupport类
##action中方法的规则
- 1.public 2.string 3.方法名任意 4.方法不能有参数 5.方法可以抛出异常
##解耦方式访问ServletAPI:
public String execute() throws Exception {
//获得数据中心--ActionContext对象
ActionContext ac = ActionContext.getContext();
//application域Map
Map<String, Object> applicationScope = ac.getApplication();
//session域Map
Map<String, Object> sessionScope = ac.getSession();
//request域Map
//Map<String, Object> requestScope =(Map<String, Object>) ac.get("request");
//struts2框架推荐使用ActionContext本身作为request域
//-----------------------------------------------------------------
//分别向3个域中存入键值对
applicationScope.put("name", "applicationTom");
sessionScope.put("name", "sessionTom");
ac.put("name", "requestTom");
return SUCCESS;
}
###strusts文件配置:
!必须以struts.xml文件名在src目录下配置
#创建strusts2主配置文件
<struts>
<!-- 开启开发模式 -->
<!-- package元素:封装Action配置.方便项目的模块开发.
name属性:给包起一个名字.名称随意.不能重复.
namespace属性:命名空间.指定Action资源名前的访问路径.
/hello: /struts2_day01/hello/HelloAction
/:/struts2_day01/HelloAction
不写该属性:相当于填写"/"
该属性值可以重复.
extends属性:继承另一个包.会将配置信息继承. 必须继承struts-default.
-->
<package name="hello" namespace="/" extends="struts-default" >
<!-- action元素:配置action对象
name属性:指定访问Action时填写的资源名称
class属性:填写Action完整类名.用于struts2框架创建action对象
method属性:定位类中处理请求的方法,填写方法名
-->
<action name="HelloAction" class="cn.itcast.a_hello.HelloAction"
method="hello" >
<!-- result元素:定义结果
name属性:对应Action中方法的返回值
type属性:指定交个哪个结果处理器来处理.
|- 转发(默认值) dispatcher
|- 重定向 redirect
标签体:指定跳转的资源路径.相对项目WebContent目录
-->
<result name="success" type="dispatcher" >/hello.jsp</result>
</action>
</package>
##配置核心入口过滤器: 把请求交给strusts来处理
在web.xml中添加:
<filter>
<filter-name>struts2</filter-name>
//ctrl+shift+ 找StrutsPrepareAndExcuteFilter ,打开找到原文件完整类名复制到filter-class元素里
<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>
##常量配置 core.apache.strust2 -> static -> default.properties 中有常量信息
-在struts.xml中:
<constant name="常量名" value="常量值"></constant>
!action类创建时推荐继承ActionSupport类
##action中方法的规则
- 1.public 2.string 3.方法名任意 4.方法不能有参数 5.方法可以抛出异常
##解耦方式访问ServletAPI:
public String execute() throws Exception {
//获得数据中心--ActionContext对象
ActionContext ac = ActionContext.getContext();
//application域Map
Map<String, Object> applicationScope = ac.getApplication();
//session域Map
Map<String, Object> sessionScope = ac.getSession();
//request域Map
//Map<String, Object> requestScope =(Map<String, Object>) ac.get("request");
//struts2框架推荐使用ActionContext本身作为request域
//-----------------------------------------------------------------
//分别向3个域中存入键值对
applicationScope.put("name", "applicationTom");
sessionScope.put("name", "sessionTom");
ac.put("name", "requestTom");
return SUCCESS;
}