Hi,

in class CountryAction you need assign retreived values from database to countryList attribute, not list (you have in jsp ... list="countryList" ...).

vinodh r wrote on 27.8.2010 7:04:
Hi,I cant load Select box dynamically. I am new to struts2, but i can do it 
statically in action page so that it will refelect in jsp page. But what i have 
to do modification in action page so that i get display in jsp page i loaded 
array list dynamically from database. Thanks in advance.

Here is the action
//CountryAction .javapackage com.pac.struts.action;import 
java.sql.SQLException;import java.util.ArrayList;
import com.opensymphony.xwork2.ActionSupport;import com.pac.struts.DataConn.Data_Conn;import 
com.pac.struts.model.Country;public class CountryAction extends ActionSupport{     private static final 
long serialVersionUID = 1L;        private String country; private ArrayList<Country>  countryList;  
public ArrayList list;  public String populate() throws Exception{              list = 
Data_Conn.load_country();                return "populate";    }       public String execute(){   
             return SUCCESS; }       public String getCountry(){             return country; }       public 
void setCountry(String country){         this.country = country; }       public ArrayList<Country>  
getCountryList(){              return countryList;     }       public void 
setCountryList(ArrayList<Country>  countryList){              this.countryList = countryList; }}


/****Data_Conn.java**/package com.pac.struts.DataConn;
import java.sql.Connection;import java.sql.DriverManager;import 
java.sql.ResultSet;import java.sql.SQLException;import 
java.sql.Statement;import java.util.ArrayList;
import com.pac.struts.model.Country;
public class Data_Conn {        static Connection con;  static Statement st;
        static ResultSet rs;    public static void connection_1() throws ClassNotFoundException, SQLException{    
      Class.forName("com.mysql.jdbc.Driver");               con = 
(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=root");
                st = con.createStatement();             System.out.println("Open Connection 
Initiated");      }       public static void close_1() throws SQLException{       
        st.close();             con.close();            System.out.println("Close Connection 
Initiated");     }               public static ArrayList<Country>  load_country() throws 
ClassNotFoundException, SQLException {            connection_1();         rs = st.executeQuery("select * 
from test.country_tbl");               ArrayList<Country>  countryList = null;   
        while(rs.next()){                       countryList = new ArrayList<Country>();   
                countryList.add(new Country(rs.getInt(1), rs.getString(2)));            }       
        close_1();              System.out.println("List Operation Completed");       
        return countryList;     }                       
        }
/*****Country.java***/package com.pac.struts.model;
public class Country {  private int countryId;  private String countryName;     
        public Country(int countryId, String countryName){              
this.countryId = countryId;             this.countryName = countryName; }

        public int getCountryId() {             return countryId;       }
        public void setCountryId(int countryId) {               this.countryId 
= countryId;     }
        public String getCountryName() {                return countryName;     
}
        public void setCountryName(String countryName) {                
this.countryName = countryName; }       
}

/*****countryworld.jsp**/<s:form action="CountryAct"><s:select name="country" list="countryList" listKey="countryId" 
listValue="countryName" headerKey="0" headerValue="Country" label="Select a country" /></s:form>




Reply via email to