1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

Struts Tutorials

Q.How to create a drop down list in Struts?

This is the code to display drop down list and retrive seleted value in struts.
medList list contains list of Medium objects.

Java :
bean class.
public class Medium {
    int medId;
    String medName;

    /**
     * @return Returns the medId.
     */
    public int getMedId() {
        return medId;
    }
    /**
     * @param medId The medId to set.
     */
    public void setMedId(int medId) {
        this.medId = medId;
    }
    /**
     * @return Returns the medName.
     */
    public String getMedName() {
        return medName;
    }
    /**
     * @param medName The medName to set.
     */
    public void setMedName(String medName) {
        this.medName = medName;
    }
}

In the Form Class :

public class MediumForm {
private List medList;
private String med;
    public void setMedList(List medList){
        this.medList = medList;
    }
    
    public List getMedList(){
        return this.medList;
    }

    public void setMed(String medList){
        this.med = med;
    }
    
    public String getMed(){
        return this.med;
    }
    

}

In the Action class :

List medList = DAO.getMediums();
form.setMedList(medList);

DAO Class :

DAO Class to retrive mediums in data base.
public static List getMediums(){
        PreparedStatement pStmt = null;
     Connection conn = null;
     boolean success = false;
     ResultSet rs = null;
     List medList = new ArrayList();
    
     try{
         conn = getConnection();
         
         String sql = " select * from MEDIUM ";
         pStmt = conn.prepareStatement(sql);
                  
         rs = pStmt.executeQuery();
         while(rs.next()){
             Medium med = new Medium();
             med.setMedId(rs.getInt("MED_ID"));
             med.setMedName(rs.getString("MEDIUM_NAME"));
             medList.add(med);
             }
        
        
        
     }catch(Exception e){
         e.printStackTrace();
         
     }finally{
         closeConnectionProp(conn,pStmt,rs);
     }
        
     return medList;
    
    }

JSP Struts:

<html:select name="MediumForm" property="med" >

<bean:define name="MediumForm" property="medList" id="mlist" />
<html:options collection="mlist" property="id" labelProperty="medName" />
</html:select>



In the Action class again :
how to retrive the select value.

String med = form.getMed();

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy