2

我需要在我的网络项目中有几个具有不同功能的按钮,但我不知道实现它的正确方法。我这样做只是为了测试:

<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 中的按钮单击事件。但这是我应该这样做的方式,我将非常感谢一个简单的例子。

4

1 回答 1

2

type submit means you are going to post the form data to the server for processing. You should use button instead of submit if you are not doing post request.

<input name="command" type="hidden" value="redirect"></input>
<input type="button" name="page" value="Make order"></input>
<input id="seeBillsButton" type="button" name="page" value="See bills"></input>
<input type="button" name="page" value="See orders"></input>

And use javascript/jquery to handle the button click events.

$("#seeBillsButton").click(function() {
  alert("Handler for .click() called.");
});

Edited At server side, you should have 3 actions to handle these requests.

  1. MakeOrderAction
  2. ViewBillAction
  3. ViewOrderAction.

For MakeOrderAction, you just want to redirect user to make order page, handle this by click event in javascript(or a hyperlink).

For ViewBillAction and ViewOrderAction, each action ties to a jsp and display the result there. You have 2 ways to archieve this. You can either make 2 forms for them and submit to corresponding action separately. Or you can use the same form, and check what user has clicked by click event, handle and submit the form in javascript. The result jsp page should defined in your action class.

http://api.jquery.com/click/

于 2013-01-31T09:03:09.517 回答