The base problem is that I am getting a
ClassCastException when I attempt to iterate through an object on my JSP page.
I am using hibernate and struts to access a database and preset the information to the user via jsp.
I have successfully retrieved information from a single table and displayed it on screen using a similar method as I will present below. The error occurs when I try and do a JOIN. The
ClassCastException error is triggered when I attempt to access the code between the comments.
//begin error
<logic:iterate id="element" name="pressrelease" scope="request" type="com.digitalagua.hibernate.PressreleaseEditDisplay">
//end error
<bean:write name="element" property="id" /><br />
<bean:write name="element" property="articleDate" format="MM-dd-yyyy" /><br />
<bean:write name="element" property="status" /><br />
<bean:write name="element" property="title" filter="false" /><br />
<bean:write name="element" property="category" /><br />
</logic:iterate>
The error is happening when I assign the
PressreleaseEditDisplay type to the Object. I am assuming the object isn?t of that type, which makes me confused on why it would work with one Object and not another.
The frustrating part is if I use the following I can loop through the object, and the data result is exactly how I want it. Unfortunately it doesn?t do me much practical good.
<logic:iterate id="element" name="pressrelease" scope="request" type="java.lang.Object" >
<logic:iterate id="innerelement" name="element" >
<bean:write name="innerelement" /><br />
</logic:iterate>
</logic:iterate>
Can anybody see what my problem with this is?
I have included a bit of code below and I am happy to include more.
Thanks in advance.
James
//HQL Query
session.createQuery("
select
pr.id as id,
pr.articleDate as articleDate,
pr.title as title,
cat.title as category,
stat.title as status
from
com.digitalagua.hibernate.Pressrelease as pr,
com.digitalagua.hibernate.PressreleaseCategory as cat,
com.digitalagua.hibernate.LookupStatus as stat
where
pr.title is not null and
pr.categoryId = cat.id and
pr.statusId = stat.id
order
by pr.createdDate desc
");
// test.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ 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.digitalagua.hibernate.PressreleaseService" %>
<%@ page import="java.util.List" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>List Press Releases</title>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
<%
/*
* This code will generate a list of objects from the
* database and place a reference to this list in the
* request object.
*
*/
List prList = PressreleaseService.getInstance().getPressreleaseEditDisplay();
request.setAttribute("pressrelease", prList);
%>
</head>
<body>
// This is where the error occurs
<logic:iterate id="element" name="pressrelease" scope="request" type="com.digitalagua.hibernate.PressreleaseEditDisplay" >
<bean:write name="element" property="id" /><br />
<bean:write name="element" property="articleDate" format="MM-dd-yyyy" /><br />
<bean:write name="element" property="status" /><br />
<bean:write name="element" property="title" filter="false" /><br />
<bean:write name="element" property="category" /><br />
</logic:iterate>
</body>
</html:html>
// PressreleaseEditDisplay
package com.digitalagua.hibernate;
import java.util.Date;
/**
* PressreleaseEditDisplay
*/
public class PressreleaseEditDisplay implements java.io.Serializable {
// Fields
private Integer id;
private Date articleDate;
private String title;
private String category;
private String status;
// Constructors
/** default constructor */
public PressreleaseEditDisplay() {
}
/** full constructor */
public PressreleaseEditDisplay( Date articleDate, String title, String category, String status) {
this.articleDate = articleDate;
this.title = title;
this.category = category;
this.status = status;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getArticleDate() {
return this.articleDate;
}
public void setArticleDate(Date articleDate) {
this.articleDate = articleDate;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {
return this.category;
}
public void setCategory(String category) {
this.category = category;
}
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
}