tags:

views:

365

answers:

2

How can we access the bean attribute in JSP?
I tried

<core:forEach var="header" items="${command.headerList}" >
<td><core:out value="${header.columnName}"/></td>
</core:forEach>

where headerList is the list of myBean which is having the attribute columnName [ getter / setter are defined in the class ]

Expected: it should print the value in columnName
Actual: it is not printing anything at all

also how can i access the nth element in the List? i tried

${command.headerList[i]}

Output: nothing displayed.

Edit:#

When i write

${command.headerList}

it displays

com.bean.MyBean@14ecb90, com.bean.MyBean@169b35, com.bean.MyBean@27d572

This is just to let you know that the list is having something

and when i write

${header}

inside the forEach loop it displays:

javax.servlet.jsp.el.ImplicitObjectELResolver$ImplicitObjects$7@d9d714

That means even header is having value, then why i am unable to print values using

${header.columnName}
+1  A: 

How can we access the bean attribute in JSP? I tried

<core:forEach var="header" items="${command.headerList}" >
<td><core:out value="${header.columnName}"/></td>
</core:forEach>

where headerList is the list of myBean which is having the attribute columnName [ getter / setter are defined in the class ]

Looks fine. What happens instead?

also how can i access the nth element in the List? i tried

${command.headerList[i]}

Looks fine as long as i is initialized and in scope. What happens instead?

When i write

${command.headerList}

it displays

com.bean.MyBean@14ecb90, com.bean.MyBean@169b35, com.bean.MyBean@27d572

It should include [ in the front and ] in the tail, but for the remnant it looks fine as long as you didn't override Object#toString() in MyBean class. What had you expected?

and when i write

${header}

inside the forEach loop it displays:

javax.servlet.jsp.el.ImplicitObjectELResolver$ImplicitObjects$7@d9d714

Looks fine. What had you expected?

Summarized: I don't understand your problem. Please elaborate more. Post an SSCCE. Tell about the expected input/output. Tell about the actual input/output.

Edit you've edited your question. Well, here are the updated answers.

${header.columnName}

Expected: it should print the value in columnName
Actual: it is not printing anything at all

Then it actually didn't contain a value. To test it properly, override Object#toString() something like as follows:

public String toString() {
    return "header[" + columnName + "]";
}

And test it using ${command.headerList}. You can also add some System.out or Logger statement to the getter method to see if it is actually invoked and actually returned a value. Or if you understand how to use it, just run a code debugger. Every decent IDE ships with one.

Next:

${command.headerList[i]}

Output: nothing displayed.

Then there's no means of a valid i. To test it properly, do the following to get the first item:

${command.headerList[0]}
BalusC
+2  A: 

header is an implicit object in JSPs that maps to request (HTTP) headers. See the JSP spec for a full list of implicit objects.

Try referencing the bean with respect to the scope you have stored it in (e.g. requestScope.header). Preferably, change the name to something else.

McDowell
+1: that caused a d'oh!
BalusC
Oh My God, now what should say. :) i changed `header` to `header1` and it worked. +10 if i could :)
Rakesh Juyal