Search code examples
hibernatestruts2displaytag

Unable to implemet Display tag with Struts


I am using Hibernate and struts framework in order to develop a web based application. i want to display some information about an object using display tag, this object is mapped and i am able to pull it from a database .. i am also able to pull a collection/list of this object from the database , i set the collection and give it to the display tag, but after executing .. an exception is thrown telling me that the properties in the display:column are unknown , i checked the java beans and everything is right ..

here is the code:

    public class ReadToolsList extends ActionSupport implements SessionAware {
    private List TestingToolsList = null;
    Map Session;
    ReadToolsListLogic RDT = new ReadToolsListLogic();

    public String toolsList() {

        setTestingToolsList(RDT.readToolsLogic());

        return Action.SUCCESS;
    }

    public List getTestingToolsList() {
        return TestingToolsList;
    }

    public void setTestingToolsList(List testingToolsList) {
        TestingToolsList = testingToolsList;
    }   

    @Override
    public void setSession(Map arg0) {
        // TODO Auto-generated method stub

    }

    public Map getSession() {
        return Session;
    }

}

here is the code within the jsp page :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>      

<!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>Test Tools List</title>
</head>

<body >
<h1>In side Tools List</h1> <br> <br>

<display:table id="data" name="TestingToolsList" requestURI="/ToolList.action" pagesize="3" export="true" >
        <display:column property="Tool_Name" title="Name" sortable="true"/> 
</display:table>

</body>
</html>

here is the code within struts.xml :

<struts>
    <package name="module-Authentication" extends="struts-default">

        <action name="authentication" class="TTI.Presenstation.AuthenticationAction"
            method="Authentication">
            <result name="admin" type="chain">ToolList</result>
            <result name="user">/View/success.jsp</result>
            <result name="error">/View/error.jsp</result>
        </action>

    </package>

    <package name="Admin-Pages" extends="struts-default">

        <action name="ToolList" class="TTI.Presenstation.ReadToolsList"
            method="toolsList">
            <result name="success">/View/Admin_Pages/Tools_List.jsp</result>
            <result name="error">/View/error.jsp</result>
        </action>
    </package>
</struts>

Solution

  • A getter must be accessed with a starting lowercase, so getTestingToolsList = testingToolsList

    <display:table id="data" 
                 name="testingToolsList"
           requestURI="/ToolList.action" 
             pagesize="3" 
               export="true" 
                  uid="data" > <!-- also add the "uid" attribute -->
    

    The same for Tool_name that should be tool_name.

    <display:column property="tool_Name" title="Name" sortable="true"/> 
    

    That said, it should also be camelCase: toolName. We recently discovered it is not only a good practice but it is mandatory in some cases.

    <display:column property="toolName" title="Name" sortable="true"/> 
    

    If you need to access the values from within the column element, you can use #attr to retrieve them from the specific scope:

    <display:column title="Name" sortable="true"> 
        <s:property value="#attr.data.toolName" />
    </display:column>