1

I have a hashmap as variable in baking bean like.

 private Map<String, Map<String, String>> states;

At JSP page, I have two combo menu 1st is for country and second is for states. I need to populate second combo menu based on selected country on first combo menu, Mapping is there at above hash map. Any suggestions. I am using spring mvc and jsp. I can access above map at javascript using

 stateMap ='${resetForm.states}';

But it will be a string. Please help

Thanks

HKumar
  • 655
  • 1
  • 8
  • 18
  • One way is to create iterate through it and create a JavaScript map. But it willl be lot of iteration. So looking for best solutions. – HKumar Aug 14 '14 at 08:29
  • What does your `Map> states` contain? Could you be more explicit( maybe show us possible values for this map) and what values you expect to be in your states combo box as auto populated based on selected country? – Daniel Aug 14 '14 at 13:41
  • @Danial Map> states will have value as USA--> value map will have 57 states of USA as short name as key and full name as Value Canada--> map of province and same for other country – HKumar Aug 18 '14 at 16:26

2 Answers2

1

You should always separate as much as possible the server-side from client-side, else you'll end up having a hard to mantain, not understandable and error prone code. That being said, you can convert your map to JSON and send that json to the client-side, so it can be handled as you want:

<script>
    var statesJSON = JSON.parse( <%= net.sf.json.JSONSerializer.toJSON(states) %> );
</script>

Now you can write your client-side code (regarding the population of the drop-down lists) by using the statesJSON global variable. You can follow these links for how to populate a drop-down list based on selection on another :

Community
  • 1
  • 1
Daniel
  • 1,861
  • 1
  • 15
  • 23
  • thx Daniel I wil give it a try. Meanwhile I settle with ajax call to controller to retrieve values when 1st dropdown changed, – HKumar Aug 21 '14 at 06:42
0

you can use JSTL,but not use map in view. for example,use ,you can get the key and value from the map. of courese ,use EL expression can archieve your goal.