aspose file tools*
The moose likes Struts and the fly likes property values not received by ActionForm or ActionForward's execute method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "property values not received by ActionForm or ActionForward Watch "property values not received by ActionForm or ActionForward New topic
Author

property values not received by ActionForm or ActionForward's execute method

Sanjay Virkar
Greenhorn

Joined: Apr 03, 2005
Posts: 19
I have a struts application in which on first form I just display few buttons. Idea is that user clicks on a button, and different jsp page is shown to user.

I use common practice of assigning value in onclick of button to the hidden variable as follows.
======================================================
html:form action="welcome.do" method="POST"
html:hidden property="action"
html:submit onklick="javascript ocument.form[0].action.value='usrmnt';
fmt:message key="welcome.button.UserMaintenance";
html:submit
..
..
** Assume there are "greter than" "less than" signs at the begining and end. I had to strip them as system did not let me submit question with them in place.
** Assume the spelling on onklick is correct in the code

(html:submit code repeats for all buttons assigning different values to "action" property)
======================================================

This property is retrieved in WelcomeAction as follows.
======================================================
String action = (String)((WelcomeForm)form).getAction();
======================================================

WelcomeForm has getter and setters for "action" property.
======================================================
public class WelcomeForm extends ActionForm
{
private static org.apache.log4j.Logger log = Logger.getLogger(LoginAction.class);
private String adminaction;
public void reset(ActionMapping mapping, HttpServletRequest request)
{
adminaction="";
}
public String getAction()
{
return adminaction;
}
public void setAction(String c)
{
log.info("setAction was called with value of " + c);
adminaction = c;
}
}
======================================================
Problem is- value assigned in jsp by javascript onclick method, is not passed to ActionForm. It is also not available in Action. So in execute method of ActionForward for following code

String action = (String)((WelcomeForm)form).getAction();

I get "" similarly as you see in Action form too, I have introduced log.info statement to see what values are set in Action form and there too I get "" for action property.

Not sure what I have done wrong.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

"thumbsup",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sanjay Virkar
Greenhorn

Joined: Apr 03, 2005
Posts: 19
Thanks for pointing that out.
I will change it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Thank you!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
There are two problems here. The first is that in specifying an onclick event, you don't precede the JavaScript command with "javascript:". Just enter the command. Example:

The second problem is your choice of the name action for a parameter name. Since action has meaning in JavaScript (it also refers to the action attribute of a <form> tag) you should never name a property action if you intend to use it in a JavaScript statement. Change the variable name to actionParm or something else. Note that you should also avoid names like submit and reset.
[ May 07, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Sanjay Virkar
Greenhorn

Joined: Apr 03, 2005
Posts: 19
I took the onclick javascript syntax from the book 'Jakarta Struts for Dummies.' Also, this syntax works for me in other programs.
Here are lines from their sample program included in the book
=========================================================
<c:choose>
<c:when test='${not empty param.id}'>
<html:submit onklick="javascript ocument.form[0].action1.value='update';">
<fmt:message key="album.button.update"/>
</html:submit>
</c:when>
<c therwise>
<html:submit onklick="javascript ocument.forms[0].action1.value='save';">
<fmt:message key="album.button.save"/>
</html:submit>
</c therwise>
</c:choose>

Secondly I did change the name of 'action' to 'adminaction' still no luck. I am still not getting values in the Form or in Action. There is something very subtle I am doing wrong and I can't figure out what
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
When you changed the property to "adminaction", did you remember to also change the property and getter/setter in your ActionForm? If you're using this in a DispatchAction, did you remember to change the "property" attribute of the action path?

Also, I'd be wary of using the "forms[0]" construct. This assumes that the form is the first form on the page. If you make this JSP part of a tiles template, this may no longer be the first form. I prefer using the actual name of the form, which in Struts is what you specified as the name attribute of the action mapping in your struts-config.xml file. Exmple:


Regarding the "javascript:". Yup, you're right, it works. I hadn't really used that construct before in specifying JavaScript events, so I stand corrected. It's still unnecessary, though, so I don't know that you gain anything by using it. I still prefer just writing the javascript code in the event.
[ May 08, 2007: Message edited by: Merrill Higginson ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi Sanjay,

Even i dont see anything wrong. But sometimes it might happen with JavaScript.

You can probably check in the front end by putting some alerts inside the JavaScript function itself. If thats through i dont think there would be an issue with either ActionClass or ActionForm in manipulating the values.

HtH.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Sanjay Virkar
Greenhorn

Joined: Apr 03, 2005
Posts: 19
Thanks everyone!

Merril, your following directions really helped. THANK YOU!

>I prefer using the actual name of the form, which in Struts is what you >specified as the name attribute of the action mapping in your struts->config.xml file. Exmple:
>document.myForm.adminaction.value="xyz"

Even though it was first and only form in jsp, and even though I am not using tiles, for some reason, forms[0] did not set values. After changing to the form name, everything is working as expected. For some reason same code (i.e. with forms[0]) is working in other jsp correctly. But for safety there too I will introduce form name.

Thanks again!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thats great Sanjay!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: property values not received by ActionForm or ActionForward's execute method