1.访问或添加request/session/application属性
publicString scope() throws Exception{
ActionContext ctx =ActionContext.getContext();
ctx.getApplication().put("app","应用范围");
//往ServletContext里放入app
ctx.getSession().put("ses","session范围");
//往session里放入ses
ctx.put("req", "request范围");
//往request里放入req
return "scope";
}
JSP:
<body>
${applicationScope.app} <br>
${sessionScope.ses}<br>
${requestScope.req}<br>
</body>
2. 文件上传
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:把form表的enctype设置为:“multipart/form-data“,如下:
<formenctype="multipart/form-data" action=””method="">
<input type="file" name="uploadImage">
</form>
第三步:在Action类中添加以下属性,属性蓝色部分对应于表单中文件字段的名称:
publicclass HelloWorldAction{
l private File upload;//得到上传的文件
private String uploadContentType;//得到文件的类型
private String uploadFileName;//得到文件的名称
//这里略省了属性的getter/setter方法
public String upload() throws Exception{
String realpath =ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(realpath);
if(!file.exists()) file.mkdirs();
FileUtils.copyFile(uploadImage, newFile(file, uploadImageFileName));
return "success";
}
}
3. 多文件上传
第一步:同单文件一样。
第二步:把form表的enctype设置为:“multipart/form-data“,其他也同单文件的一样,只是是多个input上传框。
<input type="file" name="uploadImages">
<input type="file" name="uploadImages">
第三步:在Action类中添加以下属性,也与单文件基本相同,只是将单文件中的属性都变成了数组类型。
package www.csdn.struts_upload.action;
import java.io.File;
import java.io.IOException;
importorg.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MultiPartUploadAction extends ActionSupport{
// 规范:
private File[]upload; // 上传的文件
private String[]uploadContentType;// 文件类型
private String[]uploadFileName;// 文件的名称
private String path;
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[]uploadContentType) {
this.uploadContentType =uploadContentType;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[]uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getPath() {
returnServletActionContext.getServletContext().getRealPath(path);
}
public void setPath(String path) {
this.path = path;
}
public String upload() {
File file = new File(getPath());
// 判断指定目录下的文件是否存在
if (!file.exists()) {
file.mkdir();
}
try {
// 判断文件集合或数组是否为空
if (upload != null) {
// 遍历上传的文件
for (int i = 0; i < upload.length;i++) {
File uploadFile = upload[i];
if (uploadFile == null) {
continue;
}
// commons-io FileUtils工具 copyFile(要拷贝的文件,拷贝给谁的那个文件);
FileUtils.copyFile(uploadFile, newFile(file, System
.currentTimeMillis()
+ "_" +uploadFileName[i]));
}
return "upload";
}
} catch (IOException e) {
e.printStackTrace();
}
return "fail";
}
}
<package name="upload" namespace="/csdn"extends="struts-default">
<action name="*_*"class="www.csdn.struts_upload.action.{1}"
method="{2}">
<param name="path">WEB-INF/uploads</param>
<result name="upload">/sc.jsp</result>
<result name="fail">/fail.jsp</result>
</action>