Struts2学习-1

1、默认命名空间为"",不是"/"
2、配置result转发或者重定向到其他命名空间时
     //注入思想,在执行转发或重定向之前,加载参数
     参数param必须先namespace
                           再actionName
3、<!-- result配置逻辑结果视图
name:结果视图名称,与动作方法的返回值对应,当一致时前往指定jsp
type:结果视图类型,默认为dispatcher
请求转发dispatcher
重定向redirect
转发到另一个动作chain 转发到同名或不同名命名空间下
重定向到一个动作redirectAction 转发到同名或不同名命名空间下
//两者区别
转发一次请求,重定向是两次
转发是服务器行为,重定向是浏览器行为
转发地址不变,重定向地址栏变化
转发不丢失请求域中的值,重定向丢失
-->
4、自定义结果类型
    以验证码为例ValidateCode
    /**
 * 通过配置文件调整生成文件的大小
 * @author linoer
 * 自定义结果类型
 * 1、继承StrutsResultSupport,重写doExecute方法
 */
public class MyResult extends StrutsResultSupport{
private int width;
private int height;
private int numStyle;
private int lineNum;
/* 
* @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation)
*/
@Override
protected void doExecute(String arg0, ActionInvocation arg1) throws Exception {
// TODO Auto-generated method stub
//使用第三方生成验证码的jar包
/*
* 1、拷贝jar包
* 2、创建ValidateCode对象
* 3、获取响应对象输出流
* 4、输出到浏览器
*/
//参数详解:1、图像宽高度,2、数字格式,3、干扰条数
ValidateCode code = new ValidateCode(width,height,numStyle,lineNum);
//获取响应对象
HttpServletResponse response = ServletActionContext.getResponse();
//输出到浏览器
code.write(response.getOutputStream());
// ImageIO.write(image, "jpg", response.getOutputStream());

}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getNumStyle() {
return numStyle;
}
public void setNumStyle(int numStyle) {
this.numStyle = numStyle;
}
public int getLineNum() {
return lineNum;
}
public void setLineNum(int lineNum) {
this.lineNum = lineNum;
}
}
     <!-- 配置自定义结果类型 -->
<package name="p3" extends="struts-default">
<!-- 自定义 -->
<result-types>
<result-type name="captcha" class="cn.gson.web.action.MyResult"></result-type>
</result-types>
<action name="captchaAction" class="cn.gson.web.action.CaptchaAction">
<result name="success" type="captcha">
<param name="width">150</param>
<param name="height">30</param>
<param name="numStyle">5</param>
<param name="lineNum">100</param>
</result>
</action>
</package>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值