Solved

STRUTS: Multiple buttons in a form

Posted on 2003-11-20
14
80,706 Views
Last Modified: 2011-08-18
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!

0
Comment
Question by:rusco
  • 3
  • 3
  • 2
  • +4
14 Comments
 
LVL 2

Expert Comment

by:DidierD
ID: 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
0
 
LVL 2

Expert Comment

by:DidierD
ID: 9795376
Forgot the quotes
   
 document.yourForm.action="nextPage.do";
 
0
 
LVL 6

Expert Comment

by:kotan
ID: 9795527
0
 

Author Comment

by:rusco
ID: 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 :-)
0
 

Author Comment

by:rusco
ID: 9795646
Hi Kotan,

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

Expert Comment

by:kotan
ID: 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
0
 

Author Comment

by:rusco
ID: 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!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:DidierD
ID: 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
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9796013
This is a good way of doing it (I think):

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

Accepted Solution

by:
kennethxu earned 50 total points
ID: 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.
0
 

Expert Comment

by:strucky
ID: 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.
0
 

Expert Comment

by:zenMistress2004
ID: 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");
  }
}

0
 

Expert Comment

by:zenMistress2004
ID: 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
0
 

Expert Comment

by:strucky
ID: 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.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A procedure for exporting installed hotfix details of remote computers using powershell
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now