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.
- Multiply submit in one form.
- More than 1 submit in one form.
- 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?
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:
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.
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.
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:
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.