I've gone through several deviations and techniques of handling HTML buttons in an action handler code and so far this is the best and proven code that I have came up with.
The class is called HtmlButtons and it is designed to handle buttons and image buttons in your HTML/JSP page.
Suppose we have the follwing HTML form:
<form action="/createNewUser.do"> <input type="text" name="username"/> <input type="password" name="password"/> <input type="submit" name="createButton" value="createButton"/> <input type="image" name="deleteButton" value="deleteButton"/> </form>
With HtmlButtons, the way to handle this in your action handler class would be very simple. The following is an example of a Struts Action handler:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = mapping.getInputForward(); HtmlButtons buttons = new HtmlButtons(request); if (buttons.pressed("createButton")) { // Create User } else if (buttons.pressed("deleteButton")) { // Delete User } return forward; }
On top of that, if you create an abstract base action class, you could actually make the HtmlButtons class be a part of your method arguments:
// ActionBase#executeAction (abstract) public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return executeAction(mapping, form, request, response, new HtmlButtons(request)); } // Action Class Implementation public ActionForm executeActionActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, HtmlButtons buttons) throws Exception { if (buttons.pressed("createButton")) { // Create User } else if (buttons.pressed("deleteButton")) { // Delete User } }
View HtmlButtons.java
No comments:
Post a Comment