LookupDispatchAction, buttons with the same name....

N

nkunkov

Hello,
I'm using LookupDispatchAction and this action is used from a form in
which depending on the request parameter I will either have a save and
clear button, or a save, cancel and update button.
The problem is that when I only have a save and clear button, the save
button should call the savePerson method, when I have three buttons
the save button should call update method. One name, different
methods, but still one form, one jsp page.
What happens is that no matter which Save button I click it always
goes to update method.
When i rename one of the buttons in my resource file to something
else, all works perfectly fine.
I read somwhere that this is actually the expected behavior. If
that's the case, is there a good standard way of getting around this
problem? If this is not expected behavior then what am I doing wrong?
Any help will be appreciated.
Thanks
NK

here is my keyMap:
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.Save", "savePerson");
map.put("button.UpdateSave", "update");
return map;
}
----------------------------------------------------
Here is my resource file:

button.Save=Save
button.UpdateSave=Save
-----------------------------------------------------
in my jsp i have the following (cut down version of course):
<c:choose>
<c:when test='${empty requestScope.FixErrorsPage}'>
<html:submit property="method" >
<bean:message key="button.Save" />
</html:submit>
</c:when>
<c:eek:therwise>
<html:submit property="method" >
<bean:message key="button.UpdateSave" />
</html:submit>
</c:eek:therwise>
</c:choose>
 
L

Lew

Hello,
I'm using LookupDispatchAction and this action is used from a form in
which depending on the request parameter I will either have a save and
clear button, or a save, cancel and update button.
The problem is that when I only have a save and clear button, the save
button should call the savePerson method, when I have three buttons
the save button should call update method. One name, different
methods, but still one form, one jsp page.

Strictly speaking, buttons don't call server methods, buttons report
information to server methods.

I am not so familiar with the getKeyMethodMap() idiom you showed, probably
because I avoid the Struts bean: and logic: custom tag libraries in favor of
HTML and JSTL. I usually use the button value as the key to the handler,
either directly in the code (bad!) or via a property file similar to what you
showed. That is pretty much the same as what you have, except that instead of
the bean:message value, it keys off request.getParameter( submitButtonName ).

The property file would fill the Map similarly to how you did it:

properties:
---------
button.save = Save
button.update = Update
---------

code fragment (error-handling elided):
---------
Map handlers = new HashMap();
handlers.put( props.getProperty( "button.save" ), SavePersonHandler.class );
handlers.put( props.getProperty( "button.update" ), UpdatePersonHandler.class );
---------

JSP fragment:
---------
<c:choose>
<c:when test='${empty fixErrorsPage}'>
<input type="submit" id="save"
name="submit" value="${save}" />
</c:when>
<c:eek:therwise>
<input type="submit" id="update"
name="submit" value="${update}" />
</c:eek:therwise>
</c:choose>
---------
where the EL variables 'save' and 'update' have been defined from the properties.

Sorry, I realize this doesn't answer your question. It's just another view at
how one might do it.

-- Lew
 
A

Alan Cui

Instead using LookupDispatchAction, you could use DispatchAction for
this case by adding a hidden input in your page.

When the form being submitted, change the hidden input's value by
calling javascript. And configure the dispatch parameter as the hidden
input's name in you struts-config.xml.

Good luck, anything unclear you could ask here or refer to struts's
documents.
 
C

ck

Instead using LookupDispatchAction, you could use DispatchAction for
this case by adding a hidden input in your page.

When the form being submitted, change the hidden input's value by
calling javascript. And configure the dispatch parameter as the hidden
input's name in you struts-config.xml.

This would fail if Javascript is disabled. LookupDispatchAction is a
better alternative to DispatchAction, in case one wants to scrap out
Javascript from UI. Imagine what happens if you have 100 and 1000 of
CRUD page?

Something seems to be wrong here

Should both button? Save and button.UpdateSave have the same value
Save? Obviously it would call Only save method right?

Another thing that comes to my mind is that why you have

handlers.put( props.getProperty( "button.save" ),
SavePersonHandler.class );
handlers.put( props.getProperty( "button.update" ),
UpdatePersonHandler.class );

..class? why?

Should you not be decalaring ActionForward methods? Like

public ActionForward save(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
public ActionForward save(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)

You can refer to http://husted.com/struts/tips/003.html to get more
clear picture of the problem.
 
N

nkunkov

This would fail if Javascript is disabled.LookupDispatchActionis a
better alternative to DispatchAction, in case one wants to scrap out
Javascript from UI. Imagine what happens if you have 100 and 1000 of
CRUD page?

Something seems to be wrong here


Should both button? Save and button.UpdateSave have thesamevalue
Save? Obviously it would call Only save method right?

Another thing that comes to my mind is that why you have

handlers.put( props.getProperty( "button.save" ),
SavePersonHandler.class );
handlers.put( props.getProperty( "button.update" ),
UpdatePersonHandler.class );

.class? why?

Should you not be decalaring ActionForward methods? Like

public ActionForward save(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
public ActionForward save(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)

You can refer tohttp://husted.com/struts/tips/003.htmlto get more
clear picture of the problem.

Hi, Thanks everybody for all your thoughts.
But yes, my problem is that I have avoid Java Script at all costs.
The second problem is that I have only one form and it HAS TO have
either two buttons or three buttons depending on the request
parameter, and in each case one of the buttons always MUST be save.

I also am not sure about Handler.class. In my code i just implement
regular getKeyMethodMap
protected Map getKeyMethodMap() {

Map map = new HashMap();
map.put("button.Save", "saveStaffMember");
map.put("button.UpdateSave", "update");
return map;
}

The problem of course is in the properties file:
button.Save=Save
button.UpdateSave=Save
The buttons have to be named the same.
I'm even thinking to split this action into two actions, but then on
the form the name of the action should be dinamic, maybe passed as
request parameter...
If you have any other elegant ideas, i'd like to hear them..
Thanks again.
NK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top