Search code examples
javahibernatejspstruts2hql

Retrieve value from database in drop-down(<s:select>) using Struts 2 and Hibernate


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

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 drop down. Also suggest that how the value in drop down will be displayed selected in 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/