Struts笔记5
文件下载
1.写action类
packagecom.gyf.web.action;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.InputStream;
importcom.opensymphony.xwork2.ActionSupport;
publicclassDownloadAction3extendsActionSupport{
privateInputStreamis;
publicInputStreamgetInputStream(){
returnis;
}
publicStringdownload()throwsFileNotFoundException{
Stringpath="C:/Users/Yuan/Desktop/9.png";
//给输入流赋值
is=newFileInputStream(path);
returnSUCCESS;
}
}
2.配置struts.xml
查看源码发现要以流的方式返回给客户端需要配置3个参数img
<actionname="download"class="com.gyf.web.action.DownloadAction"method="download">
<!--以流的形式返回给客户端-->
<resultname="success"type="stream">
<paramname="inputName">is</param><!--输入流名称-->
<paramname="contentType">application/octet-stream</param><!--设置响应头-->
<paramname="contentDisposition">attachment;filename=9.png</param><!--设置响应头-->
</result>
</action>
img
上面那张图流写is根本调用不了getInputStream的方法要把is变量名更改为inputStream才可以。
下载完成
img
struts的OGNL
OGNL是ObjectGraphicNavigationLanguage的缩写
他是一个单独的开源项目,Struts2框架使用OGNL作为默认的表达式语言
OGNL是struts2整合的一个开源项目,所以在Struts2中,要想使用OGNL表达式,必须使用struts2标签库
OGNL相当于EL表达式,从作用于取数据
OGNL的功能
访问对象方法,静态属性,静态方法,封装List数据,封装Map数据
<!--OGNL表达式
首先:要在jsp页面导入<%@tagliburi="/struts-tags"prefix="s"%>>
-->
<!--表示从作用于取值,加上单引号表示字符串-->
<s:propertyvalue="java-struts2"/>
<!--表示获取字符串的长度-->
<s:propertyvalue="'java-struts2'.length()"/>
<!--访问静态属性,需要在strtus上配置允许静态方法访问-->
int的最大值<s:propertyvalue="@java.lang.Integer@MAX_VALUE"/>
随机数<s:propertyvalue="@java.lang.Math@random()"/>
<!--封装List数据,数组-->
<%--<s:radiolist="{'男','女'}"name="gender"label="性别"></s:radio>--%>
<!--封装Map数据,用#{'key''value'}-->
<s:radiolist="#{'male':'男','female':'女'}"name="gender"label="性别"></s:radio>
如果您觉得本文的内容对您的学习有所帮助:
关键字:
html