struts2,如何在action中获取前台页面元素?

有个上传页面,form已正确设置enctype,然后添加<s:filename="file"id="file"/><s:submit/>,如何在后台获取选择文件后显示在文... 有个上传页面,form已正确设置enctype,然后添加<s:file name="file" id="file"/><s:submit/>,如何在后台获取选择文件后显示在文本框的那个路径?比如页面上我选择文件E:\test.txt,如何在后台bean中获取该路径,并用在action中?请认真回答,谢绝转载
先谢谢下面的回答,不过我最关心的是前台数据如何传递到后台,action中直接写get方法可以,是不是还有其他方法呢?比如通过bean获取,该怎么获取?谢谢
展开
 我来答
jimmyrubya
2010-10-11
知道答主
回答量:16
采纳率:0%
帮助的人:11.8万
展开全部
struts2的文件上传已经进一步简化了。

给你看个Demo

package com.test.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
* struts2文件上传
*
* @author Peerless
*
*/
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {

private String username;

private String password;

private List<File> file; // 用数组或集合处理多文件,单文件去掉集合即可。

private List<String> fileFileName;

private List<String> fileContentType;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

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;
}

@Override
public String execute() throws Exception {
for (int i = 0; i < file.size(); ++i) {
InputStream is = new FileInputStream(file.get(i));

String root = ServletActionContext.getRequest().getRealPath("/upload");

File destFile = new File(root, this.getFileFileName().get(i));

OutputStream os = new FileOutputStream(destFile);

byte[] buffer = new byte[400];

int length = 0;

while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}

is.close();

os.close();
}

return SUCCESS;

}

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友96ffcf7
2015-07-01 · 知道合伙人互联网行家
百度网友96ffcf7
知道合伙人互联网行家
采纳数:22722 获赞数:118704
从事多年网络方面工作,有丰富的互联网经验。

向TA提问 私信TA
展开全部
前台jsp页面获取后台值的方式
<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%>
<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%>
<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式