JSP, JavaBenas and Struts problem :(

Hi All,

Well i have tried several approachs...but I am not able to get properties of a bean object on a jsp page.

The situation is, I am creating a new java bean instances for every row in the result set. I am storing those bean objects into an ArrayList. Then I am setting that arraylist object into my session scope in my action servlet.

The problem is, I can see the arraylist in the debugger, it has all the properties loaded init. But, I was not able to iterate and print those values on my jsp page. Here is my code for my ActionServlet, JavaNean and JSP page...

detailSearchAction.java- an ActionServlet

***************************************************************************

public class detailSearchAction extends Action {

private String TARGETPAGE = null;

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

detailSearchActionForm dsa = (detailSearchActionForm) form;

ArrayList resultObjects = new PerformSearch().primarySearchUsecaseOne();

HttpSession session = request.getSession();

if(dsa.getName() != null && dsa.getName() != ""){

TARGETPAGE = "success";

session.setAttribute("INITIAL_RESULTS",resultObjects);

//request.setAttribute("SEARCH_FORM",dsa);

}else{

TARGETPAGE = "failure";

}

return mapping.findForward(TARGETPAGE);

}

}

initialSearch.java - JavaBean

***************************************************************************

public class initialSearch extends Object implements Serializable {

public static final String HOST_ID = "hostid";

public static final String PLANT_GENUS = "plantgenus";

private String hostid;

private String plantgenus;

private PropertyChangeSupport propertySupport;

public initialSearch() {

propertySupport = new PropertyChangeSupport(this);

}

public String getHostId() {

return hostid;

}

public void setHostId(String value) {

String oldValue = hostid;

hostid = value;

propertySupport.firePropertyChange(HOST_ID, oldValue, hostid);

}

public String getPlantGenus(){

return plantgenus;

}

public void setPlantGenus(String value){

String oldValue = plantgenus;

plantgenus = value;

propertySupport.firePropertyChange(PLANT_GENUS, oldValue, plantgenus);

}

public void addPropertyChangeListener(PropertyChangeListener listener) {

propertySupport.addPropertyChangeListener(listener);

}

public void removePropertyChangeListener(PropertyChangeListener listener) {

propertySupport.removePropertyChangeListener(listener);

}

}

detailResults.jsp- JSP page for Printing the results.

***************************************************************************

<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>

<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>

<%@taglib uri="http://displaytag.sf.net" prefix="dispaly"%>

<!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=UTF-8">

<title>Success!</title>

</head>

<body>

<h1>The search results are as under.</h1>

The search result string is: ${INITIAL_RESULTS[1]}

<c:set var="initialsearch" value="${INITIAL_RESULTS}"/>

<c:set var="size" value="${fn:length(initialsearch)}"/>

<c:forEach begin="0" end="${size-1}" var="count">

<jsp:useBean class="resultBeans.initialSearch" id="initS">

<jsp:getProperty name="initS" property="hostid"/>

</jsp:useBean>

</c:forEach>

Modify your search <a href="indexLeafMiners.jsp">here</a>!

</body>

</html>

Message was edited by:

jini4java

[4237 byte] By [jini4javaa] at [2007-11-27 9:00:22]
# 1

1) In your action form detailSearchActionForm, add the following lines

private ArrayList dsl= new ArrayList();

public ArrayList getDsl() {

return dsl;

}

public void setDsl(ArrayList dsl) {

this.dsl= dsl;

}

2) in you execute method of the action class change the following line from

session.setAttribute("INITIAL_RESULTS",resultObjects);

to

dsa.setDsl(resultObjects);

3) in your jsp, try the following code

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

<logic:iterate id="ds" name="detailSearchActionForm" property="dsl"> <bean:write name="ds" property="hostid" />

.......

</logic:iterate>

PatrickWanga at 2007-7-12 21:28:55 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

One of my compeers has made some changes to my bean class and it worked. Later I found out that I have not followed the standard JavaBeans conventions in method definition.

So, please take care of the rules while implementing a JavaBean any where.

The other noticeable thing about the above problems is that, I thought the <jsp:useBean ?gt; tag and <jsp:getProperty?gt; tags are related. I mean the <jsp:getProperty?gt; tag only can be used with <jsp:useBean?gt; tag.

I was wrong.

Well, the idea is, you need a bean instance (new or existing) to get and to set its properties. So, when you use <jsp:useBean?gt; tag, the container would obviously create or find the required class object by using class or type attribute of tag.

However, in the above problem, I wanted to iterate through the Collection object which contains a whole bunch of reference variables of the same JavaBean objects.

Thus, there is no need to find or create new instances if they are already available within the scope (request/session).

jini4javaa at 2007-7-12 21:28:55 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Thanks a lot Patric...I solved it, as i described with jsp:useBean. Now I will try your method as you suggested with Struts TLDs.Regards.
jini4javaa at 2007-7-12 21:28:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Hi ,please send the code after u made the changes , i want to display the bean property values in jsp in struts.regards giri
giri123a at 2007-7-12 21:28:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hi,

Considering the example I have given above, the modified jsp code is as under.

**************************************************************************

<c:set var="keysSearch" value="${KEYS_RESULTS}"/>

<c:set var="size" value="${fn:length(keysSearch)}"/>

The number of records are: ${size}.

<c:forEach var="item" items="${KEYS_RESULTS}" varStatus = "status">

<%--

Using only EL

=================================================================

item #${status.index} : ${item.hostid} --> ${item.plantgenus}

Using only some of the tags from Struts TLDs. You could avoid the

<c:...> tags, if you prefer to implement Struts TLDs fully.

=================================================================

Host id: <bean:write name="item" property="hostid"/> -

Plant genus: <bean:write name="item" property="plantgenus"/>

Using tags from only JSTL TLDs.

=================================================================

No. [${status.index}] : <jsp:getProperty name="item" property="hostid"/>

--> <jsp:getProperty name="item" property="plantgenus"/>

--%>

No. [${status.index}] : <jsp:getProperty name="item" property="hostid"/>

--> <jsp:getProperty name="item" property="plantgenus"/>

</c:forEach>

****************************************************************************

Remember, the jsp bean objects are in the ArrayList.

Regards.

jini4javaa at 2007-7-12 21:28:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...