facebook

Struts and Hibernate Listbox

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #236605 Reply

    Hi all!

    I would like to implement a listbox with some values of an Oracle Database (Table Defektdef, mapped with Hibernate2).

    However it is possible to print some rows of the table with <bean:write>, but it is not possible, to transfer the rows into my listbox.

    My idea looks like this:

    
    <%@ page language="java" pageEncoding="UTF-8"%>
    
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
    
    <%@ page import="com.roche.hibernate.DefektdefService" %>
    <%@ page import="java.util.List" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html:html locale="true">
      <head>
        <html:base />
        
        <title>reportDef.jsp</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
      </head>
      
      <body>
    <P><STRONG>Materialinfo:</STRONG></P>Materialnr: <bean:write name="compData" property="matnr" scope="session" />
        <br>
        Seriennr: <bean:write name="compData" property="seriennr" scope="session" />
        <br>
        Revnr: <bean:write name="compData" property="revnr" scope="session" />
        <br>
    
    <HR>
    
    <html:form action="fetchDetails">
    <%
          List defektdefList = DefektdefService.getInstance().getDefektdefList();
          [b]request.setAttribute("Defektdef", defektdefList);[/b]
    
      %>    
          <p>Liste an moeglichen Defekten in <code>Defektdef</code> .</p>
       <table border=1>
       [b]<logic:iterate id="element" name="Defektdef" scope="request" type="com.roche.hibernate.Defektdef" >[/b]
       <tr>
          [b]<td><bean:write name="element" property="defektdefId" /></td> 
          <td><bean:write name="element" property="bezeichnung" /></td>[/b]    
       </tr>
       </logic:iterate>
       </table>
    
    
       [b]<bean:define id="Defektdef" name="Defektdef" scope="request" />
        <html:select name="Defektdef" property="defektdefId">
            <html:optionsCollection name="Defektdef" label="defektdefId" value="bezeichnung" />
        </html:select>[/b]
    
    </html:form>
    <P></P></body>
    </html:html>
    

    The second way of representing my table (html:select) raises following exception:

    javax.servlet.ServletException: No getter method available for property defektdefId for bean under name Defektdef

    #236665 Reply

    Riyad Kalla
    Member

    The second way of representing my table (html:select) raises following exception:

    This is the right way to do it, however your Defektdef form-bean needs to have property called getDefektdefId and setDefektdefId according to your html:select element the way you wrote it above, whichi s likely why you are getting the exception.

    Does it?

    #236733 Reply

    @support-rkalla wrote:

    Does it?

    Yes – the problem was, that this form-bean wasn’t registered in struts-config.

    But another problem is, that the value und the key should be packed into a LabelValueBean. The data for the select box comes from hibernate ==> a function returning a list (==> tutorial)

    How is it possible to cast/convert this list into the LabelValueBean, or are there any other ways to do this?

    (I tried something like “new LabelValueBean(list.get(index).toString),(…..)), but this construct returns something like com.XXX.xxxx@3212e which is not the string representation I’m used to read – looks like an adress or something other 🙂 )

    Summary of my Problem: At my Form bean, I got the List containing some rows of a table – how is it possible to “transfer” them to my html:select box in my struts page?

    thx for any help and sorry for such a newbie question

    Martin

    #236757 Reply

    Solution!

    After reading page 109 of the Hibernate Documentation (Working with objects, Queries that return tuples) I get this listbox to work.

    The solution looks like this:

    hibernate ServiceBean

    
    public Iterator getDefektdefList()
        {
            /*
             * Use the ConnectionFactory to retrieve an open
             * Hibernate Session.
             * 
             */
            Session session = null;
    
            try
            {
                session = SessionFactory.currentSession();
                /*
                * Build HQL (Hibernate Query Language) query to retrieve a list
                * of all the items currently stored by Hibernate.
                 */
                Query query =
                    session.createQuery(
                        "select Defektdef.defektdefId, Defektdef.bezeichnung from com.roche.hibernate.Defektdef Defektdef");
                return query.list().iterator();
                
    
    
    
            }
            catch (HibernateException e)
            {
                System.err.println("Hibernate Exception " + e.getMessage());
                throw new RuntimeException(e);
            }
            /*
             * Regardless of whether the above processing resulted in an Exception
             * or proceeded normally, we want to close the Hibernate session.  When
             * closing the session, we must allow for the possibility of a Hibernate
             * Exception.
             * 
             */
            finally
            {
                if (session != null)
                {
                    try
                    {
                        session.close();
                    }
                    catch (HibernateException e)
                    {
                        System.err.println("Hibernate Exception" + e.getMessage());
                        throw new RuntimeException(e);
                    }
    
                }
            }
            
    
            
        }
    
     
    

    My StrutsAction contains following:

    
     Collection defektdef =  barcodeForm.getDefektdef();
     request.setAttribute("defektdef", defektdef);
    
    

    and the form….

    
     public Collection getDefektdef() {
            
             Collection defektdef = new ArrayList();
             Iterator defektDefList = DefektdefService.getInstance().getDefektdefList();
             
             
             while(defektDefList.hasNext()) {
                 Object[] tuple = (Object[]) defektDefList.next();
                 String id = tuple[0].toString();
                 String bezeichnung = tuple[1].toString();
                 defektdef.add(new LabelValueBean( bezeichnung, id));
             }
    
             return defektdef;
        }
    

    Finally the generation of my Listbox in jsp:

    
     <html:select name="barcodeForm" property="defektdef">
    <html:options collection="defektdef" property="value" labelProperty="label" />
    </html:select>
    

    That it is 😉
    So I hope I could help somebody with my posting

    #236760 Reply

    Riyad Kalla
    Member

    We very much appreciate you posting back your solution for others, no doubt many users have been in this place before and needed help and now they have it.

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Struts and Hibernate Listbox

You must be logged in to post in the forum log in