1、package元素
1.1、作用:
在struts2的配置文件中引入了面向对象思想,使用了分包管理。易于管理动作类。便于模块化开发动作类。
1.2、属性:
name:包的名称。必须写。且必须唯一。
extends:一般情况下需要继承struts-default包,但不是必须的。不过如果不继承的话,将无法使用struts2提供的核心功能。
struts-default.xml中定义着struts-default这个包。而struts-default.xml是在我们的struts.xml加载之前加载。
abstract:把包声明为抽象包,抽象包就是用来被继承的。只要是没有元素的包,就可以声明为抽象包。
namespace:名称空间。
1.3、packege中的namespace详解
namespace的默认值:
a.不写该属性
b.写了该属性,取值是一个”“.
注意:默认值不是/
动作类的搜索顺序:
2、action元素
2.1、作用:
配置动作用的。
2.2、属性:
name:动作名称
class:动作类全名。默认的动作类是:com.opensymphony.xwork2.ActionSupport
要想替换默认动作类:
method:动作类中的方法名称。默认是public String execute(){}
要求:
1.public的
2.返回值必须是String
3.没有参数
2.3、动作类
a.方式一:动作类就是一个POJO(Plain Old Java Object 原始的java对象),非常简单的javabean。
b.方式二:动作类实现com.opensymphony.xwork2.Action接口。
常量:给动作方法返回值用的。用常量可以使你的应用规范和统一。
c.方式三:动作类继承com.opensymphony.xwork2.ActionSupport 推荐使用
2.4、动作的访问
a.使用通配符:
<package name="struts-user" extends="global-package" namespace="/user">
<action name="*" class="struts.web.action.UserAction" method="{1}">
<param name=""></param>
<result name="input">/regist.jsp</result>
</action>
</package>
升级版:
<package name="struts-user" extends="struts-default" namespace="/user">
<action name="*_*" class="struts.web.action.UserAction" method="{1}{2}">
<result>/{1}{2}.jsp</result>
</action>
</package>
优先级:绝对匹配优先。使用通配符的按照在配置文件中的先后顺序进行匹配的。
b.动态方法调用
开启动态方法调用(不建议,有很大安全隐患)
请求调用的书写格式
3、结果类型视图(逻辑结果视图)
前奏:该部分内容指的就是struts配置文件中的result元素的使用
1、result元素
作用:为动作指定结果视图
属性:name:逻辑视图的名称,对应着动作方法的返回值。默认值是success。
type:结果类型,指的就是用什么方式转到定义的页面。默认是dispatcher(转发)。
2、result元素中type的取值
type属性的取值在struts-default.xml中定义着。
常用结果类型介绍:
dispatcher:(默认值)
使用请求转发,转向一个页面。
redirect:
使用重定向,转向一个页面。
redirectAction(结果类型):注意:使用的是重定向。
a.重定向到另一个相同名称空间的动作。
b.重定向到不同名称空间的动作
chain(链):注意: 使用的是请求转发。
a.转发到另一个相同名称空间的动作。
b.请求转发到不同名称空间的动作
3、result元素中param子元素
在转发或者重定向到不同包下的动作时,都用到了result元素的子元素param。
param元素的作用:依赖注入(Dependence Injection)思想
我们通过struts-default.xml中的resultTypes元素中配置可以看出,每个结果类型视图其实都是靠一个类来实现的。而param元素就是将配置的参数,注入到该类中。
调用的是对应类的setter方法进行注入的。
例如:redirectAction结果视图
该类中肯定会有对actionName和namespace属性的注入方法(setter方法)。
再比如:默认结果视图dispatcher
4、延伸出自定义结果类型
通过前面的内容,我们看出,其实结果类型就是一个类,这些类都实现了com.opensymphony.xwork2.Result接口。
或者继承自该接口的实现类org.apache.struts2.dispatcher.StrutsResultSupport。
这些类都有一个doExecute方法,用于执行结果视图。
综上:我们也可以自己写一个结果视图。
例子:
输出CAPTCHA图像的结果类型。
CAPTCHA(Completely Automated Public Turing Test to Tell Computers and Humans Apart 全自动区分计算机和人类的图灵测试)————>简称:验证码。
第一步:写一个类,实现接口或者继承接口的实现类
package struts.web.resultType;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import com.opensymphony.xwork2.ActionInvocation;
import cn.dsna.util.images.ValidateCode;
/*
* 第一步,继承StrutsResultSupport类,从写doExecute方法
* 第二步,在struts.xml中,声明结果类型
* 第三步,在设置action时,type属性指定声明结果类型名称
*/
public class CaptchaResult extends StrutsResultSupport{
/**
* ValidateCode是ValidateCode.jar包中的一个类,这个类是生成验证码的工具
* height; 高度(单位像素)
* width; 宽度(单位像素)
* codeCount; 验证码个数
* lineCount; 干扰线条数
*/
private static final long serialVersionUID = 1L;
private Integer height;
private Integer width;
private Integer codeCount;
private Integer lineCount;
@Override
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
//接收xml中传来的参数
ValidateCode code=new ValidateCode(width,height,codeCount,lineCount);
///使用ServletActionContext对象获得response对象
HttpServletResponse response=ServletActionContext.getResponse();
//该方法需要一个OutputStream,就是response对象的getOutputStream
code.write(response.getOutputStream());
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public Integer getCodeCount() {
return codeCount;
}
public void setCodeCount(Integer codeCount) {
this.codeCount = codeCount;
}
public Integer getLineCount() {
return lineCount;
}
public void setLineCount(Integer lineCount) {
this.lineCount = lineCount;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
第二步:在struts.xml文件中配置结果类型
<result-types>
<result-type name="captchaResult" class="struts.web.resultType.CaptchaResult"></result-type>
</result-types>
第三步:在action配置时引用
<action name="captchaResult">
<result name="captchaResult" type="captchaResult"></result>
</action >
最终结果:
扩展:通过可配置的参数,实现图像大小的调整
5、全局视图和局部视图
5.1、局部视图
<action name="captchaResult" >
<param name="width">400</param>
<param name="height">200</param>
<param name="codeCount">6</param>
<param name="lineCount">6</param>
</action >
5.2、全局视图
<result name="captchaResult" type="captchaResult">
<param name="width">400</param>
<param name="height">200</param>
<param name="codeCount">6</param>
<param name="lineCount">6</param>
</result>
运行结果:
over~~~