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 ...
随机推荐
- sql批量获取wordpress所有留言者的邮件地址
如果你的wordpress博客有很多读者互动的话,他们的留言都会留下具体的联系邮箱,我们如何批量导出这些联系信息呢?可以试试下面的sql语句 SELECT DISTINCT comment_autho ...
- bc.34.B.Building Blocks(贪心)
Building Blocks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- cocos2d-x 在android环境下开发遇到的一些bug
今天在弄一个关于android环境下解析xml的东东,遇到了2个比较麻烦问题 1.android的apk下文件是压缩文件,io.open模式无法读取到数据的, 解决思路就是: CCFileUtils: ...
- unity3d 截屏
原地址:http://www.cnblogs.com/88999660/archive/2013/01/21/2869747.html void OnGUI(){ if(GUI.Button(new ...
- Tooltip jqueryui
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) http://jqueryui.com/tooltip/ <meta charset=&quo ...
- 一个很不错的适合PHPER们书单,推荐给大家【转】
来我博客的访客们中,有一些是PHP的初学者,是不是很迷茫PHP应该怎么学?应该买什么样的书?到处问人,到处求助?这下好了. 正好看到黑夜路人在博客上推荐了一个书单,看上去都非常不错,很多我也没有读过, ...
- 用JAVA代码实现验证邮箱地址是否符合
public class Test{ public static void main(String[] args){ Test t = new Test(); String email = " ...
- Trailing Zeros
Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...
- Linux运维
概要:http://os.51cto.com/art/201312/423616.htm 论坛: http://www.linux360.cn/ https://www.centos.bz/ http ...
- iterator与const_iterator及const iterator区别
如果你传递过来一个const类型的容器,那么只能用const_iterator来遍历. C++ Code 12345 void Method(const vector<int> v ...