You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by vinodh r <vi...@yahoo.com> on 2010/08/27 07:04:48 UTC

I cant load Select Box dynamically

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>



      

Re: I cant load Select Box dynamically

Posted by Michal Ciasnocha <mc...@struts.cz>.
  Basic example:

you have 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>


and your are accessing action CountryAction. In class CountryAction you 
must create getter method for list attibute of select tag. Your list has 
name "countryList". So you need to create getter method
List<SomeClass> getCountryList() { return countryList; }

In countryList attribute you must have values which you want to get on 
your page. Your code have populate method - here you get data from 
database and these values are saved into list variable. Why do you have 
this variable named list and not countryList? Method getCountryList() 
returns empty variable. Information which you need are in list variable, 
but you are not accesing it - you getter calls countryList variable. 
Just rename list variable to countryList.

vinodh r wrote on 27.8.2010 12:14:
> Hi, Thanks for reply Mr.Michal Ciasnocha... I cant understand i need some explanation, i don't know how to set in a attribute. 
> Also I can't copy the result returned from " Data_Conn.load_country() to another list or array list. only chance is to use " Data_Conn.load_country()" as a original.
> if you know please do some modification on action page and paste as reply. Thanks in advance.
> Vinodh .R--- On Fri, 8/27/10, Michal Ciasnocha<mc...@struts.cz>  wrote:
>
> From: Michal Ciasnocha<mc...@struts.cz>
> Subject: Re: I cant load Select Box dynamically
> To: "Struts Users Mailing List"<us...@struts.apache.org>
> Date: Friday, August 27, 2010, 3:14 AM
>
>    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>
>>
>>
>>
>>
>
>
>

I cant load Select Box dynamically

Posted by vinodh r <vi...@yahoo.com>.
Hi, Thanks for reply Mr.Michal Ciasnocha... I cant understand i need some explanation, i don't know how to set in a attribute. 
Also I can't copy the result returned from " Data_Conn.load_country() to another list or array list. only chance is to use " Data_Conn.load_country()" as a original.
if you know please do some modification on action page and paste as reply. Thanks in advance.
Vinodh .R--- On Fri, 8/27/10, Michal Ciasnocha <mc...@struts.cz> wrote:

From: Michal Ciasnocha <mc...@struts.cz>
Subject: Re: I cant load Select Box dynamically
To: "Struts Users Mailing List" <us...@struts.apache.org>
Date: Friday, August 27, 2010, 3:14 AM

  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>
>
>
>
>



      

Re: I cant load Select Box dynamically

Posted by Michal Ciasnocha <mc...@struts.cz>.
  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>
>
>
>
>