Jane Morazi
Jane Morazi

Reputation: 39

Multiple buttons in JSP

I need to have several buttons with different functions in my web-project, but i don't know the right way to realize it. I did it this way just for test:

<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>

And then in the command i switch throw enum of page names to find out, which button was clicked. One button should just redirect client to another page, and the others should invoke their own commands.

I know it's a bad practice to postprocess a form submit, so i'm looking for another way to do it. I was advised to use JS onclick, but i don't have any experience in it and can't find any simple examples suitable to my project.

Edited

I have Action classes for ViewBillAction and ViewOrderAction. Also i have such class to define required 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);

The main problem is i don't know to HOW handle the button click events in JS at all. But this is the way i should do it and i would be very grateful for a simple example.

Upvotes: 2

Views: 3892

Answers (1)

Drogba
Drogba

Reputation: 4346

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/

Upvotes: 2

Related Questions