Search code examples
javajspjavabeans

Transferring collection object from one jsp to another


I get the data from the database and store it in some collection object in one jsp(say One.jsp). I want this collection object to be available in another jsp(say Two.jsp). It might contain large data. So, I have used java bean for that in the following way.

public class BeanClass implements Serializable{

    private  HashMap<String,List<String>> myMap;
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public HashMap<String,List<String>> getMyMap() {
        return myMap;
    }

    public void setData(HashMap<String,List<String>> myMap) {
        this.myMap = myMap;
    }
}

I am setting bean in One.jsp in the following way.

<jsp:useBean id="dataObject" class="com.mypack.BeanClass" scope="session"/>
<jsp:setProperty name="dataObject" property="myMap"/>
<%
    dataObject.setData(myMapObj);
 %>

But I get the following error:

Can't find a method to write property 'dataMap' of type 'java.util.HashMap' in a bean of type 'com.mypack.BeanClass'

Somewhere I found stating that we can't do the set for other things unless it is a string. Is there anyway that I can workaround it to make it work even for collection objects? So that I don't need to rollback lot of things that I have done. If not can someone please suggest how to transfer the collection object from one jsp to another? Example with snippet is appreciated.

Or is it that I am missing some extra configuration to make beans work?

Note: I might have used redirect() method, but here the navigation from One.jsp to Two.jsp is done by clicking on hyperlink(href)

EDIT:

Thanks to Alfreema. His solution worked after I had changed to setMyMap, but in Two.jsp I am facing issue. I try to display only some part of map like below. But it displays everything that is there in Map as if it shows when we use out.println(myMap); and it displays table at the very end of the page after printing the content of whole map.

Two.jsp:

<jsp:include page="header.jsp"/>
<jsp:useBean id="dataObject" class="com.mypack.BeanClass" scope="session"/>
<jsp:getProperty name="dataObject" property="myMap"/>

<%! HashMap<String,List<String>> gottenMap = null; %>
<%
gottenMap = dataObject.getMyMap();
%>
<table>
<tbody>

<%
String selectedPart = request.getParameter("part");
List<String> list = gottenMap.get(selectedPart);
for(int i=0; i<list.size(); i++)
{
%>
<tr>
        <td colspan='2'><strong>
        <%= list.get(i) %>
        </strong></td>
</tr>

<%
}
%>

</tbody>
</table>

I am confused why it does like that without coding for printing the whole collection.


Solution

  • Change this:

    <jsp:useBean id="dataObject" class="com.mypack.BeanClass" scope="session"/>
    <jsp:setProperty name="dataObject" property="myMap"/>
    <%
        dataObject.setData(myMapObj);
    %>
    

    To this:

    <jsp:useBean id="dataObject" class="com.mypack.BeanClass" scope="session"/>
    <jsp:setProperty name="dataObject" property="data" value="${myMapObj}"/>
    

    Your setter is called "setData(...)", not "setMyMap(...)". JSP is going to see property="data" and call setData(...).

    So that will fix you up, but you didn't show us where you are setting myMapObj. We have to assume that you are setting that up correctly elsewhere and have made it available to the tag via a pageContext.setAttribute("myMapObj", myMapObj); call.

    Side note: I recommend you rename the setData(..) method to setMyMap(...) to make it consistent with the getter getMyMap(...). If you do that, then you will need to change the property="..." back to "myMap", just like you had it.