GeeCON Prague 2014*
The moose likes Struts and the fly likes LookupDispatchAction + Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "LookupDispatchAction + Javascript" Watch "LookupDispatchAction + Javascript" New topic
Author

LookupDispatchAction + Javascript

Bednarski Adam
Greenhorn

Joined: Mar 21, 2007
Posts: 13
Hi,
I'm using also LookupDispatchAction in my action, but unfortunately I need (I suppose at least) JavaScript simultaneously.

My current situation:
- my class extends LookupDispatchAction
- this class has lot of methods which are called (of course) by getKeyMethodMap()

My target:
I need button to update part of form, unfortunately the label on the button must be different then the key which will be used in getKeyMethodMap()

My solution - _which_is_not_working_:
(Because of security rules on this forum I need to change of "onclick" to "on_click_")
My idea is to set manually method "key" but after clicking my button the action is executing its method called "unspecified" - so the "key" is not set at all - how to do it ?
<html:button
property="method"
on_click="setLabelForGetKeyMethodMap('button.update')">
BUTTON_LABEL_WHICH_IS_NO_APPROPRIATE_FOR_A_LABEL
</html:button>

<script type='text/javascript'>
function updateStatus(method) {
document.PuForm.method.value = method;
document.PuForm.submit();
}
</script>
[ March 21, 2007: Message edited by: bedek bedek ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
bedek bedek,

I'm providing a response to your question below, but before you post again, there's something I'd like you to take care of. Since "bedek bedek" is an obviously fictitious name, it is in violation of our naming policy, which we take quite seriously. Please change the "publicly displayed name" in youruser profile so that it conforms to the JavaRanch naming policy.

My advice in this case is simply to change your Action so that it extends DispatchAction instead of LookupDispatchAction. Then change the property name of all your buttons to something other than method and make method a hidden field. Then for each of your buttons, use JavaScript to set the value of the method field before submitting. Example:

[ March 21, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Bednarski Adam
Greenhorn

Joined: Mar 21, 2007
Posts: 13
Originally posted by Merrill Higginson:
bedek bedek,

I'm providing a response to your question below, but before you post again, there's something I'd like you to take care of. Since "bedek bedek" is an obviously fictitious name, it is in violation of our naming policy, which we take quite seriously. Please change the "publicly displayed name" in youruser profile so that it conforms to the JavaRanch naming policy.

My advice in this case is simply to change your Action so that it extends DispatchAction instead of LookupDispatchAction. Then change the property name of all your buttons to something other than method and make method a hidden field. Then for each of your buttons, use JavaScript to set the value of the method field before submitting. Example:


[ March 21, 2007: Message edited by: Merrill Higginson ]

Hi,
Thank you for your answer, I've changed my public name to more real one, I'm just thinking why It is not possible to use LookupDispatchAction ? For example if I will have such entry in my MessageResources:
...
button.update=button.update
...
will it work ? I think it should be no problem - or maybe I'm wrong ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
There may be a way to make it work, and if you really want to continue using LookupDispatchAction, then go for it.

All I'm saying is that the only real reason to use LookupDispatchAction over DispatchAction is if you want to have the message bundle key used for the button labels act as the means of selecting which method to execute.

If for some reason you can't do that, to me it makes no sense at all to use LookupDispatchAction. All you're doing is making the process a lot more complicated. Using DispatchAction is much more straightforward in a situation where you're using JavasScript to set a value.
Bednarski Adam
Greenhorn

Joined: Mar 21, 2007
Posts: 13
Originally posted by Merrill Higginson:
There may be a way to make it work, and if you really want to continue using LookupDispatchAction, then go for it.

All I'm saying is that the only real reason to use LookupDispatchAction over DispatchAction is if you want to have the message bundle key used for the button labels act as the means of selecting which method to execute.

If for some reason you can't do that, to me it makes no sense at all to use LookupDispatchAction. All you're doing is making the process a lot more complicated. Using DispatchAction is much more straightforward in a situation where you're using JavasScript to set a value.


This code:

<html:submit onklick="document.MyForm.method.value='someDispatchActionMethod'" >
THIS_IS_LABEL_ON_THE_BUTTON
</html:submit>

is not working correctly in my app when I click on this button the app server is throwing error:

2007-03-22 10:21:50,502 ERROR [org.apache.struts.actions.LookupDispatchAction] Action[/MyAction] missing resource in key method map 'THIS_IS_LABEL_ON_THE_BUTTON'

so the javascript method is not setting correctly from some reason the method property.

Just to make it clear in my struts-config.xml I have:

<action path="/Pu"
type="app.MyAction"
name="MyForm"
scope="session"
parameter="method">
<forward name="results" path="results.jsp"/>
</action>

Maybe the problem is that I'm using session ? Or maybe it is impossible to use javascript buttons with LookupDispatchAction ?
[ March 22, 2007: Message edited by: Bednarski Adam ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you'll look closely at my first post, you will see that the suggested solution that I gave was dependent on changing the Action class so that it no longer extends LookupDispatchAction, but extends DispatchAction instead. You apparently haven't changed the inheritance of your Action class, and that's why you're getting the error.

As I mentioned before, I think using LookupDispatchAction in this case does not make sense. However, if you have some reason that I don't know about for continuing to use LookupDispatchAction, here's how you'd do it:

Create an entry in your message bundle:

If you have multiple properties files for different languages, you must put the same key with the same value in each of them. Do not translate "someValue" into the other languages, but leave it exactly as it is.
Then make sure "my.key" is in the key/method lookup table and code:

[ March 22, 2007: Message edited by: Merrill Higginson ]
Bednarski Adam
Greenhorn

Joined: Mar 21, 2007
Posts: 13
Originally posted by Merrill Higginson:
If you'll look closely at my first post, you will see that the suggested solution that I gave was dependent on changing the Action class so that it no longer extends LookupDispatchAction, but extends DispatchAction instead. You apparently haven't changed the inheritance of your Action class, and that's why you're getting the error.

As I mentioned before, I think using LookupDispatchAction in this case does not make sense. However, if you have some reason that I don't know about for continuing to use LookupDispatchAction, here's how you'd do it:

Create an entry in your message bundle:

If you have multiple properties files for different languages, you must put the same key with the same value in each of them. Do not translate "someValue" into the other languages, but leave it exactly as it is.
Then make sure "my.key" is in the key/method lookup table and code:


[ March 22, 2007: Message edited by: Merrill Higginson ]


Using this approach do I need to:
1) have "method" member in my form ?
2) put this member as <html:hidden... to my page ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Bednarski Adam:


Using this approach do I need to:
1) have "method" member in my form ?
2) put this member as <html:hidden... to my page ?


The answer is yes to both questions.
Bednarski Adam
Greenhorn

Joined: Mar 21, 2007
Posts: 13
Here are my experiences:

It seems that I've resolved my problem, the main mistake was calling a property of from "method" - this seems to be bad idea, it is somehow confusing Struts (or Java ?) - so in conclusion, everything now works fine but I needed to change "method" name of form member to "dispatch" (or anything that is not "method")
 
GeeCON Prague 2014
 
subject: LookupDispatchAction + Javascript