aspose file tools*
The moose likes Struts and the fly likes Shoud I use window.location.replace() with Struts? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Shoud I use window.location.replace() with Struts?" Watch "Shoud I use window.location.replace() with Struts?" New topic
Author

Shoud I use window.location.replace() with Struts?

Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hi All,
General question here.
I have a page that has several select box's on it. One of the select box's depends on the selected item of another, so when the one box gets an item selected, I do a submit back to the action so I can populate the second drop down accordingly.

My question is this, I have seen javascript added to jsp pages that will do a window.location.replace, and put all of the data on the page appended to the url, in order to not have to reload all of the select box lists when getting back to the action. My question is, is this really worth it since you have to go to the effort of parsing everything off of the url? Or is it just as well to re-hit the database to get the data for the other drop downs when getting back to the action if there is a submit on the drop down in question.

Or , maybe there is another way to handle this that is better that I haven't thought of?

Thanks for any advice!
Kim
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
I'm not sure what you are talking about when you say you've seen people use window.location.replace(), but if you have dropdowns that are dependant on other dropdowns, there are a couple of things to do:

1) If the list is large, use AJAX or do a round trip like you are currently doing.
2) Send a list of all of the options and use JS to add/remove options from the dependant dropdown.

i.e. If you have 2 drop downs, 1 called manufacturer, and one called model that depends on manufacturer, you can do something like this in JS:

var allCars = new Array();
<logic:iterate name="myForm" property="allCars" id="currentCar">
var currentCar = new Object();
currentCar.manufacturer = "<bean:write name="currentCar" property="manufacturer"/>";
currentCar.model = "<bean:write name="currentCar" property="model"/>";
allCars.push(currentCar);
</logic:iterate>

Then when the user changes the manufacturer dropdown, clear the second drop down and repopulate it based on the js allCars array. This could be a good method of there isn't too much data.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Ditto mostly what Dom said. On a previous project we used JavaScript for smaller list and a round-trip for larger lists (I think "smaller" versus "larger" was a judgment call). On my current project all dependant lists are done using a round-trip. This was partly because it was easier to just implement one strategy but mostly because the lead and the client wanted us to avoid JavaScript.

Now that I am the lead and the client has realized that it is impossible to create an interactive web site without using some JavaScript it is likely that in the next few months we will start replacing the dependant lists that use a "submit and refresh" cycle with an Ajax solution. For these basic types of operations it seems that Ajax is the way to go...you avoid an ugly page refresh, you don't have the overhead of loading every possible selection when the page is shown and you avoid bloating your pages with JavaScript.

- Brent
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Thank you so much for your reply! This sounds like a great solution but I am still a bit confused about how to do the javaScript. If I have 2 drop downs, 1 called floors, and one called rooms that depends on floor, and the user selects a floor, how do I get the list of rooms PER that floor?

I would have a relationship like this :

Floor 1 : Rooms 101, 102, 103 (like in a hotel)
Floor 2 : Rooms 201, 202, 203, 204, 205
Floor 3 : Rooms 301,302

In my form, I would have a list of floors (1,2,3)
Then I assume I would need a hashmap, with floor being the key, and then rooms being a list which is the object in the hashmap.

so, I am guessing in javascript psuedo code, I need to say :

function reloadRoomList(FloorValue){
List roomlist = new List;
get roomList from form where floor= floorValue;
set the selectBox on the page to use this new roomlist
}

I am just not sure how to actually write this in the javascript. Do I have the concept correct though?
Thank you again so much, this is just the kind of solution I was looking for.
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
I've made an HTML file for you, but I can't get it to copy into the message because of illegal function names and such. Anyone know a way I can get it posted without having to modify every other character?

Edit- I uploaded it here:
http://www.savefile.com/files/420586
[ January 17, 2007: Message edited by: Dom Lassy ]
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Wow, thanks so much for investing the time to do that! I am going to give this a try.
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Still struggling with this one!
Trying to get the info from my form into the javascript variables is confusing me.
I have a hashmap in my form that is just like the javascript arrays that you show in your example, I just can't seem to get the values from the hashmap in the form into javascript arrays.

Any ideas?
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I have done this before. The confusing part is that you have to write Java/scriptlet code in your JSP that generates the JavaScript code. I always get confused when I have to mix the two. Be sure to look at the source code in the browser to make sure your JSP is generating the correct JavaScript code.

Edited to say that you technically do not have to use scriptlet code in your JSP...you might be able to do all of this with jstl tags or Struts tags such a logic:iterate and bean:write.

- Brent
[ January 18, 2007: Message edited by: Brent Sterling ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shoud I use window.location.replace() with Struts?