Struts Listing Problem
I'm trying to get a listing of data from a PostgreSQL database table to view on a Java Server Page. I'm receiving the following error, javax.servlet.ServletException: Cannot find bean listHqsunitdata in scope request
Below you will find what I currently have coded to receive that result:
JSP Page:
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<html:html locale="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><bean:message key="title.unitData_unitData"/></title>
<script src="../common/fireSupport.js" type="text/javascript"></script>
<link rel="STYLESHEET" type="text/css" href="../common/style.css">
<script language="JavaScript" type="text/JavaScript">
<!-- cutfor brevity -->
</script>
</head>
<body onload="MM_preloadImages('../images/new_hit.gif')">
<%@ include file="header_admin.jsp" %>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;">
<tr>
<td width="50%">
<table width="500" border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;">
<tr>
<td id="headerTitle" nowrap="nowrap"><bean:message key="header.label.udhqsUnitData"/></td>
<td><img src="../images/img_headerLean.gif" width="38" height="26" alt="" /></td>
<td width="99%"> </td>
</tr>
</table>
<td width="50%" style="text-align: right;">xxx</td>
</table>
<div style="font-size: 10px; font-weight:normal; text-align: right; font-family: verdana; margin-right: 10px;">2 Records Found</div>
<table id="displayTable" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th><bean:message key="label.disEntity"/></th>
<th><bean:message key="label.uic"/></th>
<th><bean:message key="label.name"/></th>
<th><bean:message key="label.radio"/></th>
<th><bean:message key="label.tfAddress"/></th>
<th><bean:message key="label.device"/></th>
<th><bean:message key="label.alias"/></th>
<th><bean:message key="label.commo"/></th>
<th><bean:message key="label.ipAddress"/></th>
<th><bean:message key="label.urn"/></th>
</tr>
<logic:iterate name="listHqsunitdata"
id="hqsunitTO"
scope="request"
type="scenario.to.HqsunitTO">
<tr>
<td class='displayTD1'><bean:write name="hqsunitTO" property="disEntity" /></td>
<td class='displayTD1'><bean:write name="hqsunitTO" property="uic"/></td>
<td class='displayTD1'><bean:write name="hqsunitTO" property="name" /></td>
<td class='displayTD1'><bean:write name="hqsunitTO" property="radio" /></td>
<td class='displayTD1'><bean:write name="hqsunitTO" property="tfAddress" /></td>
<td class='displayTD1'><bean:write name="hqsunitTO" property="device" /></td>
<td class='displayTD1'><bean:write name="hqsunitTO" property="alias" /></td>
<td class='displayTD1'><bean:write name="hqsunitTO" property="commo" /></td>
<td class='displayTD1'><bean:write name="hqsunitTO" property="ipAddress" /></td>
<td class='displayTD1'><bean:write name="hqsunitTO" property="urn" /></td>
</tr>
</logic:iterate>
</table>
<a href="javascript:showhide('cover_large,create_item')" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('new','','../images/new_hit.gif',1)"><img src="../images/new.gif" alt="Create New" name="new" width="16" height="16" border="0" id="new" /></a> <a href="javascript:showhide('cover_large,deleteWarning')" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('delete','','../images/delete_hit.gif',1)"><img src="../images/delete.gif" alt="Delete" name="delete" width="16" height="16" border="0" id="delete" /></a>
<div id="edit_item" style="position:absolute; width:422px; height:457px; left:300px; top:150px; visibility:hidden; z-index:5;">
<iframe src="hqsunitdata_edit.jsp" name="test" width="422" height="457" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="true" style="background-color: transparent;"></iframe>
</div>
<div id="create_item" style="position:absolute; width:422px; height:457px; left:300px; top:150px; visibility:hidden; z-index:5;">
<iframe src="hqsunitdata_create.jsp" name="test" width="422" height="457" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="true" style="background-color: transparent;"></iframe>
</div>
</body>
</html:html>
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- ============================================ Data Source Configuration -->
<!--
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="org.postgresql.Driver" />
<set-property
property="url"
value="jdbc:postgresql://localhost/mydatabase" />
<set-property
property="username"
value="me" />
<set-property
property="password"
value="test" />
<set-property
property="maxActive"
value="10" />
<set-property
property="maxWait"
value="5000" />
<set-property
property="defaultAutoCommit"
value="false" />
<set-property
property="defaultReadOnly"
value="false" />
<set-property
property="validationQuery"
value="SELECT COUNT(*) FROM market" />
</data-source>
</data-sources>
-->
<!-- ================================================ Form Bean Definitions -->
<form-beans>
<form-bean
name="hqsunitdataForm"
type="scenario.actions.EditHqsunitdataAction"/>
<form-bean name="hqsunitForm" type="scenario.form.HqsunitForm"/>
<form-bean name="appLogin" type="scenario.form.LoginForm"/>
</form-beans>
<!-- ========================================= Global Exception Definitions -->
<global-exceptions>
<!-- sample exception handler
<exception
key="expired.password"
type="app.ExpiredPasswordException"
path="/changePassword.jsp"/>
end sample -->
</global-exceptions>
<!-- =========================================== Global Forward Definitions -->
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>
<!-- =========================================== Action Mapping Definitions -->
<action-mappings>
<action path="/Welcome" name="appLogin" forward="/pages/Welcome.jsp"/>
<action path="/listHqsunitdata" forward="/pages/hqsunitdata.jsp"/>
<action path="/getHqsunit"
type="scenario.action.GetHqsunitAction"
name="hqsunitForm"
scope="request"
validate="false">
<forward name="success" path="/pages/displayUpdateHqsunitForm.jsp"/>
</action>
</action-mappings>
<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="MessageResources" />
<!-- Cutoutfor brevity -->
</struts-config>
[13192 byte] By [
tclyndella] at [2007-10-2 15:01:28]

It is obviously caused by this line of code.
<logic:iterate name="listHqsunitdata"
id="hqsunitTO"
scope="request"
type="scenario.to.HqsunitTO">
effectively it calls request.getAttribute("listHqsunitdata") to retrieve the list of objects to iterate over.
It can't find this object, so it throws an error message telling you that.
The question is - how should this list get into scope? How do you make it available to the jsp? Somewhere you should have a request.setAttribute("listHqsunitdata",...) call. Where is that, and why hasn't it been called?
I have it listed now, but I still receive the same result:
Here is the java class (ListHqsunitAction.java) the request.setAttribute is set. What have I missed? I'm new to this and right smack in the middle of it.
ListHqsunitAction.java
package scenario.action;
import scenario.dao.HqsunitDAO;
//import scenario.dao.DAOException;
import scenario.dao.DAOFactory;
import scenario.form.HqsunitForm;
import scenario.to.HqsunitTO;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
public class ListHqsunitAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionMessages actionMessages = new ActionMessages();
HqsunitTO hqsunit = null;
//String disentity = request.getParameter("disentity");
HqsunitDAO hqsunitDAO = DAOFactory.getInstance().getHqsunitDAO();
try {
int disentity = Integer.parseInt(request.getRemoteUser());
//WLM 20060308 changed 1 line below : uncommented 1 line
Collection collection = hqsunitDAO.findAll(disentity);
request.setAttribute("listHqsunitdata", collection);
return (mapping.findForward("success"));
}
catch (Exception ex) {
actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("failed.get"));
saveMessages(request, actionMessages);
return (mapping.findForward("failed"));
}
}
}
Thanks a million for your help.
Maybe the call to hqsunitDAO.findAll(disentity) returns null?
You have me jumping around grabbing bits and pieces of the application, and that's good. I'll know what I need to present the next time I'm stuck on stupid. I don't believe that hqsunitDAO.findAll(DisEntity) = null. However, I have been wrong, so here are the other pieces of code that I fail to present earlier:
In the HqsunitDAO.java:
package scenario.dao;
import scenario.to.HqsunitTO;
import java.util.ArrayList;
import java.util.Collection;
public interface HqsunitDAO {
public void createHqsunit(HqsunitTO hqsunit) throws DAOException;
public void updateHqsunit(HqsunitTO hqsunit) throws DAOException;
public HqsunitTO getHqsunit(int disentity) throws DAOException;
public void deleteHqsunit(int disentity) throws DAOException;
public Collection findAll(int disentity) throws DAOException;
}
In the HqsunitPostgresImpl.java:
package scenario.dao;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import scenario.dao.DAOException;
import scenario.dao.DBUtil;
import scenario.to.HqsunitTO;
public class HqsunitDAOPostgreImpl extends DAOBase
implements HqsunitDAO {
/* code omitted to shorten length */
private static final String LIST_HQSUNIT_SQL =
"SELECT * from rtmuser.s_hq_unit WHERE disentity = ?";
public void createHqsunit(HqsunitTO hqsunit) throws DAOException {
Connection connection = null;
PreparedStatement pStatement = null;
try {
connection = getConnection();
//Prepare a statement to insert a record
pStatement = connection.prepareStatement(CREATE_HQSUNIT_SQL);
pStatement.setInt(1, hqsunit.getDisentity());
pStatement.setString(2, hqsunit.getUic());
pStatement.setString(3, hqsunit.getName());
pStatement.setInt(4, hqsunit.getRadio());
pStatement.setString(5, hqsunit.getAlias());
pStatement.setString(6, hqsunit.getTfAddress());
pStatement.setString(7, hqsunit.getDevice());
pStatement.setString(8, hqsunit.getHqsObserver());
pStatement.setInt(9, hqsunit.getGridzone());
pStatement.setInt(10, hqsunit.getMmEast());
pStatement.setInt(11, hqsunit.getMmNorth());
pStatement.setString(12, hqsunit.getIType());
pStatement.setString(13, hqsunit.getCommo());
pStatement.setString(14, hqsunit.getIpAddress());
pStatement.setString(15, hqsunit.getUrn());
pStatement.executeUpdate();
pStatement.close();
}
catch (SQLException ex) {
throw new DAOException();
}
finally {
try {
connection.close();
}
catch (SQLException ex) {
throw new DAOException();
}
}
}
public void updateHqsunit(HqsunitTO hqsunit) throws DAOException {
Connection connection = null;
PreparedStatement pStatement = null;
try {
connection = getConnection();
// Prepare a statement to insert a record
pStatement = connection.prepareStatement(UPDATE_HQSUNIT_SQL);
pStatement.setInt(1, hqsunit.getDisentity());
pStatement.setString(2, hqsunit.getUic());
pStatement.setString(3, hqsunit.getName());
pStatement.setInt(4, hqsunit.getRadio());
pStatement.setString(5, hqsunit.getAlias());
pStatement.setString(6, hqsunit.getTfAddress());
pStatement.setString(7, hqsunit.getDevice());
pStatement.setString(8, hqsunit.getHqsObserver());
pStatement.setInt(9, hqsunit.getGridzone());
pStatement.setInt(10, hqsunit.getMmEast());
pStatement.setInt(11, hqsunit.getMmNorth());
pStatement.setString(12, hqsunit.getIType());
pStatement.setString(13, hqsunit.getCommo());
pStatement.setString(14, hqsunit.getIpAddress());
pStatement.setString(15, hqsunit.getUrn());
pStatement.executeUpdate();
pStatement.close();
}
catch (SQLException e) {
throw new DAOException();
}
finally {
try {
connection.close();
}
catch (SQLException ex) {
}
}
}
public HqsunitTO getHqsunit(int disentity) throws DAOException {
Connection connection = null;
PreparedStatement pStatement = null;
ResultSet rs = null;
HqsunitTO hqsunit = new HqsunitTO();
try {
connection = getConnection();
// Prepare a statement to insert a record
pStatement = connection.prepareStatement(GET_HQSUNIT_SQL);
pStatement.setInt(1,disentity);
rs = pStatement.executeQuery();
if (rs.next()) {
hqsunit.setDisentity(rs.getInt("disentity"));
hqsunit.setUic(rs.getString("uic"));
hqsunit.setName(rs.getString("name"));
hqsunit.setRadio(rs.getInt("radio"));
hqsunit.setAlias(rs.getString("alias"));
hqsunit.setTfAddress(rs.getString("tfaddr"));
hqsunit.setDevice(rs.getString("device"));
hqsunit.setHqsObserver(rs.getString("hqs_observer"));
hqsunit.setGridzone(rs.getInt("gridzone"));
hqsunit.setMmEast(rs.getInt("mm_east"));
hqsunit.setMmNorth(rs.getInt("mm_north"));
hqsunit.setIType(rs.getString("i_type"));
hqsunit.setCommo(rs.getString("commo"));
hqsunit.setIpAddress(rs.getString("ipaddress"));
hqsunit.setUrn(rs.getString("urn"));
}
rs.close();
pStatement.close();
}
catch (SQLException ex) {
throw new DAOException();
}
finally {
try {
connection.close();
}
catch (SQLException ex) {
}
}
return hqsunit;
}
public void deleteHqsunit(int disentity) throws DAOException {
Connection connection = null;
PreparedStatement pStatement = null;
try {
connection = getConnection();
pStatement = connection.prepareStatement(DELETE_HQSUNIT_SQL);
pStatement.setInt(1, disentity);
pStatement.executeUpdate();
pStatement.close();
}
catch (SQLException e) {
throw new DAOException();
}
finally {
try {
connection.close();
}
catch (SQLException ex) {
}
}
}
public Collection findAll(int disentity) throws DAOException {
Connection connection = null;
PreparedStatement pStatement = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
HqsunitTO hqsunit = new HqsunitTO();
try {
connection = getConnection();
// Prepare a statement to insert a record
pStatement = connection.prepareStatement(LIST_HQSUNIT_SQL);
pStatement.setInt(1,disentity);
rs = pStatement.executeQuery();
while(rs.next()) {
hqsunit.setDisentity(rs.getInt(1));
hqsunit.setUic(rs.getString(2));
hqsunit.setName(rs.getString(3));
hqsunit.setRadio(rs.getInt(4));
hqsunit.setAlias(rs.getString(5));
hqsunit.setTfAddress(rs.getString(6));
hqsunit.setDevice(rs.getString(7));
hqsunit.setHqsObserver(rs.getString(8));
hqsunit.setGridzone(rs.getInt(9));
hqsunit.setMmEast(rs.getInt(10));
hqsunit.setMmNorth(rs.getInt(11));
hqsunit.setIType(rs.getString(12));
hqsunit.setCommo(rs.getString(13));
hqsunit.setIpAddress(rs.getString(14));
hqsunit.setUrn(rs.getString(15));
list.add(hqsunit);
}
return list;
}
catch (SQLException e) {
throw new DAOException();
}
}
}
Agreed, the findAll() method can not return null. If there are no records, it returns an empty list. It was worth checking though.
So that leaves me to believe that the attribute is not being set.
Should have thought of this one before, but which of the actions in your struts-config is being invoked?
I was presuming it is the "/getHqsunit", as that is the one that invokes your action.
Your other action /listHqsunitdata: <action path="/listHqsunitdata" forward="/pages/hqsunitdata.jsp"/> forwards straight to the jsp page. It doesn't run any actions, so nothing gets put into request scope.
Remember request scope attributes only last for one request. As soon as the web page is sent back to the client, it is gone. So if you are counting on it being there from a previous request, you would need to either reload it, or store the value in session.
So, what you are saying is that my action which currently reads:
<action path="/listHqsunitdata" forward="/pages/hqsunitdata.jsp"/>
needs more in to respond correctly.
I don't know what to add at this point. What guidance do you have? I'm looking at the attributes of the action element, now. What should I add?
Thanks!
Warren
The error message is coming up because the request attribute 'listHqsunitdata" is null.
Why is that value null?
either
- it is being set to null
- it is not being set on this request.
We discarded the first option, that leaves the second.
If the url you are invoking is /listHqsunitdata, and the jsp page listed at the top is "/pages/hqsunitdata.jsp" that explains it.
The action directly routes to the JSP. Nowhere along this path is the required request attribute sett.
Guidance at this point?
The attribute needs to be in scope for the page to run successfully.
This means either
1 - invoke an action which puts the variable in request scope (you need to reload it and call request.setAttribute again)
2 - put it into session, so it is available over multiple requests. (but uses more memory)
So, would something like this work for the action-mapping:<action path="/listHqsunitdata" type="scenario.action.ListHqsunitAction" input="/pages/hqsunitdata.jsp"/>Thanks,Warren