0

In my Spring Application i have two Select boxes.

first one is Country List

Second one is State List.

When we are select one Country Related State Will be Displayed.

In our Jsp we can manage to Display List Using Jquery.

But How to Prepare This List in Spring(Java)?

Please Suggest me How to Do this In Java?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Java Developer
  • 1,873
  • 8
  • 32
  • 63
  • How to Prepare The List of Country and State .. if we give one example for one country, i will Prepare All the Country List and States. – Java Developer May 02 '13 at 05:32
  • @AndrewThompson.. This is for Spring.. When i'm before asking this Question, i'm also see your question.. – Java Developer May 02 '13 at 05:43
  • You're looking for Spring MVC 3 + Ajax solution (since you say you can manage to display the data using jQuery). After some search on the web, I found [this decent Q/A](http://stackoverflow.com/q/7196181/1065197). For your fortune, the case is for country/state. Still, it would be **better** if you search about spring mvc 3 and ajax. – Luiggi Mendoza May 02 '13 at 06:00
  • Exactly same when we create List object for List of Countries, We are using Model Attribute we can get value.. – Java Developer May 02 '13 at 06:07

3 Answers3

0

Create two Bean Interfaces Country and State like :

interface Country {

 State state;

}

interface State {

  List<String> states;

}

Implement these interfaces with simple getters and setters and then define your initiation configuration in spring.xml file like

<bean id ="CountryX" class="CountryImpl">
//Give reference to State bean say StateX in this case.

<bean id ="StateX" class="StateImpl">
// here set the list of states 
Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
  • this process is very hard but i'm thinking Short cut to get all country and state.. – Java Developer May 02 '13 at 05:40
  • In case you want to do it with Spring, you have to define all the configuration in spring configuration file. And Once you do that, then there is nothing else you have to do .. its done – Vineet Singla May 02 '13 at 05:42
  • i'm comfortable only using annotation.. so i'm not thinking spring configuration files.. – Java Developer May 02 '13 at 05:46
  • Then you might have to create your own custom annotations like @interface MyAnnotation { String State(); //Code goes here } – Vineet Singla May 02 '13 at 05:48
  • Actually i'm thinking first Create List of Available Counties then Using This every Object in List We can create Another List for States. If Using Locale Class we can get Countries but Hear how to Prepare States? – Java Developer May 02 '13 at 05:54
  • probably the same way you are creating countries. Can you show me the code you have written and tell me the point where you are stuck ? – Vineet Singla May 02 '13 at 06:01
0

One more approach would be to use Map<String,List<String>> in your Spring bean xml

 <bean id="beanName" class="package.CountryPojo">
     <property name "countryMap">
        <map>
           <entry key="USA">
              <value>
                 <list merge="true">
                     <value>MN</value>
                     <value>CA</value>
                 </list>
              </value>
           </entry>
           <entry key="UK">
              <value>
                 <list merge="true">
                     <value>XY</value>
                     <value>IJ</value>
                 </list>
              </value>
           </entry>
        </map>      
     </property>
 </bean>

Having your bean class like

class CountryPojo {

   private Map<String,List<String>> countryMap;

   //getters : setters

}
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • Thanks for Your Replay.. Correct for Using Map Object but we have to maintain nearly 100 of countries in that case XML is heavy.. is their any other way? – Java Developer May 02 '13 at 05:49
  • The best way would be to store the info in RDBMS, since you opted spring, I thought above solution can help – sanbhat May 02 '13 at 05:50
0

Create 2 Bean interfaces Country and State like below :

 interface Country {

 State state;

}

interface State {

  List<String> states;

}

Implement these interfaces with getter and setter and then initialize the configuration in spring.xml file in below way:

  <bean id ="CountryTemp" class="CountryImpl">
//Give reference to State bean say StateTemp in this case.

<bean id ="StateTemp" class="StateImpl">
// here set the list of states 
NullPointer
  • 7,094
  • 5
  • 27
  • 41