Question

STRUTS: Multiple buttons in a form

Asked by: rusco

Hi experts!

My page has 4 buttons in one form. One is the submit button, another one is a cancel button, and the other two are buttons that does another action if chosen. (For example, delete and edit).

I use <html:img> or <html:image> tags for the buttons. I was wondering how I could get my action to determine which button was clicked and have the forward subsequently use the right method in my action.

Any help would be great. Thanks!

Thanks!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-11-20 at 22:53:13ID20804830
Tags

struts

,

button

,

multiple

Topic

Java Server Pages (JSP)

Participating Experts
6
Points
50
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. What is Struts?
    What is Struts???
  2. Struts 1.1 - Problem with html:cancel tag and using gr…
    I am creating a web app using Tomcat v3.3 (forced to use this version), JDK v 1.3, and Struts v1.1. I have a JSP that uses both a Form class/object and Action class/object . The JSP also does Struts validation via the validation.xml file, so the Form class extends Validator...
  3. Using html:img tag in struts
    Hi! I am trying to use img tag but I have this error msg. "According to TLD, tag html:img must be empty, but is not" This is what I did. In jsp page. <html:img pageKey="image.arrow" width="6" height="12"> in application...
  4. Basic struts
    Hi guys, I want to work in struts. I have tomcat5.5 server. how to work in that.is it any software i have to download? could you give me the simple hello word program in struts? explain the steps to be followed . already i posted the link for studying purpose.
  5. Struts Image tag!
    Hi experts, how to change this html image tag: <td background="images/Line_Vertical_dotted.gif" width="1"></td> into a struts image tag?? Thanks..
  6. using html:img struts tag, How can I get the Value attribute.
    Hi, I am using struts html:img tag and setting property, value attributes and How can I get the value set for value attribute. <html:img property="left" src=" " value="leftclick" />, I have to get the value set for the value attribute. c...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: DidierDPosted on 2003-11-21 at 00:08:18ID: 9795366

You can do it like this:

<a href="/nextPage.do"><html:img src="/submit.gif" border="0" /></a>

or

<script>
  function gotoCancel(){
    document.yourForm.action=nextPage.do;
    document.yourForm.submit();
  }

</script>


<a href="javascript:gotoCancel()"><html:img src="/submit.gif" border="0" /></a>

Greetz,
Didier

 

by: DidierDPosted on 2003-11-21 at 00:10:37ID: 9795376

Forgot the quotes
   
 document.yourForm.action="nextPage.do";
 

 

by: ruscoPosted on 2003-11-21 at 00:53:07ID: 9795548

Hi Didier,

Just a quick question, when you use <a href="/nextPage.do"> this means that all of the buttons will have this link right? I mean, they will all go to one action but to the different methods of the action? Or does it mean having multiple actions? (mutiple .java/.class).

Thanks :-)

 

by: ruscoPosted on 2003-11-21 at 01:14:15ID: 9795646

Hi Kotan,

Will the link you sent me also work with <html:images> used as submit buttons? Just wanted to check. Thanks!

 

by: kotanPosted on 2003-11-21 at 01:20:49ID: 9795672

I think can not. Then you can use its super class, DispatchAction

http://jakarta.apache.org/struts/api/org/apache/struts/actions/DispatchAction.html

 

by: ruscoPosted on 2003-11-21 at 02:29:49ID: 9795929

Hi Kotan,

Could you give me a code sample of how I could pass the the paramenter/method?

I tried using this:

        <html:link action="/RemoveConfigItem" paramId="method" paramName="remove">
        <html:image src="/ADM/images/delete_button.gif" border="0"/>
        </html:link>

But I get an error because it can't find the bean names remove. I was wondering how I'd be able to pass this :-)

Thanks!

 

by: DidierDPosted on 2003-11-21 at 02:46:02ID: 9795997

Hi rusco,

No, you can put the <a href="youraction"> ...........</a> around every html:img. Give them all a differnt value for href.


The same with the other example. You can make a function gotoCancel(), gotoOK(), gotoWhatever(), etc

Greetz,
Didier

 

by: TimYatesPosted on 2003-11-21 at 02:50:07ID: 9796013

This is a good way of doing it (I think):

http://j2ee.lagnada.com/struts/html-buttons.htm

 

by: kennethxuPosted on 2003-11-21 at 11:06:32ID: 9799203

I always use a hidden field named as action, and for each button/image onclick, set the action to the various value. and in the formbean define action property to determine which button/image was clicked.

 

by: struckyPosted on 2004-03-22 at 10:06:40ID: 10651120

DidierD,

I like what you are proposing, but I can't seem to get it to work.  I'm using LookupDispatchAction to determine which action method gets called when a button on the form is clicked (there are several buttons: add, save, exit).  I'm also using the Validator framework so that when I click the Add and Save buttons the form gets validated before the action class methods are executed (some fields on the form cannot be left blank for Add and Save).  But, I don't want validation to occur when I click the Exit button (because user should be able to exit from a blank form).   So, I want to change the form action to /exit.do when the Exit button is clicked (in struts-config.xml the action mapping for /exit.do has validate='false' to prevent the form from being validated before going to the action class's exit() method).  Here are the pieces:

Action mappings from struts-config.xml:
<action path="/theformaction" name="myForm" type="CommonActions" validate="true" input="/thejspfile.jsp" parameter="methodToCall" scope="request" >
      <forward name="success" path="/success.jsp" />
      <forward name="failure" path="/failure.jsp" />
</action>
<action path="/exit" name="myForm" type="CommonActions" validate="false" parameter="methodToCall" scope="request" >
      <forward name="mainmenu" path="/mainMenu.jsp" />
</action>

(myForm is of type org.apache.struts.validator.DynaValidatorForm, CommonActions is a subclass of org.apache.struts.actions.LookupDispatchAction).

Button code from thejspfile.jsp:
<html:submit property="methodToCall">
      <bean:message key="button.add"/>
</html:submit>
<html:submit property="methodToCall">
      <bean:message key="button.save"/>
</html:reset>
<input type="button" name="methodToCall" value="Exit" onclick="javascript:exit()">
<script>
      function exit(){
            document.forms[0].action="/exit.do";
            document.forms[0].submit();
      }
</script>

Struts uses the methodToCall parameter to figure out which method in my LookupDispatchAction subclass to execute.  It works fine for the add and save buttons (validation occurs, then the add() or save() methods run), but when I click the exit button, I get a runtime exception:

javax.servlet.ServletException: Request[/exit] does not contain handler parameter named methodToCall.

This is what the form buttons look like with view source:

<input type="submit" name="methodToCall" value="Add">
<input type="submit" name="methodToCall" value="Add">
<input type="button" name="methodToCall" value="Exit" onclick="javascript:exit()">

Apparently, the methodToCall parameter isn't being added to the ActionMapping object, but I don't know why.  Very sorry this is so long, but I wanted to provide a complete description.  

Note that I know this works:

<input type="button" value='Exit' onclick='window.location="mainMenu.jsp";'>

 but I don't want to do that, because then the path to the jsp file is displayed to the user in the URL.

Thanks for any help.

 

by: zenMistress2004Posted on 2004-06-06 at 23:06:56ID: 11246377

To make buttons work:

JSP:
 <html:submit property="submit">
                          <bean:message key="button.add"/>
                </html:submit>

note:  there has to be space between the " /> .

1. property can be any name you want but you will have to remember this because this is what you will need for parameter name in the struts-config file.

2. The key to the button is called button.add.  You can make any name you want.  You can call it e.g. butt.pizza.  You will also have to remember this because you will need to add this name in the file ApplicationResources.properties.  In the applicationResources.properties, you do your translation.  button.pizza=Nice Job.   note:  no space in the equal.   This is why this makes struts more extensible.  

3. You will need to change the struts-config file:

 <action path="/doSomething"
            type="com.fitness.pt.actions.doSomethingAction"
            name="measurementForm" scope="request" parameter="submit" validate="false">
      <forward name="success" path="/doSomething.do"/>
    </action>

please note parameter="submit"  this is the property in the .jsp
validate can be false or true since I do not have a validation file, I set it to false.

4.  You will need only one java class to control your routing.  I think this makes it cleaner.  You will need to import LookupDispatchAction.  Please do not inherit PTBaseAction since it has an execute() method.  If you do that, this overrides the execute method of the LookupDispatchAction.  I do not know how to make a PTLookupDispatchAction to make the codes reusable because you have to implement getKeyMethodMap();

The method getKeyMethodMap() is your database.  This tells the class what your button.pizza has to do.  map.put("button.pizza", "buyPizza");    Then you need to implement a method called buyPizza().



  protected Map getKeyMethodMap() {
    Map map = new HashMap();
    map.put("button.update", "update");
    map.put("button.delete", "delete");
    map.put("button.add", "add");
    map.put("button.save", "save");
    return map;
  }

    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    System.out.println("called the update");
    return mapping.findForward("success");
  }

  public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    System.out.println("called the delete");

    return mapping.findForward("success");
  }
   public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    System.out.println("called the update");
   
    return mapping.findForward("success");
  }

  public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    System.out.println("called the delete");
    return mapping.findForward("success");
  }
}

 

by: zenMistress2004Posted on 2004-06-06 at 23:10:48ID: 11246386

I made a mistake in the following>  I used these codes in my application and these are the notes to my colleagues.  I forgot to take out all the application specific codes.  These are:

1. PTBaseAction is specific to our application.  The codes are tested and it works

 

by: struckyPosted on 2004-06-07 at 06:30:34ID: 11249047

zenMistress2004, thanks for the reply.  I actually did get this to work using the following for my exit button code:

<input type="button" value="Exit" onclick='document.forms[0].action="exit.do";document.forms[0].submit();'>

I changed the mapping in struts-config.xml to the following:

<action path="/exit" type="TheCommonActions" parameter="exitToMenu" >
      <forward name="mainmenu" path="/hospcapMenu.jsp" />
</action>

TheCommonActions is a new class extending MappingDispatchAction.  "exitToMenu" is the name of a method in the class.  The method simply has the following return statement:

return actionMapping.findForward("mainmenu");

BTW, regarding your comment that there has to be a space between the " and the /> terminating the <html:bean tag, I don't have a space there and it works fine.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...