?文g上传面和显CZ传成功页面代码内宏V?
?UploadActioncM实现上传功能Ҏ和上传文件属性介l?
?struts.xml中UploadAction配置Q以及字W编码、文件时存放\径配|?
?上传后所处\径和最l上传成功后效果展示?/div>
演示代码
上传文g面Q这里笔者定义的是多个文件上传?
<!---------------------文g名:upload.jsp----------------->
<% @taglib prefix= "s" uri= "/struts-tags" %>
<html>
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=gb2312" >
<title>上传文g</title>
</head>
<body>
<!-- 上传文g表单定义 -->
<s:form action= "upload" method= "post" enctype= "multipart/form-data" >
<tr>
<!-- 上传文g标签定义 -->
<td>上传文g:<s:file name= "file" ></s:file></td>
</tr>
<tr>
<td>再次上传文g:<s:file name= "file" ></s:file></td>
</tr>
<tr>
<td align= "left" ><s:submit name= "submit" value= "提交" ></s:submit></td>
</tr>
</s:form>
</body>
</html>
<!---------------------文g名:upload.jsp----------------->
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>上传文g</title>
</head>
<body>
<!-- 上传文g表单定义 -->
<s:form action="upload" method="post" enctype="multipart/form-data">
<tr>
<!-- 上传文g标签定义 -->
<td>上传文g:<s:file name="file"></s:file></td>
</tr>
<tr>
<td>再次上传文g:<s:file name="file"></s:file></td>
</tr>
<tr>
<td align="left"><s:submit name="submit" value="提交"></s:submit></td>
</tr>
</s:form>
</body>
</html>
上传文g成功后结果页?
<!-------------------文g名:result.jsp ----------------->
<% @taglib prefix= "s" uri= "/struts-tags" %>
<html>
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=gb2312" >
<title>上传l果</title>
</head>
<body>
上传文gQ?
<!-- 昄上传成功文g?nbsp;-->
<s:property value= "fileFileName" />
</body>
</html>
<!-------------------文g名:result.jsp ----------------->
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>上传l果</title>
</head>
<body>
上传文gQ?
<!-- 昄上传成功文g?-->
<s:property value="fileFileName" />
</body>
</html>
UploadActioncM?
<!------------------文g名:UploadAction.java ------------------>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private final static String UPLOADDIR = "/upload" ;
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this .file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this .fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this .fileContentType = fileContentType;
}
public String execute() throws Exception {
for ( int i = 0 ; i < file.size(); i++) {
uploadFile(i);
}
return "success" ;
}
private void uploadFile( int i) throws FileNotFoundException, IOException {
try {
InputStream in = new FileInputStream(file.get(i));
String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);
File uploadFile = new File(dir, this .getFileFileName().get(i));
OutputStream out = new FileOutputStream(uploadFile);
byte [] buffer = new byte [ 1024 * 1024 ];
int length;
while ((length = in.read(buffer)) > 0 ) {
out.write(buffer, 0 , length);
}
in.close();
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
<!------------------文g名:UploadAction.java ------------------>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
//文g上传Action
public class UploadAction extends ActionSupport {
//上传文g存放路径
private final static String UPLOADDIR = "/upload";
//上传文g集合
private List<File> file;
//上传文g名集?
private List<String> fileFileName;
//上传文g内容cd集合
private List<String> fileContentType;
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
public String execute() throws Exception {
for (int i = 0; i < file.size(); i++) {
//循环上传每个文g
uploadFile(i);
}
return "success";
}
//执行上传功能
private void uploadFile(int i) throws FileNotFoundException, IOException {
try {
InputStream in = new FileInputStream(file.get(i));
String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);
File uploadFile = new File(dir, this.getFileFileName().get(i));
OutputStream out = new FileOutputStream(uploadFile);
byte[] buffer = new byte[1024 * 1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
struts.xml配置文g中有x件上传的配置Q?
<!--------------------文g名:struts.xml------------------->
<struts>
<!-- pȝ帔R定义Q定义上传文件字W集~码 -->
<constant name= "struts.i18n.encoding" value= "gb2312" ></constant>
<!-- pȝ帔R定义Q定义上传文件时存放\?nbsp;-->
<constant name= "struts.multipart.saveDir" value= "c:\" ></constant>
<!-- Action所在包定义 -->
< package name= "C04.4" extends = "struts-default" >
<!-- Action名字Q类以及D面定义 -->
<!-- 通过Actioncd理才D的的Action定义 -->
<action name= "upload" class = "action.UploadAction" >
<result name= "input" >/jsp/upload.jsp</result>
<result name= "success" >/jsp/result.jsp</result>
</action>
</ package >
</struts>
<!--------------------文g名:struts.xml------------------->
<struts>
<!-- pȝ帔R定义Q定义上传文件字W集~码 -->
<constant name="struts.i18n.encoding" value="gb2312"></constant>
<!-- pȝ帔R定义Q定义上传文件时存放\?-->
<constant name="struts.multipart.saveDir" value="c:\"></constant>
<!-- Action所在包定义 -->
<package name="C04.4" extends="struts-default">
<!-- Action名字Q类以及D面定义 -->
<!-- 通过Actioncd理才D的的Action定义 -->
<action name="upload" class="action.UploadAction">
<result name="input">/jsp/upload.jsp</result>
<result name="success">/jsp/result.jsp</result>
</action>
</package>
</struts>
Q?Q:文g上传面如图4.8所C?
?.8 文g上传
Q?Q:选择文g如图4.9所C?
?.9 选择上传的文?
Q?Q:单击“提交”按钮后文件上传成功页面,q显CZ传文件名Q如?.10所C?
?.10 上传文g成功后效?
Q?Q:两个被上传文件最l在服务器上存放路径效果如图4.11所C?
?.11 上传文g存放路径?
代码解释
Q?Q在upload.jsp中通过Form标签和File标签定义了两个上传文件。Struts2标签W者会在之后章节里具体介绍Q这里只是让读者知道是如何使用标签昄?.8昄的内宏V如果上传成功,W者在result.jsp?#8220;[”?#8220;]”之间昄上传文g的文件名Q如果是多个文gQ以“Q?#8221;盔R。这些显C格式都是用Property标签定义的?
注意Q如果上传文Ӟ在JSP的Form中一定要定义如upload.jsp文g中黑体表C的部分。method和enctype属性都必须要如代码中所C。这样Form中上传文件才会v作用?
Q?QUploadAction文g中先定义了常量UPLOADDIRQ它是上传文件上传后存放的文件夹名字。比如笔者用的是JBossQ附录中有安装和在MyEclipse中部|的操作说明Q,则在它的已部|Web目下的upload文g夹中Q会有所有上传成功的文g。如?.11读者也可以看见它的上传文g最l存放\径?
注意Q在MyEclipse中开发的“WebRoot”目录下也要新Z个upload文g夹,否则部v后在JBoss的已部vWeb目下将没有upload文g夏V因为部|的时候会所?#8220;WebRoot”目录下的文g夹和文g都部|到JBoss的已部vWeb目下?
定义好UPLOADDIR后,在定义上传文件的属性变量。也许其中的“fileFileName”?#8220;fileContentType”读者看了有点别扭,其?#8220;fileFileName”感觉不符合Java命名规范Q但是这两个属性变量是4.1节中介l的“fileUpload”拦截器类中的cd有变量名字,只有q样定义QUploadAction执行时候会把在面上选择的上传文件的属性值放在这两个变量里面Q否则调试UploadAction时候会发现q两个变量都会是“null”即空倹{不怿的读者可以自行改变这两个变量名再执行上传文g功能q行调试看一下这两个变量得到的倹{?
注意Q因里笔者是q行多个文g上传功能开发,因此“file”?#8220;fileFileName”?#8220;fileFileName”属性变量都讑֮为ListcdQ其实还可以讑֮为数l类型。个得没有啥大区别。完全凭个h喜好而定。还有如果读者自己开发单个文件上传,没必要把它们设定ؓListcd或数l类型。直接把“file”定义为Java的IO包中的FilecdQ?#8220;fileFileName”?#8220;fileFileName”定义为普通的Stringcd卛_W串cd?
之后在executeҎ中,写一个@环,Ҏ有页面中选择的上传文件一个个q行上传。这里笔者运用了重构中的“抽取Ҏ”的方式,上传文件的功能装成一个私有方法,名字?#8220;uploadFile”。其中运用了Java的IO包中很多APIҎ。有寚w构和Java的IO功能不了解的读者可以去查阅相关资料ȝ解掌握,q里不是本书以及本节重点Q因此不再具体记q?
Q?Qstruts.xml中定义了<constant>标签Q主要定义了文g名和文g内容昄的字W编码集以及q些被上传文件时存放\径?
先说明一?lt;constant>标签Q顾名思义q是定义整个Web目的一些常量属性|如果不定义则在Struts2自带的default.propertiesQ读者们可到自己安装Struts2的文件\径src\core\src\main\resources\org\apache\struts2\下找刎ͼ文g中有q些帔R的定义,比如在本节struts.xml文g中的“struts.i18n.encoding”?#8220;struts.multipart.saveDir”在default.properties定义代码如下Q?
<!--------------------文g名: default .properties---------------->
### This can be used to set your default locale and encoding scheme
# struts.locale=en_US
struts.i18n.encoding=UTF- 8
### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data
# struts.multipart.parser=cos
# struts.multipart.parser=pell
struts.multipart.parser=jakarta
# uses javax.servlet.context.tempdir by default
struts.multipart.saveDir=
<!--------------------文g名:default.properties---------------->
### This can be used to set your default locale and encoding scheme
# struts.locale=en_US
struts.i18n.encoding=UTF-8
### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data
# struts.multipart.parser=cos
# struts.multipart.parser=pell
struts.multipart.parser=jakarta
# uses javax.servlet.context.tempdir by default
struts.multipart.saveDir=
如果不在struts.xml文g中定义,则Web目会缺省用default.properties文g中这两个帔R属性的定义。一个将使字W编码集变ؓ“UTF-8”Q另一个干脆没有Q何文件\径指定。而笔者开发的该Web目~省支持的字W编码集?#8220;gb2312”Q而且需要指定时上传文件存放\径。(当然如果读者开发的Web目~省~码集就?#8220;UTF-8”Q而且也ƈ不需要指定时\径时候,没必要在struts.xml中定义这两个<constant>Q,因此有必要定义这两个属性符合项目开发要求?
注意Q也可以如第3章那P把这两个属性定义在自定义的struts.properties文g中,具体代码可以如下Q?
<!------------------------文g名:struts.properties------------------>
struts.i18n.encoding =gb2312
struts.multipart.saveDir= c:\
<!------------------------文g名:struts.properties------------------>
struts.i18n.encoding =gb2312
struts.multipart.saveDir= c:\
W者个为比在struts.xml中定义更加好Q毕竟Struts2自己也是定义在properties属性文件中Q而不是定义在自己的xml配置文g中。(Struts2自带的xml配置文g为struts-default.xmlQ在4.1节中已记述Q。这里是Z让读者知道struts.xml配置文g也可以配|这些属性,因此写在struts.xml配置文g中。从3.2节W者说明struts.xml配置文g时ƈ没有介绍<constant>标签q点也可以知道笔者个人其实是不赞同这L配置手段卛_struts.xml中配|?lt;constant>标签?
?lt;Action>标签中配|?#8220;result”Q和W?章类|这两个JSP文g的导航流E配|好卛_?
Q?Q开始进行文件上传功能展C,按照如上记述的步骤执行即可。笔者在桌面上新Z两个文本文gQ将它们上传到JBoss已部|的Web目中展C文件上传的upload文g夹下。如?.11所C?
其实q可以指定上传文件的格式Q让它只上传特定cd的文件。比如只能上传文本和xml文gQ则在struts.xml需要显C配|?#8220;uploadFile”拦截器。如下代码:
<!-----------------------文g名:struts.xml------------------>
<struts>
<!-- Action所在包定义 -->
< package name= "C04.4" extends = "struts-default" >
<!-- Action名字Q类以及D面定义 -->
<!-- 通过Actioncd理才D的的Action定义 -->
<action name= "upload" class = "action.UploadAction" >
<result name= "input" >/jsp/upload.jsp</result>
<result name= "success" >/jsp/result.jsp</result>
</action>
<!—显C配|文件上传拦截器 -->
<interceptor-ref name=”fileUpload”>
<!—指定特定类型的上传文g -->
<param name =”allowedTypes”>text/plain,application/xml</param>
</ interceptor-ref >
<interceptor-ref name=”defaultStack”></ interceptor-ref >
</ package >
</struts>
<!-----------------------文g名:struts.xml------------------>
<struts>
<!-- Action所在包定义 -->
<package name="C04.4" extends="struts-default">
<!-- Action名字Q类以及D面定义 -->
<!-- 通过Actioncd理才D的的Action定义 -->
<action name="upload" class="action.UploadAction">
<result name="input">/jsp/upload.jsp</result>
<result name="success">/jsp/result.jsp</result>
</action>
<!—显C配|文件上传拦截器 -->
<interceptor-ref name=”fileUpload”>
<!—指定特定类型的上传文g -->
<param name =”allowedTypes”>text/plain,application/xml</param>
</ interceptor-ref >
<interceptor-ref name=”defaultStack”></ interceptor-ref >
</package>
</struts>
定义了一个名?#8220;allowedTypes”的参敎ͼ其中?lt;param></param>之间的是文gcdQ也可以?#8220;Q?#8221;间隔Q表C允怸传多个文件类型。这里允怸传文件类型ؓtxt、xml格式的文件。如果读者不知道各个文gcd的定义,可在自己的JBoss安装目录中的server\default\deploy\jboss-web.deployer\conf\下的web.xml文g中找刎ͼ搜烦<mime-mapping>卛_Q?
注意Q如果显C配|Struts2自己的缺省拦截器一定要写在“defaultStack”前,否则“fileUpload”拦截器不会执行拦截。因为Struts2中如果某个拦截器执行拦截时候发现自己已l执行过Q第二个乃至之后同名的拦截器都不会执行。这里因?#8220;defaultStack”拦截器栈中包含了“fileUpload”拦截器,?#8220;fileUpload”拦截器已l执行拦截了Q则不会再执行拦截。如果把“defaultStack”拦截器栈攑֜“fileUpload”拦截器前配置Q则只执?#8220;defaultStack”拦截器栈中的“fileUpload”拦截器,q里是没有定?#8220;allowedTypes”的,Struts2~省默认的是支持所有文件类型。因此它会支持所有文件类型的文g上传。因此再讑֮“allowedTypes”没有Q何意义了?
]]>
11 Struts2文g下蝲功能开发(转自黑暗子Q?/title> http://m.tkk7.com/lanxin1020/archive/2009/04/13/265295.htmllanxin1020 lanxin1020 Mon, 13 Apr 2009 07:30:00 GMT http://m.tkk7.com/lanxin1020/archive/2009/04/13/265295.html http://m.tkk7.com/lanxin1020/comments/265295.html http://m.tkk7.com/lanxin1020/archive/2009/04/13/265295.html#Feedback 0 http://m.tkk7.com/lanxin1020/comments/commentRss/265295.html http://m.tkk7.com/lanxin1020/services/trackbacks/265295.html Struts2文g下蝲功能开?/span>
技术要?
本节代码详细说明文g下蝲功能的开发流E,介绍知识点如下:
?上传成功面重修改后支持文g下蝲代码内容?
?DownloadAction文g下蝲功能开发?
?struts.xml中DownloadAction配置Q以及支持文件名Z文字W的文g下蝲?
?下蝲文g程展示?/div>
演示代码
上传成功面Q这里笔者让其在每个上传文g后提?#8220;下蝲”链接?
<!------------------------文g名:result.jsp------------------->
<% @taglib prefix= "s" uri= "/struts-tags" %>
<body>
上传文gQ?
<table>
<!-- 循环昄上传成功文g?nbsp;-->
<s:iterator value= "fileFileName" status= "fn" >
<tr>
<td>
<!-- 上传成功文g?nbsp;-->
<s:property />
</td>
<td>
<!-- 下蝲文g链接内容为定义的下蝲Action -->
<!-- 下蝲文g名作为链接参数fileName|用OGNL表达式表?nbsp;-->
<a href="<s:url value= 'download.action' >
<s:param name= 'fileName'
value= 'fileFileName[#fn.getIndex()]' />
</s:url>">下蝲</a>
</td>
</tr>
</s:iterator>
</table>
</body>
<!------------------------文g名:result.jsp------------------->
<%@taglib prefix="s" uri="/struts-tags"%>
<body>
上传文gQ?
<table>
<!-- 循环昄上传成功文g?-->
<s:iterator value="fileFileName" status="fn">
<tr>
<td>
<!-- 上传成功文g?-->
<s:property />
</td>
<td>
<!-- 下蝲文g链接内容为定义的下蝲Action -->
<!-- 下蝲文g名作为链接参数fileName|用OGNL表达式表?-->
<a href="<s:url value='download.action'>
<s:param name='fileName'
value='fileFileName[#fn.getIndex()]'/>
</s:url>">下蝲</a>
</td>
</tr>
</s:iterator>
</table>
</body>
DownLoadActioncM?
<!------------文g名:DownLoadAction.java ------------------>
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownLoadAction extends ActionSupport {
private final static String DOWNLOADFILEPATH= "/upload/" ;
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this .fileName = fileName;
}
public InputStream getDownloadFile() {
return
ServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+fileName);
}
public String getDownloadChineseFileName() {
String downloadChineseFileName = fileName;
try {
downloadChineseFileName = new String(downloadChineseFileName.getBytes(), "ISO8859-1" );
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadChineseFileName;
}
public String execute() {
return SUCCESS;
}
}
<!------------文g名:DownLoadAction.java ------------------>
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownLoadAction extends ActionSupport {
//下蝲文g原始存放路径
private final static String DOWNLOADFILEPATH="/upload/";
//文g名参数变?
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
//从下载文件原始存放\径读取得到文件输出流
public InputStream getDownloadFile() {
return
ServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+fileName);
}
//如果下蝲文g名ؓ中文Q进行字W编码{?
public String getDownloadChineseFileName() {
String downloadChineseFileName = fileName;
try {
downloadChineseFileName = new String(downloadChineseFileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadChineseFileName;
}
public String execute() {
return SUCCESS;
}
}
struts.xml配置文g中有x件下载的配置Q?
<!------------------文g名:struts.xml----------------->
<struts>
<!-- 下蝲文g的Action定义 -->
<action name= "download" class = "action.DownLoadAction" >
<!-- 讄文g名参敎ͼ由页面上传入 -->
<param name= "fileName" ></param>
<result name= "success" type= "stream" >
<!-- 下蝲文gcd定义 -->
<param name= "contentType" >text/plain</param>
<!-- 下蝲文g处理Ҏ -->
<param name= "contentDisposition" >
attachment;filename= "${downloadChineseFileName}"
</param>
<!-- 下蝲文g输出定?nbsp;-->
<param name= "inputName" >downloadFile</param>
</result>
</action>
</struts>
<!------------------文g名:struts.xml----------------->
<struts>
<!-- 下蝲文g的Action定义 -->
<action name="download" class="action.DownLoadAction">
<!-- 讄文g名参敎ͼ由页面上传入 -->
<param name="fileName"></param>
<result name="success" type="stream">
<!-- 下蝲文gcd定义 -->
<param name="contentType">text/plain</param>
<!-- 下蝲文g处理Ҏ -->
<param name="contentDisposition">
attachment;filename="${downloadChineseFileName}"
</param>
<!-- 下蝲文g输出定?-->
<param name="inputName">downloadFile</param>
</result>
</action>
</struts>
Q?Q:文g开始下载页面如?.12所C?
?.12 文g下蝲
Q?Q:单击“下蝲”链接Q比如点“下蝲文g1.txt”文g双“下蝲”链接Q出现对话框如图4.13所C?
?.13 下蝲文g处理方式
Q?Q:单击“保存”按钮后选择下蝲文g存放路径Q如?.14所C?
?.14 下蝲文g选择存放路径
代码解释
Q?Q在result.jsp中通过iterator标签和url标签定义?#8220;fileFileName”的@环显CZ及链接。其中有?#8220;status”和OGNL表达式笔者会在之后章节里具体介绍Q这里只是让读者知道是如何使用标签昄?.12昄的内宏V特别指?lt;param>标签为downloadAction定义了一个参敎ͼ该参数名?#8220;fileName”Q因为在4.4.1节中笔者定义的“fileFileName”是个Listcd的数据集合,因此利用OGNL表达式将文g名作?#8220;fileName”参数g入downloadAction中?
Q?QDownLoadAction文g中先定义了常量DOWNLOADFILEPATHQ它是下载文件在服务器存攄路径名,也就?.4.1节中上传文件在服务器存攄路径名?
定义好DOWNLOADFILEPATH后,在定义DownLoadAction的属性变量。因为在result.jsp中定义了参数“fileName”Q则它作为DownLoadAction的属性变量,需要定义相应的getter、setterҎ?
然后定义了getDownloadFileҎQ它q回的是一个文件流Q表明将被下载文件{换ؓ输出,方便下蝲。利用Struts2自带?#8220;ServletActionContext”cȝAPI把下载文件存放\径作为方法参敎ͼd下蝲文gQ将其{换ؓ文g?
q有一个getDownloadChineseFileNameҎQ该Ҏ主要作用是将文g名ؓ中文字符的文件进行文件名的字W编码集合{换。因为在Webpȝ中由JSP{视N面传入的变量|特别是中文字W的变量。缺省的字符~码集合都是“ISO8859-1”Q因此利用Java的字W串cȝAPIQ将字符~码转成开发需要的字符~码集。防止中文字Wؕ码问题发生?
Q?Qstruts.xml中定义了名ؓ“download”的Action。其中它自己的参?#8220;fileName”因ؓ在这里它的g从JSP面上传入,所以这里只是定义,没有具体l它赋Q何?
?lt;result>标签中定义了type属性,gؓ“stream”。如果是下蝲文g功能开发,DownLoadAction一定要讄type属性,而且gؓ“stream”。这是因为在Struts2自带的xml配置文g为struts-default.xml中有关于“stream”的resultq回cd的定义,代码如下Q?
<!-------------------文g名:struts- default .xml-------------->
<result-type name= "stream" class = "org.apache.struts2.dispatcher.StreamResult" />
<!-------------------文g名:struts-default.xml-------------->
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
q里Struts2定义了resultq回cd?#8220;stream”Q这个resultcd主要是处理文件的输入和输出流时候需要用的。因Z载文件就是把文g转换成输入输出流Q将其从一个文件\径放到另外一个文件\径中厅R所以肯定要讄q个resultcd的?
“contentType”?#8220;contentDisposition”?#8220;inputName”都是q个result的属性?#8220;contentType”是文gcd。这里因Z载的文g是文本文Ӟ因此讑֮的gؓ文本文gcdQ具体各个文件类型如何定义,4.4.1节已经介绍q,q里不再做说明?#8220;contentDisposition”是指定下载文件处理方式,如图4.13是处理方式的效果。特别指出如?#8220;contentDisposition”定义的值把前面?#8220;attachment”LQ则下蝲方式不是以附件方式下载,如果单击“下蝲”链接Q则会把下蝲文g的内ҎC在览器中。读者可以去试验一下。这里有?#8220;${downloadChineseFileName}”Q这是在DownLoadAction中定义getDownloadChineseFileNameҎ的目的,${downloadChineseFileName}是OGNL的表辑ּQ它昄?#8220;downloadChineseFileName”变量的具体|因ؓ在DownLoadAction中定义getDownloadChineseFileNameҎQ则把已l{换成W合需要字W编码集的下载文件名作ؓ下蝲文g方式对话框中昄的名Uͼ不会造成Mq问题?#8220;inputName”是最关键的一个属性,也是一定要定义的属性,“inputName”参数中定义的?#8220;downloadFile”是DownLoadAction中getDownloadFileҎq回的文件流名字。在Struts2中Acion用前~名ؓget的方法得到各U属性的|q些属性有些是在Action中定义,有些像本示例在配置文g中利用OGNL表达式或直接定义?
Q?Q开始进行文件下载功能展C,按照如上记述的步骤执行即可。笔者将两个文本文g上传上去Q然后在上传成功面对具体的文gq行下蝲。在?.13中单?#8220;保存”按钮显C图4.14Q选择在本Z存放下蝲文g的\径即可完成下载文件功能?
]]>
12Struts2标签使用原理Q{自黑暗浪子) http://m.tkk7.com/lanxin1020/archive/2009/04/13/265294.htmllanxin1020 lanxin1020 Mon, 13 Apr 2009 07:28:00 GMT http://m.tkk7.com/lanxin1020/archive/2009/04/13/265294.html http://m.tkk7.com/lanxin1020/comments/265294.html http://m.tkk7.com/lanxin1020/archive/2009/04/13/265294.html#Feedback 0 http://m.tkk7.com/lanxin1020/comments/commentRss/265294.html http://m.tkk7.com/lanxin1020/services/trackbacks/265294.html Struts2标签使用原理解疑
在笔者下载的Struts2的包中,读者可以在/lib下找到struts2-core-2.0.11.1.jar包,解压该包在其根目录下?META-INF文g夹下可以看到一个名字ؓ“struts-tags.tld”文g。该文g是Struts2中所有自带的标签库定义。本节通过对该文g代码的介l来让读者知晓Struts2内部是如何用这些标{来q行工作。ƈ单说明JSP中是如何用其来书写标{代码?
技术要?
本节代码说明Struts2内部定义标签的格式和在JSP中用方式?
?struts-tags.tld文g定标{֮义配|格式?
?JSP中用标{֊能介l?/div>
演示代码
<!------------------文g名: struts-tags.tld----------------->
<taglib>
<tlib-version> 2.2 . 3 </tlib-version>
<jsp-version> 1.2 </jsp-version>
< short -name>s</ short -name>
<uri>/struts-tags</uri>
<display-name> "Struts Tags" </display-name>
<description>………………</description>
<tag>
<name>action</name>
<tag- class >org.apache.struts2.views.jsp.ActionTag</tag- class >
<body-content>JSP</body-content>
<description><![CDATA[Execute an action from within a view]]></description>
<attribute>
<name>executeResult</name>
<required> false </required>
<rtexprvalue> false </rtexprvalue>
<description><![CDATA[Whether the result of this action (probably a view) should be executed/rendered]]></description>
</attribute>
…………………………
<attribute>
<name>namespace</name>
<required> false </required>
<rtexprvalue> false </rtexprvalue>
<description><![CDATA[Namespace for action to call]]></description>
</attribute>
</tag>
</taglib>
<!------------------文g名: struts-tags.tld----------------->
<taglib>
<tlib-version>2.2.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>s</short-name>
<uri>/struts-tags</uri>
<display-name>"Struts Tags"</display-name>
<description>………………</description>
<tag>
<name>action</name>
<tag-class>org.apache.struts2.views.jsp.ActionTag</tag-class>
<body-content>JSP</body-content>
<description><![CDATA[Execute an action from within a view]]></description>
<attribute>
<name>executeResult</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Whether the result of this action (probably a view) should be executed/rendered]]></description>
</attribute>
…………………………
<attribute>
<name>namespace</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Namespace for action to call]]></description>
</attribute>
</tag>
</taglib>
代码解释
Q?Qstruts-tags.tld是Struts2自标{֮义文件。所有标{֮义都是在<tablib>?lt;/taglib>之间定义。以<tag></tag>用来定义一个具体标{。每个标{֛为都可以有很多自q属性。这些属性定义都是以<attribute></attribute>来定义?
Q?Q?lt;tlib-version></tlib-version>之间定义的是标签库的版本?lt;jsp-version></jsp-version>定义的是标签库这些标{是支持JSP的哪个版本?lt;short-name> </short-name>其实是标{ֺ的默认名Q也可以认ؓ是其늧?lt;uri> </uri>定义的是标签库的URIQ在JSP中会使用到?lt;display-name></display-name>是显C名?lt;description></description>是标{ֺ的记qͼ记述标签库的使用用途等{?
Q?Q?lt;attribute>?lt;name></name>是属性名U定义?lt;required></required>表示的该属性是否是必须的属性,如果是必ȝ?lt;required></required>之间为trueQ否则ؓfalse?lt;rtexprvalue></rtexprvalue>表示的是可否使用表达式,大多数标{N是ؓfalse。这里不是不能用表辑ּQ而是恰恰相反表示可以使用表达式?lt;description></description>定义和前面介l相同?
Q?Q在JSP中,如之前章节的演示代码所C,都是在文件头有个使用标签的声明,代码如下?
<!---------------------文g名: *.jsp------------------------->
<% @taglib prefix= "s" uri= "/struts-tags" %>
<!---------------------文g名: *.jsp------------------------->
<%@taglib prefix="s" uri="/struts-tags"%>
有了q个声明Q在JSP文g中就可以使用Struts2的标{。比如form标签定义要像如下代码所C?
<s:form action= "upload" ………>
<s:form action="upload" ………>
C一定要?#8220;s”Q它是Struts2中标{默认名也是相当于一个昵Uͼ当然读者也可以把它改ؓ自己惛_的名字,不过在标{֣明中?#8220;prefix”中就要改成那个自己取的名字?
注意Q因为笔者用的Servlet版本?.3之上的版本,因此没必要在web.xml中定义标{ֺ。如果读者用的Servlet版本比较低,则在web.xml文g中需要定义如下的代码Q?
<!----------------------文g名:web.xml----------------------->
<taglib>
<!- 定义URI - ->
<taglib-uri>/Struts 2 -tags</taglib-uri>
<!- 定义标签库支持的jar包位|? ->
<taglib-location>/WEB-INF/lib/struts2-core- 2.0 . 11.1 .jar</taglib-location>
</taglib>
<!----------------------文g名:web.xml----------------------->
<taglib>
<!- 定义URI - ->
<taglib-uri>/Struts 2-tags</taglib-uri>
<!- 定义标签库支持的jar包位|? ->
<taglib-location>/WEB-INF/lib/struts2-core-2.0.11.1.jar</taglib-location>
</taglib>
只有q样标签库才会在Servlet版本比较低的情况下用有效果?
]]>
վ֩ģ壺
aһƬ |
鵺̳߹ۿ |
Ʒպ?V |
岻߹ۿ |
ɫƷVRһ |
ƬվŮ |
Ļ |
ѹۿһëƬ |
߳ëƬڵƵ |
100018ѷ˸ |
777Ʒþþþþ |
ĻѸ |
aרav |
þþþù |
˵þþþƷ |
91Ʒѹۿ |
˰ǿŮ²Ƶ |
ŷ͵Ʒ
|
ŷͽȺ |
40 |
þۺ |
þƵ |
33333 |
һػƸѴƬ |
Ƶ߹ۿ |
ƷĻ |
ëƬ߲ |
aëƬվ |
ղ2021 |
vѹۿ |
100018վ |
ҳվѹۿ |
˾þþƷ |
xxxxƵ |
߹ۿ |
պ ɫ ͼվ |
ƷƷۺ |
Ļ |
ԴƵ |
ҹƵվ |
鶹˾þþƷ |