Retrive data from table by select tag for dropdownlist using hibernate and struts2 integration in eclipse and oracle

Create Database Table named EntranceAction and insert data using insert sql command.


Don't forget to commit after insert data into table

Project View After Completion
Hibernate and Struts2 Setting
right click over project on Project Explorer and goto properties->Java Build Path->Libraries
Add Required Hibernate, struts jars, JRE system Library, Apache Tomcat server by click on Add Library and Add External Jar ojdbc14.jar by click on Add External Jars.

Properties->Web Deployment Assembly->Add->Java Build Path Entries->next
Select HibernateJars and StrutsJars and click finish


ddlAction.java
This is a POJO class and it is working as Persistent class for Hibernate and action class for Struts.
 package org.ddl;  
 import com.opensymphony.xwork2.ActionSupport;  
 import java.util.List;  
 import org.ea.EntranceAction;  
 import org.hibernate.Query;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.Transaction;  
 import org.hibernate.cfg.Configuration;  
 
 public class ddlAction extends ActionSupport  
 {  
 private List<EntranceAction> examsid;  
   public List<EntranceAction> getExamsid() {  
     return examsid;  
   }  
   public void setExamsid(List<EntranceAction> examsid) {  
     this.examsid = examsid;  
   }  
   @SuppressWarnings("deprecation")  
      public String execute() throws Exception  
   {  
          Session session = null;  
            SessionFactory factory= new Configuration().configure().buildSessionFactory();  
             session = factory.openSession();  
       Transaction tc=session.beginTransaction();  
       Query query=session.createQuery("select examid from EntranceAction");  
       examsid=query.list();  
       
       tc.commit();  
       session.close();  
       return "examsid";  
     }  
   }  

EntranceAction.java
 package org.ea;  
 public class EntranceAction   
 {    
 private String examid;  
 public String getExamid() {  
      return examid;  
 }  
 public void setExamid(String examid)  
 {  
 this.examid=examid;  
 }  
 }  

login.jsp
This is page for user's view and here we using struts tags and on submit action is ddl.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
 <%@ taglib prefix="s" uri="/struts-tags" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title></title>  
 </head>  
 <body>  
 <h1></h1>  
 <s:form action="ddl">  
 <s:submit value="GO"/>  
 <%-- <a href="ddl.action">register</a>--%>  
 </s:form>  
 </body>  
 </html>  

struts.xml
This file contains information about which action class to be invoked. here we are invoking ddlAction class using action name ddl present in login.jsp's page form tag on submit button click.
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE struts PUBLIC  
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
   "http://struts.apache.org/dtds/struts-2.0.dtd">  
   <struts>  
   <package name="log" namespace="/" extends="struts-default">  
   <action name="ddl" class="org.ddl.ddlAction" method="execute">  
   <result name="examsid">/ddl.jsp</result>  
   <result name="error">/login.jsp</result>  
   </action>  
   </package>  
   </struts>  

web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  <display-name>ProjDropdownlist</display-name>  
  <welcome-file-list>  
     <welcome-file>login.jsp</welcome-file>  
  </welcome-file-list>  
  <filter>  
    <filter-name>struts2</filter-name>  
    <filter-class>  
      org.apache.struts2.dispatcher.FilterDispatcher  
    </filter-class>  
   </filter>  
   <filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
   </filter-mapping>  
 </web-app>  

ddl.jsp
This is page for user's view and here we using struts tags for select tag showing data in the form of dropdownlist retrieve from database table EntranceAction.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
   <%@ taglib prefix="s" uri="/struts-tags" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title></title>  
 </head>  
 <body>  
   
 <s:select name="courseinterested" label="Course Interested" list="examsid"/>  
 
 </s:form>
 </body>  
 </html>  

hibernate.cfg.xml
This is a file that contains information about Oracle database(or any other database you want to use) Driver and connection information and mapping class information. 
In place of Annotations we are using .hbm file
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-configuration PUBLIC  
 "-//Hibernate/Hibernate Configuration DTD//EN"  
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
 <hibernate-configuration>  
 <session-factory>  
   <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>  
   <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
   <property name="hibernate.connection.username">hr</property>  
   <property name="hibernate.connection.password">hr</property>  
   <property name="hibernate.connection.pool_size">10</property>  
   <property name="show_sql">true</property>  
   <property name="dialect">org.hibernate.dialect.OracleDialect</property>  
   <property name="hibernate.hbm2ddl.auto">update</property>  
   <mapping resource="hibernate.hbm.xml"/>  
     
 </session-factory>  
 </hibernate-configuration>  

hibernate.hbm.xml
hibernate mapping file which we are using to relate our database table to project's object.
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
 <hibernate-mapping>  
  <class name="org.ea.EntranceAction" table="EntranceAction">  
    <id name="examid" column="examsid">  
      <generator class="assigned"/>  
      </id>  
    </class>  
 </hibernate-mapping>  

Output:



No comments: