Multiply submit in one form. More than 1 submit in

2019-08-11 17:48发布

问题:

I'm doing web-application. And I have a problem when I edit data

I want to create smth like this

I hope you can help me to find way to do this. I found next variant but I don't know as people usually do this.

  1. Multiply submit in one form.
  2. More than 1 submit in one form.
  3. Or more then one form in 1 JSP

And I should't use any framework. And without javascript.

Thanks

OK, If It helps to understand what I want to get on servlet I show some part of selvlet

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String page = new String();
        Command command = CommandFactory.getCommand(request);

        page = command.execute(request);
        if (page != null) {
            RequestDispatcher dispatcher =
                    getServletContext().getRequestDispatcher(page);
            dispatcher.forward(request, response);
        }
          ....
} 

And CommandFactory

public static Command getCommand(HttpServletRequest request){
        Command current = new NoCommand();

        String action = request.getParameter("command");

        if(action == null || action.isEmpty()){
            return current;
        }

        try{
            CommandEnum currentState = CommandEnum.valueOf(action.toUpperCase());
            current = currentState.getCurrentCommand();           
        }
...
}

And CommandEnum

public enum CommandEnum {

    EDIT_QUESTION {

        {
            this.command = new EditQuestionCommand();
        }
    };
} 

And in particular command I do some business logic

Please, help me to find way getting from jsp value as command="delete". With single submit I use hidden field. How we can do the same for several button?

回答1:

You can use any number of submit buttons in a single form. But name them uniquely so that you can identify easily which button is clicked. Server will not receive name, value pair of buttons that are not clicked. It becomes easy for you to find which button clicked is available as request parameter. Using that you can implement your requirement.

Sample HTML:

<form>
 <input type="submit" name="s1" value="S1"/><br/>
 <input type="submit" name="s2" value="S2"/>
</form>

On clicking any of these buttons you will see query string as either ?s1=S1 or ?s2=S2

UPDATE 1:
You can have same name for all submit buttons and to identify uniquely, they MUST have different values.

<input type="submit" name="modeOfAction" value="Delete"/><br/>
<input type="submit" name="modeOfAction" value="Update"/>

UPDATE 2:
If you really don't care what the value of each button, then it would be better using unique names for each of the submit buttons.

<input type="submit" name="delete" value="Удалить (Err! I don't know what this text is!)"/><br/>
<input type="submit" name="update" value="Update"/>


回答2:

There was a time when indeed the optional JavaScript meant: gracefully accept when no JavaScript is present. With the small devices one might think this position might be strengthened again, but:

  • HTML forms are limited in their usage, as shown above a bit.
  • DOM usage, like AJAX calls updating part of the page, need JS.
  • JavaScript user interface possibilities (effects, table sorting, paging, themes, validation) with libraries like jQuery.

I still now a way to get a command=edit: write a servlet filter that translates request parameters. For instance translate a parameter command-NAME = TEXT into command=NAME.