Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

STRUTS: Multiple buttons in a form

Posted on 2003-11-20
14
Medium Priority
?
80,814 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Files go missing when using DFS (Distributed File System) Replication and how to recover them and fix it.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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