You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Ugo Cei <ug...@apache.org> on 2006/01/27 12:00:13 UTC

Java objects in JX templates

Our docs [1] state that something like:

  <jx:forEach var="${var}"
          items="${java.util.StringTokenizer(items, delims)}">
   </jx:forEach>

should work. However, that doesn't seem to be the case, at least in  
2.1.8 while it apparently worked before. I did a few more tests and  
discovered that even simple cases like ${java.util.Date()} produce no  
output and no error message. This even if I put "Packages." in front  
of the package name. Is this a bug or what?

	Ugo

[1] http://cocoon.apache.org/2.1/userdocs/flow/jxtemplate.html


-- 
Ugo Cei
Tech Blog: http://agylen.com/
Open Source Zone: http://oszone.org/



Re: Java objects in JX templates

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Sylvain Wallez wrote:
> Ugo Cei wrote:
> 
>> Our docs [1] state that something like:
>>
>>  <jx:forEach var="${var}"
>>          items="${java.util.StringTokenizer(items, delims)}">
>>   </jx:forEach>
>>
>> should work. However, that doesn't seem to be the case, at least in 
>> 2.1.8 while it apparently worked before. I did a few more tests and 
>> discovered that even simple cases like ${java.util.Date()} produce no 
>> output and no error message. This even if I put "Packages." in front 
>> of the package name. Is this a bug or what?
> 
> 
> You can only call methods on existing objects. I had to use a static 
> method (only callable using JXPath!) as a workaround in the CForms macros:
This is not true. You can call static methods and even constructors. 
Both from JXPath and JEXL.

   <jx:set var="buffer" value="${java.lang.StringBuffer('Hello ')}"/>
   <jx:set var="ignore" value="${buffer.append( 'World!' )}"/>
	<hw>${buffer.toString()}</hw>

Works like a charm in trunk.



> 
> <jx:set var="cformsHelper" 
> value="#{org.apache.cocoon.forms.generation.JXMacrosHelper.createHelper($cocoon/consumer,$cocoon/request,$cocoon/parameters/locale)}"/> 
> 
> 
> Sylvain
> 


-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re: Java objects in JX templates

Posted by Sylvain Wallez <sy...@apache.org>.
Ugo Cei wrote:
> Our docs [1] state that something like:
>
>  <jx:forEach var="${var}"
>          items="${java.util.StringTokenizer(items, delims)}">
>   </jx:forEach>
>
> should work. However, that doesn't seem to be the case, at least in 
> 2.1.8 while it apparently worked before. I did a few more tests and 
> discovered that even simple cases like ${java.util.Date()} produce no 
> output and no error message. This even if I put "Packages." in front 
> of the package name. Is this a bug or what?

You can only call methods on existing objects. I had to use a static 
method (only callable using JXPath!) as a workaround in the CForms macros:

<jx:set var="cformsHelper" 
value="#{org.apache.cocoon.forms.generation.JXMacrosHelper.createHelper($cocoon/consumer,$cocoon/request,$cocoon/parameters/locale)}"/>

Sylvain

-- 
Sylvain Wallez                        Anyware Technologies
http://bluxte.net                     http://www.anyware-tech.com
Apache Software Foundation Member     Research & Technology Director


Re: Java objects in JX templates

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Ugo Cei wrote:
> 
> Il giorno 27/gen/06, alle ore 15:54, Leszek Gawron ha scritto:
> 
>> Ugo Cei wrote:
>>
>>> Our docs [1] state that something like:
>>>  <jx:forEach var="${var}"
>>>          items="${java.util.StringTokenizer(items, delims)}">
>>>   </jx:forEach>
>>> should work. However, that doesn't seem to be the case, at least  in  
>>> 2.1.8 while it apparently worked before. I did a few more  tests and  
>>> discovered that even simple cases like ${java.util.Date ()} produce 
>>> no  output and no error message. This even if I put  "Packages." in 
>>> front  of the package name. Is this a bug or what?
>>>     Ugo
>>> [1] http://cocoon.apache.org/2.1/userdocs/flow/jxtemplate.html
>>
>>
>> Didn't you want to do:
>> <jx:forEach var="varName"
>>   items="${java.util.StringTokenizer(items, delims)}">
>> </jx:forEach>
> 
> 
> The code I copied above is straight from our docs, but it doesn't  make 
> an inch of a difference whether I change to what you suggest or  not. 
> The problem is that invoking Java class constructors or calling  static 
> methods does not work.
As I replied in another message: referencing any java constructs like:

- packages
- classes
- constructors
- static methods
- constants

is available in jxtg via rhino (org.mozilla.javascript.NativeJavaClass, 
NativeJavaPackage, NativeJavaTopPackage). ${Packages} is simply a 
NativeJavaTopPackage and ${java} is a NativeJavaPackage. They are 
treated as ordinary view data. I have never tried but this should work also:

<jx:set var="MyConstantsClass" 
value=${Packages.com.mycompany.some.very.long.package.name.MyConstantsClass}"/>

<jx:if test="${bean.status == MyConstantsClass.STATUS_OK}">
    <ok/>
</jx:if>

Unfortunatelly in 2.1.x those variables are only properly initialized 
when flowscript is the controller. 2.2 artificialy creates those 
variables no matter if you already have rhino context available or not.



-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re: Java objects in JX templates

Posted by Ugo Cei <ug...@apache.org>.
Il giorno 27/gen/06, alle ore 15:54, Leszek Gawron ha scritto:

> Ugo Cei wrote:
>> Our docs [1] state that something like:
>>  <jx:forEach var="${var}"
>>          items="${java.util.StringTokenizer(items, delims)}">
>>   </jx:forEach>
>> should work. However, that doesn't seem to be the case, at least  
>> in  2.1.8 while it apparently worked before. I did a few more  
>> tests and  discovered that even simple cases like ${java.util.Date 
>> ()} produce no  output and no error message. This even if I put  
>> "Packages." in front  of the package name. Is this a bug or what?
>>     Ugo
>> [1] http://cocoon.apache.org/2.1/userdocs/flow/jxtemplate.html
>
> Didn't you want to do:
> <jx:forEach var="varName"
>   items="${java.util.StringTokenizer(items, delims)}">
> </jx:forEach>

The code I copied above is straight from our docs, but it doesn't  
make an inch of a difference whether I change to what you suggest or  
not. The problem is that invoking Java class constructors or calling  
static methods does not work.

	Ugo


-- 
Ugo Cei
Tech Blog: http://agylen.com/
Open Source Zone: http://oszone.org/



Re: Java objects in JX templates

Posted by Fabrizio Sitzia <fa...@chem.lu>.
Hello,

I upgraded two webapps from Coccon 2.1.7 to 2.1.8 lately...

And I found this thread intriguing, so I have added the following to one
of my existing JX templates (which is run through a JX generator pipeline,
invoked by flowscript):

  <p>
      <jx:set var="items" value="alpha,beta,gamma"/>
      <jx:set var="delims" value=","/>
      <jx:set var="mytokenizer" value="${java.util.StringTokenizer(items,
delims)}"/>
      <jx:forEach var="token" begin="1" end="${mytokenizer.countTokens()}">
          ${mytokenizer.nextToken()}<br/>
      </jx:forEach>
  </p>


The html result:

  <p>
          alpha<br>
          beta<br>
          gamma<br>
  </p>


So, it appears to work fine for flowscript! (Mi spiace, Ugo ;-)
(Don't know about Javaflow, though!)

Fabrizio


BTW. the reason I was intrigued is that I'm using similar constructs in
production webapps - which made me wonder why users haven't been
complaining if indeed this feature had stopped working after migrating to
Cocoon 2.1.8 :->


>> ${java.util.StringTokenizer(items, delims)}
>>
> According to my tests, it doesn't work in 2.1.8, regardless of
> whether I use flow or not and whether I prepend "Packages." or not.
> Can anyone else confirm this?


Re: Java objects in JX templates

Posted by Ugo Cei <ug...@apache.org>.
Il giorno 27/gen/06, alle ore 21:10, Leszek Gawron ha scritto:

> One more thing: this construct
>
> ${java.util.StringTokenizer(items, delims)}
>
> will work only when .jx is invoked from flow. This is due to the  
> fact that neither JEXL nor JXPath is able to reference packages/ 
> create java objects. The functionality in fact is provided by Rhino  
> library.

According to my tests, it doesn't work in 2.1.8, regardless of  
whether I use flow or not and whether I prepend "Packages." or not.  
Can anyone else confirm this?

	Ugo


-- 
Ugo Cei
Tech Blog: http://agylen.com/
Open Source Zone: http://oszone.org/



Re: Java objects in JX templates

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Jens Maukisch wrote:
> Hi,
> 
> 
>>>[1] http://cocoon.apache.org/2.1/userdocs/flow/jxtemplate.html
> 
> 
>>Didn't you want to do:
>><jx:forEach var="varName"
>>   items="${java.util.StringTokenizer(items, delims)}">
>></jx:forEach>
> 
> 
> Have you tried the sample mentioned in the Docs? It does not work
> for me either and your corrected code above too (C2.1.8).
> 
> So I think it's a bug?
One more thing: this construct

${java.util.StringTokenizer(items, delims)}

will work only when .jx is invoked from flow. This is due to the fact 
that neither JEXL nor JXPath is able to reference packages/create java 
objects. The functionality in fact is provided by Rhino library.

This is already fixed for 2.2-dev (still the dependency on Rhino remains).


-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re[2]: Java objects in JX templates

Posted by Jens Maukisch <co...@maukisch.net>.
Hi,

>> [1] http://cocoon.apache.org/2.1/userdocs/flow/jxtemplate.html

> Didn't you want to do:
> <jx:forEach var="varName"
>    items="${java.util.StringTokenizer(items, delims)}">
> </jx:forEach>

Have you tried the sample mentioned in the Docs? It does not work
for me either and your corrected code above too (C2.1.8).

So I think it's a bug?

-- 
* best regards
* Jens Maukisch              



Re: Java objects in JX templates

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Ugo Cei wrote:
> Our docs [1] state that something like:
> 
>  <jx:forEach var="${var}"
>          items="${java.util.StringTokenizer(items, delims)}">
>   </jx:forEach>
> 
> should work. However, that doesn't seem to be the case, at least in  
> 2.1.8 while it apparently worked before. I did a few more tests and  
> discovered that even simple cases like ${java.util.Date()} produce no  
> output and no error message. This even if I put "Packages." in front  of 
> the package name. Is this a bug or what?
> 
>     Ugo
> 
> [1] http://cocoon.apache.org/2.1/userdocs/flow/jxtemplate.html

Didn't you want to do:
<jx:forEach var="varName"
   items="${java.util.StringTokenizer(items, delims)}">
</jx:forEach>


-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65