I'm running a simple test with the library
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<f:view>
<html>
<body>
<script type="text/javascript">
<json:object>
<json:property name="itemCount" value="$
{ContactsBean.testProp}"/>
</json:object>
</script>
<script type="text/javascript">
<json:object>
<json:property name="itemCount"
value="#{ContactsBean.testProp}"/>
</json:object>
</script>
</body>
</html>
</f:view>
With the managed bean
public class ContactsBean {
public ContactsBean() {
this.setTestProp("testing");
}
private String testProp;
public String getTestProp() {
return this.testProp;
}
public void setTestProp(String testProp) {
this.testProp = testProp;
}
}
Web.xml:
<web-app
version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> ... etc
Which generates the following
<script type="text/javascript">
{"itemCount":""}
</script>
<script type="text/javascript">
{"itemCount":"#{ContactsBean.testProp}"}
</script>
QUESTION:
Why does neither EL syntax (# vs $) generate the value of the testProp
variable?
As you can see, in my contacts bean I have the property set correctly
and if I put it in an h:outputText it prints correctly.
Thanks for the Kudos. Hopefully we can help you get up and running.
First off, I don't see the alternate #{} syntax mentioned in the JSTL
1.0 or 1.1 specs. I'm not sure where you got that from, but it looks
like your servlet container isn't processing it either.
For the ${} version, I don't see where you defined the ContactsBean in
the page. Is that done by the f:view tag? I don't see where the
associate taglib for that is defined either. Anyway, I suspect that
the problem is the ContactsBean is null. You might want to try simply
outputting the ContactsBean independently of the taglib so that you
can remove the taglib from the equation. Try adding the following to
your page:
ContactBean: ${ContactBean}
ContactBean.testProp: ${ContactsBean.testProp}
G'luck!
--
Jeremy Sears
1. Make sure the managed bean has been instantiated by JSF before
calling the json-taglib
2. Reference the objects from the adequate scope
<script type="text/javascript">
aStr = '<h:outputText value="#{ContactsBean}"/>'
<-- call this or something else to ensure your MB is instantiated
<json:object>
<json:property name="itemCount" value="$
{requestScope.ContactsBean.contactList}"/> <-- reference your MB in
its scope
</json:object>
</script>
Now it works fine!!
Thanks Jeremy!!!!
On May 8, 11:37 am, "Jeremy Sears"
<json.taglib.Jeremy.Se...@gmail.com> wrote:
> Hi there,
>
> Thanks for the Kudos. Hopefully we can help you get up and running.
>
> First off, I don't see the alternate #{} syntax mentioned in the JSTL
> 1.0 or 1.1 specs. I'm not sure where you got that from, but it looks
> like your servlet container isn't processing it either.
>
> For the ${} version, I don't see where you defined the ContactsBean in
> the page. Is that done by the f:view tag? I don't see where the
> associate taglib for that is defined either. Anyway, I suspect that
> the problem is the ContactsBean is null. You might want to try simply
> outputting the ContactsBean independently of the taglib so that you
> can remove the taglib from the equation. Try adding the following to
> your page:
>
> ContactBean: ${ContactBean}
> ContactBean.testProp: ${ContactsBean.testProp}
>
> G'luck!
> --
> Jeremy Sears
>
Thanks for your reply,
The "#" syntax is a JSF expression, but I'm guessing that is why the
"$" syntax is required, because the json-taglib is JSP.
And therefore it makes sense for the bean to be null, because the
ContactsBean is a JSF managed bean so maybe it is not yet instanced
when the taglib is invoked.
So this is obviously a JSF vs JSP issue and the problem is that I'm
forced to use 1.4 not 1.5.
I will try a few things and post back the results!
Thanks for your helpful comments, Jeremy.
Federico.