如何在Struts2应用程序中使用Ajax提交表单
问题描述:
我正在运行Struts2应用程序.
I'm running a Struts2 application.
我想在提交表单之前运行Ajax函数.
I want to run an Ajax function before submitting the form.
为此,我想在单击提交"按钮时调用Ajax函数
接着
从Ajax调用form.submit()
.
For this, I want to call the Ajax function on clicking submit button
and then
call the form.submit()
from Ajax.
我可以在这里做吗?因为表格必须提交给行动
Can I do this here? Because the form has to submit to an action
<s:form action="saveFile" method="post" enctype="multipart/form-data" id="form1">
<label for="myFile">Upload your file</label>
<s:file name="myFile" id="myFile"/>
<s:submit id="submit123" value="Upload" />
</s:form>
答
通过struts2-jquery插件非常容易在Struts中使用Ajax.
Using Ajax in Struts very easy with struts2-jquery plugin.
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<html>
<head>
<sj:head/>
</head>
<body>
<s:form action="saveFile" method="post" enctype="multipart/form-data" id="form1">
<label for="myFile">Upload your file</label>
<s:file name="myFile" id="myFile"/>
<sj:submit formId="form1" value="Upload" />
</s:form>
</body>
</html>
form.submit()
未使用Ajax,如果要从JavaScript手动调用Ajax,则需要使用XHR.
The form.submit()
is not using Ajax, if you want manually call Ajax from JavaScript you need to use XHR.