在struts中添加按钮而不是提交表单

in struts add button instead of submit form

本文关键字:提交 表单 按钮 struts 添加      更新时间:2023-09-26

如何在javascript调用中点击按钮提交表单以调用函数,表单的所有信息如下。我正在使用struts核心标签库和添加按钮标签。以下是代码

<body>
    <html:form action="createtableAction.do?param=uploadExcelFile1" method="post" enctype="multipart/form-data">
        <table>
            <tr>
                <td align="left" colspan="1" class="migrate">
                    <html:file property="filename" styleId="filename" style="background-color:lightgoldenrodyellow; color:black;" />
                </td>
            </tr>
            <tr style="width:100%">
                <td align="center" colspan="2" class="migrate">
                    <html:submit onclick="return Checkfiles()" style="background-color:silver; color:darkblue;">Upload File</html:submit>
                </td>
            </tr>
        </table>
    </html:form>
</body>

在函数调用操作中并提交

function Checkfiles() {
    if (fileList[count] === tempFileName) {
        //want action and form submit code ...
        //document.forms[0].action='/createtableAction.do?param=uploadExcelFile1';
        //document.forms[0].submit();
        return true;
    } else {
        return false;
    }    
}

您可以使用ajax请求发送文件。javascript中有可用的FormData()。您可以将文件附加到FormData对象并将其发送到servlet。

function checkFiles(){
     var formData=new FormData();
     var uploadUrl = 'createtableAction.do?param=uploadExcelFile1';
     // Uncomment below this if you wish to send any other fields with file
     // formData.append('id', 1);
     console.log('loading file');
     var file= document.getElementById("filename"); 
     formData.append('filename',file);
     //create the ajax request (traditional way)
     var request = new XMLHttpRequest();
     request.open('POST', uploadUrl);
     request.send(formData);
     // if you want to reload uncomment the line below
     // window.location.reload();
}

希望这对你有帮助。