【发布时间】:2015-07-16 09:59:29
【问题描述】:
这是我需要验证的上传页面。目前验证没有发生。我认为在 Struts 2 表单验证是不同的。我可以点击这个链接http://www.mkyong.com/struts/struts-validator-framework-example/ 并以类似的方式实现我的表单吗?
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/displaytag-12.tld" prefix="display"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%-- <%@ taglib prefix="s" uri="/struts-tags"%> --%>
<%@ page errorPage="error.jsp"%>
</style>
<script>
var _validFileExtensions = [".xlsx", ".xls", ".xlt"];
function Validate(oForm) {
alert("alert message in upload form");
if( document.getElementById("filename").files.length == 0 ){
alert("inside if");
document.getElementById("fileSelect").style.display="";
return false;
}
else{
alert("inside else");
document.getElementById("fileSelect").style.display="none";
}
var arrInputs = oForm.getElementsByTagName("input");
for (var i = 0; i < arrInputs.length; i++) {
var oInput = arrInputs[i];
if (oInput.type == "file") {
var sFileName = oInput.value;
if (sFileName.length > 0) {
var blnValid = false;
for (var j = 0; j < _validFileExtensions.length; j++) {
var sCurExtension = _validFileExtensions[j];
if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
blnValid = true;
break;
}
}
if (!blnValid) {
alert("not valid");
document.getElementById("fileFormat").style.display="";
return false;
}
}
}
}
return true;
}
</script>
<center>
<html:form action="/process.do?method=upload" method="POST" enctype="multipart/form-data" onsubmit="return Validate(this);">
<table cellpadding='4' border='0' width="80%">
<tr>
<td width="1%"></td>
<td colspan='4' align='center'>
<b>Upload VIP Orders Shipment Dates:</b>
<div style="margin: 20px 20px 20px 250px">
<strong><label>File :</label></strong> <html:file id="filename" property="uploadedFile"/><br/>
<input type="submit" value="Upload File" style="margin: 40px 0 0 160px"/><br /><br />
<div id="fileSelect" style="color:Red;display:none">No file selected for uploading.</div>
<div id="fileFormat" style="color:Red;display:none">Invalid File Format. Only .xlsx, .xls, and .xlt file format allowed.</div>
</div>
</td>
</tr>
</table>
</html:form>
我只想获取文件名。如果文件名为空,则显示错误消息。
我希望验证发生在客户端本身,而不是请求到服务器端进行验证。这是我要显示的两条消息: 没有选择要上传的文件 ----- 当没有选择文件时,您单击提交按钮。和 文件格式无效。仅允许 .xlsx、.xls 和 .xlt 文件格式--- 当文件不是excel格式时
【问题讨论】:
-
那么您使用的是哪些 Struts?
-
您发布的 S1 代码与 S2 有什么关系?
-
我是 struts 新手。在我的项目中,使用了 struts 1 和 2。所以你可以假设它的支柱 1 ;)
-
控制台有什么错误?
-
首先我想知道这是否是正确的代码:
。我在 javascript 中使用这个 id="filename" 并尝试检查是否选择了文件。但控件没有到达警报消息:alert("inside if")
标签: validation file-upload struts-1 apache-commons-fileupload