aspose file tools*
The moose likes Struts and the fly likes OK to make the value of action attribute of a form a variable ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "OK to make the value of action attribute of a form a variable ?" Watch "OK to make the value of action attribute of a form a variable ?" New topic
Author

OK to make the value of action attribute of a form a variable ?

Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hi All,
I have a jsp page that I am reusing for 3 differant conditions. Basically it has the same content, but sometimes shows 3 rows and 3 columns of data, other times 1 row and 9 columns of data, etc. So, I added a numColumn and numRow value to my form so I can render the jsp on the fly for differant configurations.

Now I am wondering if this is a good idea, because if someone clicks on one of the controls on the page, I need to go to a differant action based on what configuration of the page I am currently showing, so I am thinking of doing something like this in javascript:




My question is, am I trying too hard to re-use code that maybe shouldn't be re-used? I could just have 3 similar jsp pages, each with thier own action. Would either be considered better practice, or is reusing the jsp considered an OK thing to do?

To make the jsp page configurable, there are a lot of scriptlets with for loops and I am also not sure if this also is an ok idea.

Any opinions appreciated, and thanks in advance for your time.
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
From my experience, if you are changing the form's action using javascript, you aren't doing things the best way. Use of scriptlets falls under the same category, although you may be able to use <logic:iterate> instead of the for loops.

You might be able to get some good advice if you give a little more detail. Either way, you might want to read up about DispatchAction.

You should be able to use a combination of tiles and custom tags to get the most reuse.
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Thank you for your reply. Here is more information about my application.
I will be rendering a page given a viewType, for the sake of argument, lets say my view types are named vOne, vTwo, and vThree.
vOne will show data in a table like this :

item1 item2 item3
item4 item5 item6
item7 item8 item9

vTwo will show the data like this

item1 item2 item3 item4
item5 item6 item7 item8

vThree will show the data like this

item1 item2 item3 item4 item5 item6 item7

So, when i get a request for my application (it comes from a differant app) i will get a URL like http://localhost/myApp/init.do?viewType=vThree

my init.do action will determine that I need to forward to vThreeAction.do because of the value pulled from the request, in vThreeAction.do, I will tell the form that I should use one row with 7 columns (they are form values, numRows, numColumns). If the url had been http://localhost/myApp/init.do?viewType=vOne, I would go to vOneAction.do, where I would set numRows = 3 and numColums=3.

I then render the jsp page.

So, for these three views, I only need one jsp page, one struts form, and 3 struts actions. The tricky part is, If I want to submit the form to preform some action, like sorting, then I somehow have to know that I am submitting to vOneAction.do, vTwoAction.do, or vThreeAction.do.

Am I making this too complicated? Should I just use 3 differant jsp's ?
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
You might not even have to pass around viewType and you might be able to avoid populating numRows and numColumns. All you should have to do is populate a multidimensional list and put that in the form. For example:

item1 item2 item3
item4 item5 item6
item7 item8 item9

Represented in your action can be:

And your jsp:


Then use dispatch action to call the correct action.

Deciding whether or not to use 3 different pages is going to be based on the similarity of things, but technically you can get away with one that is not very complex.
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Dom,
I can't thank you enough! This approach worked like a charm. I did already have the vector type code you suggested, just was iterating over the list of lists using scriptlets, I didnt think of the fact that I could get and use the <iterate tag, one of those "not seeing the forrest for the trees" things I guess.

Thanks so much for your time, my page is so improved by this !

Now off to try to get the Dispatch action to work . . .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OK to make the value of action attribute of a form a variable ?
 
Similar Threads
Seeing wierd action behavior
Struts 2 <s:submit action > and javascript help
Query String Problem
form bean value not getting
Ajax javascript not recieving responseText from Struts