jsp中:
function upload(){
if(checkEmpty($("#ip")[0],"IP地址")&&checkEmpty($("#content")[0],"目录")&&checkEmpty($("#uploadFilePath")[0],"上传文件本地路径")){
$.post("gatherMainTainUpload.action",
{"gatherMainTain.ip":document.getElementById("ip").value,
"gatherMainTain.content":document.getElementById("content").value,
"gatherMainTain.uploadFilePath":document.getElementById("uploadFilePath").value
},
function(data){
alert($("#flag").val());
if(data+""=="true"){
if($("#flag").val()=="true"){
alert("上传文件成功!");
}else{
alert("目录不存在,无法上传文件,请重新输入!");
}
}else{
alert("上传文件失败!");
}
}
);
}
}
action中:
public String gatherMainTainUpload() throws Exception{
try{
this.gatherMainTains = this.gatherMainTainBeanRemote.selectByCondition(gatherMainTain);
GatherMainTainAction sf = new GatherMainTainAction();
String host = gatherMainTain.getIp();
int port = Integer.parseInt(gatherMainTains.get(0).getPort());
String username = gatherMainTains.get(0).getUserName();
String password = gatherMainTains.get(0).getPassWord();
String directory = gatherMainTain.getContent();
String uploadFile = gatherMainTain.getUploadFilePath();
ChannelSftp sftp=sf.connect(host,port,username,password);
//判断当前目录是否存在
this.gatherMainTain.setFlag("false");
try {
SftpATTRS sftpATTRS = sftp.lstat(directory);
if(sftpATTRS.isDir()){
this.gatherMainTain.setFlag("true");
}
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
this.gatherMainTain.setFlag("false");
}
}
// if(gatherMainTain.getFlag()=="true"){
// sftp.cd(directory);
// File file=new File(uploadFile);
//
// //uploadFile是本地文件所在绝对路径名
// sftp.put(new FileInputStream(file), file.getName());
// }
//
}
catch(Exception e){
e.printStackTrace();
throw(new GatherException(e));
}
return SUCCESS;
}
配置文件:
<action name="gatherMainTainUpload" class="system_gatherMainTainAction"
method="gatherMainTainUpload">
<result name="success">/ajaxTrue.jsp</result>
action可以正常执行,文件可以上传,我想判断目录是否存在,设置了flag,后台测试没问题,但是怎么传回页面啊,get set方法也都有,求指点菜鸟,谢谢
你需要在action中的session或者request 然后把想要传的值存到setAttribute("");中 他可以传值也可以传对象。然后再页面getAttribute();得到就可以了
页面上var flag1='<%=request.getSession().getAttribute("flag1").toString()%>'; alert(flag1);
配置文件/system/gatherMainTain/gatherMainTainQuery.jsp/system/gatherMainTain/gatherMainTainList.jsp
最终测试发现,传过来的flag1值是list那个里边的,upload里设置成功的flag1没传到页面,是神么原因呢