gpt4 book ai didi

struts - Struts 1.3 中的多个提交按钮

转载 作者:行者123 更新时间:2023-12-02 19:29:36 27 4
gpt4 key购买 nike

我的 JSP 中有以下代码:

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
..
..
<html:form action="update" >
..
..
<html:submit value="delete" />
<html:submit value="edit" />
<html:sumit value="update" />
</html:form>

这在 struts-config.xml 中文件:

<action path="/delete" name="currentTimeForm" input="/viewall.jsp" type="com.action.DeleteProduct">
<forward name="success" path="/viewall.jsp" />
<forward name="failure" path="/viewall.jsp" />
</action>

喜欢delete行动吧,我有editupdate 。如果我特别给出像 <html:form action="delete"> 这样的名称,它就可以正常工作。但是,如何让它动态地工作于 updateedit

最佳答案

您有一个表单和多个提交按钮。问题在于,无论表单中有多少个提交按钮,表单都只能提交一个操作。

现在想到三个解决方案:

1. 只需执行一项操作即可提交所有内容。进入 Action 类后,检查使用哪个按钮提交表单并根据该按钮执行适当的处​​理。

<html:form action="modify">
..
..
<html:submit value="delete"/>
<html:submit value="edit" />
<html:sumit value="update" >
</html:form>

ModifyAction.execute(...) 方法中,有类似以下内容:

if (request.getParameter("delete") != null || request.getParameter("delete.x") != null) {
//... delete stuff
} else if (request.getParameter("edit") != null || request.getParameter("edit.x") != null) {
//...edit stuff
} else if (request.getParameter("update") != null || request.getParameter("update.x") != null) {
//... update stuff
}

2. 在提交表单之前,使用 JavaScript 更改 HTML 表单的操作属性。您首先将提交按钮更改为附加了点击处理程序的普通按钮:

<html:form action="whatever">
..
..
<html:button value="delete" onclick="submitTheForm('delete.do')" />
<html:button value="edit" onclick="submitTheForm('edit.do')" />
<html:button value="update" onclick="submitTheForm('update.do')" />
</html:form>

使用处理程序:

function submitTheForm(theNewAction) {
var theForm = ... // get your form here, normally: document.forms[0]
theForm.action = theNewAction;
theForm.submit();
}

3.使用DispatchAction (一个 Action 类类似于第 1 点),但无需测试单击的是哪个按钮,因为该按钮由 DispatchAction 处理。

您只需提供三个执行方法,正确命名为deleteeditupdateHere is an example that explains how you might do it .

结论:对于第一点,我真的不喜欢那些丑陋的测试......对于第二点,我真的不喜欢你必须玩这个 Action 使用 JavaScript 构建表单,所以我个人会选择第 3 个

关于struts - Struts 1.3 中的多个提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7386643/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com