javahibernatejspstruts2hql

How to pupulate dropdown with <s:select> tag from database in Struts 2?


I want to use dropdown and getting the value in dropdown from the database, the dropdown should contain a company code for saving the purpose & company description for display purposes.

Below is my code:

Bean Class:

package com.ims.master.company.bean;

public class CompanyBean {
    
        private String id;
        private String cmpCode;
        private String cmpDes;
        private String cmpStatus;
        private String cmpCreated;
        
        public CompanyBean(String cmpCode, String cmpDes) {
            super();
            this.cmpCode = cmpCode;
            this.cmpDes = cmpDes;
        }
        
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getCmpCreated() {
            return cmpCreated;
        }
        public void setCmpCreated(String cmpCreated) {
            this.cmpCreated = cmpCreated;
        }
        public String getCmpCode() {
            return cmpCode;
        }
        public void setCmpCode(String cmpCode) {
            this.cmpCode = cmpCode;
        }
        public String getCmpDes() {
            return cmpDes;
        }
        public void setCmpDes(String cmpDes) {
            this.cmpDes = cmpDes;
        }
        public String getCmpStatus() {
            return cmpStatus;
        }
        public void setCmpStatus(String cmpStatus) {
            this.cmpStatus = cmpStatus;
        }
}

DAO class:

package com.ims.master.company.DAO;

import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.ims.hibernate.HibernateUtil;
import com.ims.master.company.bean.CompanyBean;


public class CompanyDAO {
  
    SessionFactory factory = HibernateUtil.getFactory();
    Session session = factory.openSession();
    ArrayList<CompanyBean> recList = new ArrayList<CompanyBean>();
    
    @SuppressWarnings("unchecked")
    public ArrayList<CompanyBean> retrieveCmpCode()
    {
        System.out.println("=====inside DAO======");
        Query query = session.createQuery("select b.cmpCode,b.cmpDes from CompanyBean b where b.cmpStatus=:val");
        query.setParameter("val", "Y");
        recList = (ArrayList<CompanyBean>) query.list();
        System.out.println("=====value====="+recList);
        return recList;
    }
}

Action Class:

package com.ims.master.masterData;

import java.util.ArrayList;
import com.ims.master.company.DAO.CompanyDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class MasterLookUp extends ActionSupport {
    
    ArrayList companyCode;
    public String getCompany()
    {
        CompanyDAO companyCodeValue = new CompanyDAO();
        companyCode = companyCodeValue.retrieveCmpCode();
        return SUCCESS;
    }
    public ArrayList getCompanyCode() {
        return companyCode;
    }
    public void setCompanyCode(ArrayList companyCode) {
        this.companyCode = companyCode;
    }
    

}

jsp:

<s:select name="companyName" list="companyCode" key="label.companyName" listKey="cmpCode" listValue="cmpDes"/>

Please suggest me, how the value will come in dropdown?

Also suggest that, how the value in dropdown will be displayed selected in the edit part?


Solution

  • you can use below code in your JSP

    <html:select property ="cmpDes">
        <html:optionsCollection name ="cmpDes" /> 
        </html:select>
    

    When above code is added in ur JSP , your dropdown will have the cmp description which is fetched from DB.

    And below is site which has perfect example to learn struts-1 and is also related to your question for getting some ideas to go on. http://www.javabeat.net/struts-html-optionscollection-tag-htmloptionscollection/