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获取,该怎么获取?谢谢 展开
先谢谢下面的回答,不过我最关心的是前台数据如何传递到后台,action中直接写get方法可以,是不是还有其他方法呢?比如通过bean获取,该怎么获取?谢谢 展开
2个回答
展开全部
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;
}
}
给你看个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;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-07-01 · 知道合伙人互联网行家
关注
展开全部
前台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")%>
<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%>
<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%>
<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询