我需要在我的网络项目中有几个具有不同功能的按钮,但我不知道实现它的正确方法。我这样做只是为了测试:
<input name="command" type="hidden" value="redirect"></input>
<input type="submit" name="page" value="Make order"></input>
<input type="submit" name="page" value="See bills"></input>
<input type="submit" name="page" value="See orders"></input>
然后在命令中我切换页面名称的抛出枚举以找出单击了哪个按钮。一个按钮应该只是将客户端重定向到另一个页面,其他按钮应该调用他们自己的命令。
我知道后处理表单是一种不好的做法submit
,所以我正在寻找另一种方法来做到这一点。有人建议我使用 JS onclick
,但我没有任何经验,也找不到任何适合我项目的简单示例。
已编辑
我有 ViewBillAction 和 ViewOrderAction 的 Action 类。我也有这样的类来定义所需的操作:
public class Redirect implements Action {
@Override
public String execute(TransferredData requestInformation) {
String pageName = requestInformation.getRequestParameter("page");
Action action;
switch (pageName.toUpperCase()) {
case "MAKE ORDER":
return JSPManager.INSTANCE.getProperty(JSPManager.ORDER_PAGE_PATH);
case "VIEW BILLS":
action = new ViewBillsCommand();
return action.execute(requestInformation);
case "VIEW ORDERS":
action = new ViewRequestsCommand();
return action.execute(requestInformation);
主要问题是我根本不知道如何处理 JS 中的按钮单击事件。但这是我应该这样做的方式,我将非常感谢一个简单的例子。