struts.xml中可以使用el表达式和ognl表达式
转自:http://blog.csdn.net/hzc543806053/article/details/7538723
文件上传链接:
1)Servlet 文件上传 ———— 点击打开链接
2)Struts2 文件上传 ———— 点击打开链接
文件下载是一个很常见的功能,用struts2实现文件下载的步骤:
一)定义一个Action类,FileDownload.java
- package com.struts2.filedownload;
- import java.io.InputStream;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- //文件下载
- public class FileDownload extends ActionSupport{
- private int number ;
- private String fileName;
- public int getNumber() {
- return number;
- }
- public void setNumber(int number) {
- this.number = number;
- }
- public String getFileName() {
- return fileName;
- }
- public void setFileName(String fileName) {
- this.fileName = fileName;
- }
- //返回一个输入流,作为一个客户端来说是一个输入流,但对于服务器端是一个 输出流
- public InputStream getDownloadFile() throws Exception
- {
- if(1 == number)
- {
- this.fileName = "Dream.jpg" ;
- //获取资源路径
- return ServletActionContext.getServletContext().getResourceAsStream("upload/Dream.jpg") ;
- }
- else if(2 == number)
- {
- this.fileName = "jd2chm源码生成chm格式文档.rar" ;
- //解解乱码
- this.fileName = new String(this.fileName.getBytes("GBK"),"ISO-8859-1");
- return ServletActionContext.getServletContext().getResourceAsStream("upload/jd2chm源码生成chm格式文档.rar") ;
- }
- else
- return null ;
- }
- @Override
- public String execute() throws Exception {
- return SUCCESS;
- }
- }
二)在struts.xml文件中配置相关信息
- <struts>
- <package name="struts2" extends="struts-default">
- <action name="FileDownload" class="com.struts2.filedownload.FileDownload">
- <result name="success" type="stream">
- <param name="contentType">text/plain</param>
- <param name="contentDisposition">attachment;fileName="${fileName}"</param>
- <param name="inputName">downloadFile</param>
- <param name="bufferSize">1024</param>
- </result>
- </action>
- </package>
- </struts>
1.结果类型必须要写成 type="stream" ,与之对应的处理类是 org.apache.struts2.dispatcher.StreamResult
2.涉及到的参数:
3.
1) <param name="contentDisposition">attachment;fileName="${fileName}"</param>
contentDisposition默认是 inline(内联的), 比如说下载的文件是文本类型的,就直接在网页上打开,不能直接打开的才会打开下载框自己选择
2) attachment :下载时会打开下载框
3) fileName="${fileName}" :在这定义的名字是一个动态的,该名字是显示在下载框上的文件名字
4.<param name="inputName">downloadFile</param>,这个downloadFile名字要和FileDownload.java类中的getDownloadFile()方法名去掉get 一致
三)用于显示下载的链接界面 filedownload.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'filedownload.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <h2>文件下载内容:</h2><br/>
- Dream.jpg:<a href="FileDownload.action?number=1">点击下载</a><br/>
- jd2chm源码生成chm格式文档.rar:<a href="FileDownload.action?number=2">点击下载2</a>
- </body>
- </html>
struts.xml中可以使用el表达式和ognl表达式的更多相关文章
- struts.xml中出现Package struts2 extends undefined package struts-default解决办法
在struts.xml中出现extends undefined package struts-default,经过查阅资料原来是因为没有联网的缘故.这样解决:在myeclipse中关联本地的dtd文件 ...
- 在Struts.xml中的result元素指的是:指定动作类的动作方法执行完后的结果视图.
result结果集 上一篇文章主要讲Struts2框架(4)---Action类访问servlet这篇主要讲result结果集 在Struts.xml中的result元素指的是:指定动作类的动作方法执 ...
- struts.xml中出现extends undefined package struts-default解决的方法
在struts.xml中出现extends undefined package struts-default,经过查阅资料原来是由于没有联网的缘故. 这样解决:在myeclipse中关联本地的dtd文 ...
- el 表达式 和 ognl表达式
el (expression language) el 基础操作符 el 能够隐含对象(就是可以直接访问的) el 的两种使用方式,第二种好像在jsp中没有什么用,主要用于jsf el能够访问的对象( ...
- 关于Struts2中的值栈与OGNL表达式
1.1.1 OGNL概述: Object Graphic Navigation Language(对象图导航语言)的缩写 * EL :OGNL比EL功能强大很多倍. 它是一个开源项目. ...
- struts.xml中的配置常量的含义
struts.serve.static.browserCache 该属性设置浏览器是否缓存静态内容.当应用处于开发阶段时,我们希望每次请求都获得服务器的最新响应,则可设置该属性为false. stru ...
- el表达式跟ognl表达式的区别(转)
EL表达式: >>单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application.>>如果在struts环境中,它除了有在上面的 ...
- el表达式跟ognl表达式的区别
: EL表达式 单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application. 如果在struts环境中,它除了有在上面的四个作用域的取值功能外,还 ...
- struts2 ValueStack详解,页面获取值el表达式、ognl表达式
http://www.blogjava.net/max/archive/2007/04/28/114417.html 我想用的是el表达式! http://www.cnblogs.com/belief ...
随机推荐
- JAVA命令大全
1.java.exe:======================运行java程序,这个相信每一位用Java的人知道了. 2.javac.exe:======================编译的Ja ...
- PhpStorm 设置php代码格式
phpstorm 代码格式化方法: 快捷键:Ctrl + Alt + L 设置代码样式:File -> Settings -> Code Style ->PHP 根据个人php代码规 ...
- Clover(资源管理器增强)
Clover(资源管理器增强) 下载地址:http://www.orsoon.com/Soft/13157.html 功能: Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌 ...
- NGUI 粒子显示在上级
http://bbs.taikr.com/thread-2272-1-1.html [NGUI]3.0+版本,粒子在UI后面显示 -- : 48人阅读 评论() 收藏 举报 [Unity3D][NGU ...
- node.js模拟qq漂流瓶
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) node.js模拟简易漂流瓶,页面有扔瓶子和捡瓶子的功能,一个瓶子只能被捡到一次,阅读完就置状态位, ...
- nginx学习(一):基本安装
转载自http://summervast.blog.51cto.com/690507/385511 注意:可能因版本不同,个别指令不起作用,需要注意版本灵活安装,我在安装时也遇到过此问题 开始学习ng ...
- 【SpringMVC】SpringMVC系列10之视图与视图解析器
10.视图与视图解析器 10.1.概述 请求处理方法执行完成后,最终返回一个 ModelAndView处理方法,Spring MVC 也会在内部将它们装配成一个ModelAndView 对象, ...
- 【云计算】Netflix 开源持续交付平台 Spinnaker
oschina 发布于: 2015年11月19日 (0评) 分享到: 收藏 +1 CDS首都在线全球云主机.全球私有网络,开工送礼,免费试用! » 日前,Ne ...
- Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- static_cast dynamic_cast const_cast reinterpret_cast总结对比
[本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...