Image buttons in Struts (using ImageButtonBean class)

13

Basically, the recipe for using image buttons using the Struts tag library can be found on this page at j2ee.lagnada.com and this excerpt uit Ted Husted’s book Struts in Action.

Read on to see what I have to add to this.

Let me first illustrate why you would want image buttons ;-)

ImageButtonScreenShot

Basically, I thought it was unclear from Ted Husted’s page what to put exactly in the JSP page, and to be more specific, what established ‘the link’ between the image button in the JSP page and the corresponding field in the ActionForm.

Moreover, I did not understand why Ted Husted defined his own ImageButtoBean class, while there was one already present in the Stuts framework itself (maybe he has been working with an older Struts version?).

Combining the information of the above resources and using the ImageButtoBean class from the Struts framework, my solution is as follows:

In the JSP page, create image buttons as follows:

<html:image property="authorSearchButton" src="images/searchIcon.jpg" alt="authorSearch" />

In your ActionForm, create a corresponding member variable for this button with getter and setter methods:

import org.apache.struts.util.ImageButtonBean;

private ImageButtonBean authorSearchButton = new ImageButtonBean();

public void setAuthorSearchButton(ImageButtonBean button)
{
    this.authorSearchButton = button;
}

public ImageButtonBean getAuthorSearchButton()
{
    return this.authorSearchButton;
}

Note the correspondence of the name in the JSP page and the ActionForm (explained nicely on the above mentioned the HTML buttons webpage).

Also, add a isSelected() method to your ActionForm:

public int getSelected()
{
    if (getAuthorSearchButton().isSelected())
    {
          return AUTHOR_SEARCH;
    }
}

I use an int value, but you may choose your own type, of course.

Now you can use this method in your Action class, so this could be something like

public ActionForward execute(
    ActionMapping mapping,
    ActionForm actionForm,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception
{
    int selected = ((PublicationSearchForm) actionForm).getSelected();

    switch (selected)
    {
        case AUTHOR_SEARCH:
        // etc.

Since I have multiple image buttons on my form, I implemented a switch statement. Now I can take appropriate actions to each of the button presses!

Note also that this solution is elegant in the sense that it doesn’t rely on Javascript.

Share.

About Author

13 Comments

  1. i want simple solution for creating print button wid simple print image how can i do it can any one help me

  2. Pingback: AdSense Money Maker

  3. Re: Comment #10, we’ve dealt with that here by the following:

    public ActionErrors validate(
    ActionMapping mapping,
    HttpServletRequest request)
    {

    String action = StringUtil.removeNull(request.getParameter(Constants.ACTION_PARAMETER));

    logger.debug(“Action hit is ‘” + action + “‘”);

    logger.debug(“Method is ‘” + request.getMethod() + “‘”);

    if ((Constants.POST_METHOD.equalsIgnoreCase(request.getMethod())
    && (((Constants.ADD_ACTION.equals(action))
    || ((Constants.UPDATE_ACTION.equals(action)))))))
    {
    logger.debug(“validation is on for this call”);
    return super.validate(mapping, request);
    }
    else
    {
    logger.debug(“validation is off for this call”);
    return null;
    }
    }

    and if this is for many forms, place that logic in your own abstract form class that extends ValidatorForm.

  4. I have a problem with this solution because I’m doing validation on the form. I have two buttons one to accept and another to cancel.
    When I submit the application first validate and then go to the Action if I submit Cancel the validate not let me reach the
    proper action because never go inside Action
    Regards

  5. rss feeds are available; but without a nice orange image :-(. Check the bottom of the left navigation ‘frame’. btw Both Opera and Firefox are also picking it up.

  6. Zeger Hendrikse on

    IMHO, way too many programmers use JavaScript way too easy. In situations like these, I really dislike use of JavaScript, hence I wrote this post. I’m glad more people are enthousiastic about this. Moreover, this makes me feel more confident that I’m right about the (over)use of JavaScript.

  7. This article saved my life. I am a Struts newb and trying to support a legacy app. The previous guy over-engineered the crap out the application. He used a complex piece of java script to create the buttons, and I still don’t know how they work, really. I need to add more buttons and It Just Won’t Work. But this solution is what I like – simple, clean, lean, explicit, and _maintainable_.

    Good catch by Olaf.

  8. EXCELLENT ILLUSTRATION ( with good note from Olaf ) It helped !! We needed to put some buttons in the same form ( like submit, getting another page, refresh etc.. ).

  9. You forgot to mention that the buttons have to be resetted. Otherwise it will stay selected. This is best to be done in the reset method of the form bean:

    public void reset(ActionMapping mapping, HttpServletRequest request)
    {
    authorSearchButton.setX(null);
    authorSearchButton.setY(null);
    }

  10. Leon van Tegelen on

    But imagine you have a list returned by an application, and for each entry you want a delete button, then it is more elegant to have all these related actions

    If this list is dynamic , you would need a dynamic switch :-(

  11. Zeger Hendrikse on

    > – Why would you want separate searchbuttons in a searchform?
    > What if i want to search for all books a certain author published
    > in a certain year? Looks to me just one button and one action
    > is enough, creating the where-clause of the searchstatement
    > with only the non-null fields.

    Of course, this example is a bit contrived, as my whole study/hobby application is (using EJBs, which is a total overkill). But imagine you have a list returned by an application, and for each entry you want a delete button, then it is more elegant to have all these related actions grouped together. But your suggestion to compose the fields to one “aggregate query” is true, in this case there should at least be a search button which combines the fields!

    > – If you want so much buttons in one page/form for any reason
    > (call me uncreative, but i can’t think of any good reason to do so),
    > why not just separate the form into multiple forms and
    > create corresponding actions?

    See above, or am I missing something here?

    > – Going with the separate actions thingy, you could create a
    > searchAction, and subclass it in AuthorSearchAction or something,
    > linking the author-button to that action. This makes it all
    > more reusable and removes the unnatural switch from the code.

    You are right, everytime one sees a switch statement, a good OO programmer should be alarmed. Let’s say it was late in the evening… (as a matter of fact, it was).

  12. Could you explain to me again why this is neccesary? It all seems overly complicated to me, like you’re working against the way Struts wants you to do things..
    (disclaimer: if this sounds a bit cranky, that might be because i’m irritated by the Hibernate-documentation cramping my research)

    – Why would you want separate searchbuttons in a searchform? What if i want to search for all books a certain author published in a certain year? Looks to me just one button and one action is enough, creating the where-clause of the searchstatement with only the non-null fields.

    – If you want so much buttons in one page/form for any reason (call me uncreative, but i can’t think of any good reason to do so), why not just separate the form into multiple forms and create corresponding actions?

    – Going with the separate actions thingy, you could create a searchAction, and subclass it in AuthorSearchAction or something, linking the author-button to that action. This makes it all more reusable and removes the unnatural switch from the code.

    Allright, i’ll shut up now ;)