aspose file tools*
The moose likes Struts and the fly likes Probably a very simple Struts form question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Probably a very simple Struts form question?" Watch "Probably a very simple Struts form question?" New topic
Author

Probably a very simple Struts form question?

A Harry
Ranch Hand

Joined: Jan 23, 2002
Posts: 124
I have a struts app but am not using the tab library stuff (too many reasons to go in to). Seriously considering using them though but have probably a very simple but question I can't seem to find an answer to -
My web page has 5 buttons on WITHIN 1 form, each button calls (terminology) a different Action command set via a Javascript function. So for example a "Show Projects" button when clicked calls gotoProjectsPage() below -
function gotoProjectsPage()
{
gotoPage("projects");
}
function gotoPage(page)
{
var oForm = top.document.frmMain;
oForm.action = page + ".jsp";
oForm.submit();
}
This works fine but if I decide to use the <html:form> tag how so I achieve the same? - the tag requires 1 action!
Do you have to hava a <html:form> around every button or wot?
many thanks for any help!
harry
Bart Fransen
Greenhorn

Joined: Feb 12, 2004
Posts: 4
Hi Harry,
I had the same problem and solved it by submitting the form to one action, which then determined what button was pressed and forwarded the request to the appropriate action.
So basically, you're moving the decision on which action to call from the client browser to the servlet.
HTH,
Bart
Norma Caballero
Greenhorn

Joined: Jan 30, 2004
Posts: 27
Hi
I've been in those situations where you dont know exactly what action you need to call. What I do is that I have a javascript function like:

And on the onclick in each button you would call the DoAction method sending the action you want to execute as parameter.
This way you only have one function that submit all the actions you may need.
Good Luck!
A Harry
Ranch Hand

Joined: Jan 23, 2002
Posts: 124
Thanks for replying
Norma this sounds like what I'm doing now, only I was a bit worried if I use the struts <htm;:form> stuff there might be a problem. As long as the field values still get passed in the form object when calling an action NOT specified in the "action" attribute that's fine?
I have to call a javascript function sometimes to allow the user to enter things like passwords etc... to access certain functionality!
Norma Caballero
Greenhorn

Joined: Jan 30, 2004
Posts: 27
Hi
I'm glad this is useful to you. But I dont understand your question very good. Could you be more specific?
PramilaT Thakur
Greenhorn

Joined: Aug 21, 2003
Posts: 8
Hi,
There could be another solution for this. You could use
<html:form action=".someAction.do">
<input type="submit" name="submit" value="Click"/>
<!-- add other buttons-->
All to be named as submits, but different values
</html:form >
In the action class just determine what the value of submit button is.
And depending on that determine the forward page name and return that.
But remember these action forwards should be included in th e struts-config.xml
Hope this helps.
Enjoy.
Pramila Thakur
SCJP,SCJD, SCWCD
PramilaT Thakur
Greenhorn

Joined: Aug 21, 2003
Posts: 8
Hi,
There could be another solution for this. You could use
<html:form action=".someAction.do">
<input type="submit" name="submit" value="Click"/>
<!-- add other buttons-->
All to be named as submits, but different values
</html:form >
In the action class just determine what the value of submit button is.
And depending on that determine the forward page name and return that.
But remember these action forwards should be included in th e struts-config.xml
Hope this helps.
Enjoy.
Pramila Thakur
SCJP,SCJD, SCWCD
Srikanth Shenoy
author
Ranch Hand

Joined: Jan 24, 2004
Posts: 184
Hi,
I seriously doubt if Harry's current design (based on Model 1) needs Struts at all. I mean, there is absolutely no advantage of migrating a Model 1 based design to Struts.
Let me be more specific:
From your description it looks like you want to forward to another page when a button is clicked (using javascript form submission).
All the procesing logic is also probably located in the JSP which pulls the request parameter.
Hence there is no use in having a Action just to do a forward.
Anyway inspite of all these if you want to use html:form to do the submissions to the next page, here is what you should do:
1) Use a DispatchAction instead of the plain vanilla Action
2) Add the buttons with the same name (as one of the previous mails pointed out)

3) In the DispatchAction define methods named as projects(), sockets() (These are the values of the button in the form)
4)Set up the DispatchAction in struts-config.xml
The advantage of this approach is that you dont have code the execute to decide which method should be called. Instead the DispatchAction calls the projects() method when projects buttoin is pressed, sockets() method when the sockets() method is pressed.
I know this is too short of an explanation for a topic like DIspatchAction.
If you want to know more about DispatchAction, download a free chapter from a Struts book that I recently wrote. It is available at:
Download a Free chapter covering DispatchAction
Srikanth Shenoy
Author: Struts Survival Guide
[ March 02, 2004: Message edited by: Srikanth Shenoy ]

Srikanth Shenoy
Author of Struts Survival Guide : Basics to Best Practices
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Probably a very simple Struts form question?