如果有两个链接都使用同一个action方法,那么注解可以使用如下方式(在方法上面注解):
@Actions( {
@Action(value = "/image", results = { @Result(name = "success", type = "dispatcher", location = "/WEB-INF/jsp/photo.jsp") }),
@Action(value = "/image_gdc", results = { @Result(name = "success", type = "dispatcher", location = "/WEB-INF/jsp/photo_new.jsp") }) })
如果使用注解方式进行文件下载,那么注解使用如下方式(在方法上面注解):
public class GdcDownloadAction extends ActionSupport {
private static final long serialVersionUID = -8423083786146592523L;
private String inputPath;
public void setInputPath(String value) {
this.inputPath = value;
}
public InputStream getInputStream() throws Exception {
String path = ServletActionContext.getServletContext().getRealPath("/"); //获取web application的根路径
setInputPath(path+ "gdc.exe");
return new FileInputStream(inputPath);
// ServletActionContext.getServletContext().getResourceAsStream(inputPath); //该方法不知为什么取出io总是为null,或许是将struts2以过滤器方式加载,没有以servlet方式加载。。。只好用FileInputStream了
}
/**//*
* 使用struts2注解方式进行下载,
* 其中contentType,inputName,contentDisposition,bufferSize分别代表下载文件类型、指定被下载文件的入口输入流、指定下载文件的文件名、指定下载文件时的缓冲大小
* 其中params={key1,value1,key2,value2..},下面注解里面的inputName的value值代表getInputStream()方法(潜规则为去掉get前缀、首字母小写的字符串即为inputStream)
* 如果想在注解里面添加字符串,或者想调用方法并获得返回值,那么需要使用\"${方法名字这里依然遵守潜规则}\"这种形式,比如在"attachment;filename=gdc.exe",可以将gdc.exe替换为\"${方法名}\"通过调用该方法获得下载文件名字
* application/octet-stream表示可以下载任何文件
*/
@Action(value = "/GdcDownload", results = { @Result(name = "success", type = "stream", params = {
"contentType", "application/octet-stream", "inputName",
"inputStream", "contentDisposition", "attachment;filename=gdc.exe",
"bufferSize", "4096" }, location = "/WEB-INF/patient.jsp") })
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
posted on 2010-09-09 18:49
朔望魔刃 阅读(2281)
评论(0) 编辑 收藏