You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Scott Kingdon <sc...@kingdon.net> on 2007/11/09 23:51:10 UTC

Formatting strings in Struts2/Freemarker

I am very used to using JSP taglibs and el functions for formatting
text. In my current application I am using Struts2 with Freemarker as
the templating engine. I need to format a phone number that is pulled
from the database as 8888888888 to be (888)888-8888. Using el
functions you can just call a static method from some class. This
would be nice since I already have that static method and it is used
elsewhere in the app.

I would use JSP for this, but our template is already built in
Freemarker. There are also come JSP pages that use this phone
formatter as well. Actually I have already created the el function tag
and have been using it all over in the app.

I have tried several things including trying to use el functions in
freemarker or even including JSP pages from freemarker.

Any ideas how this is done in the Struts2/Freemarker world? I am open
to any idea.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Formatting strings in Struts2/Freemarker

Posted by Scott Kingdon <sc...@kingdon.net>.
This worked! Thank you, Wes!!

<@s.property value="@package.class@formatPhone('${Session.USER.customer.phone}')"/>

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

On Nov 9, 2007 9:29 PM, Wes Wannemacher <we...@wantii.com> wrote:
> Have you tried to use a struts2 tag with OGNL? If I am understanding
> correctly, you could use -
>
> @s.property value="@package.class@staticMethod('OGNL.pathTo.variable')
>
> Of course, I don't know freemarker, and this is assuming you can reach
> the variable from OGNL.
>
> -W
>
>
> On 11/9/07, Scott Kingdon <sc...@kingdon.net> wrote:
> > I am very used to using JSP taglibs and el functions for formatting
> > text. In my current application I am using Struts2 with Freemarker as
> > the templating engine. I need to format a phone number that is pulled
> > from the database as 8888888888 to be (888)888-8888. Using el
> > functions you can just call a static method from some class. This
> > would be nice since I already have that static method and it is used
> > elsewhere in the app.
> >
> > I would use JSP for this, but our template is already built in
> > Freemarker. There are also come JSP pages that use this phone
> > formatter as well. Actually I have already created the el function tag
> > and have been using it all over in the app.
> >
> > I have tried several things including trying to use el functions in
> > freemarker or even including JSP pages from freemarker.
> >
> > Any ideas how this is done in the Struts2/Freemarker world? I am open
> > to any idea.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
>
> --
> Wesley Wannemacher
> President, Head Engineer/Consultant
> WanTii, Inc.
> http://www.wantii.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Formatting strings in Struts2/Freemarker

Posted by Wes Wannemacher <we...@wantii.com>.
Have you tried to use a struts2 tag with OGNL? If I am understanding
correctly, you could use -

@s.property value="@package.class@staticMethod('OGNL.pathTo.variable')

Of course, I don't know freemarker, and this is assuming you can reach
the variable from OGNL.

-W

On 11/9/07, Scott Kingdon <sc...@kingdon.net> wrote:
> I am very used to using JSP taglibs and el functions for formatting
> text. In my current application I am using Struts2 with Freemarker as
> the templating engine. I need to format a phone number that is pulled
> from the database as 8888888888 to be (888)888-8888. Using el
> functions you can just call a static method from some class. This
> would be nice since I already have that static method and it is used
> elsewhere in the app.
>
> I would use JSP for this, but our template is already built in
> Freemarker. There are also come JSP pages that use this phone
> formatter as well. Actually I have already created the el function tag
> and have been using it all over in the app.
>
> I have tried several things including trying to use el functions in
> freemarker or even including JSP pages from freemarker.
>
> Any ideas how this is done in the Struts2/Freemarker world? I am open
> to any idea.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Wesley Wannemacher
President, Head Engineer/Consultant
WanTii, Inc.
http://www.wantii.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Tiles-def.xml

Posted by "Zhang, Larry (L.)" <lz...@ford.com>.
That approach works except in your following note it should be


<tiles:importAttribute name="myList" />
<c:forEach var="item" items="${myList}">
		c:out value="${item}" />
	</c:forEach> 

Thanks a lot.



-----Original Message-----
From: Antonio Petrelli [mailto:antonio.petrelli@gmail.com] 
Sent: Monday, November 12, 2007 11:07 AM
To: Struts Users Mailing List
Subject: Re: Tiles-def.xml

2007/11/12, Zhang, Larry (L.) <lz...@ford.com>:
>
>                 <tiles:getAsString name="myCss1" ignore="true"/>
>                 <tiles:getAsString name="myCss2" ignore="true"/>
>                 <tiles:getAsString name="myCss3" ignore="true"/>
>                 <tiles:getAsString name="myCss4" ignore="true"/>
>                 <tiles:getAsString name="myCss5" ignore="true"/>
>                 <tiles:getAsString name="myCss6" ignore="true"/>
>                 <tiles:getAsString name="myCss7" ignore="true"/>
>                 <tiles:getAsString name="myCss8" ignore="true"/>
>                 <tiles:getAsString name="myCss9" ignore="true"/>
>                 <tiles:getAsString name="myCss10" ignore="true"/>


I think that you could use the list attribute:

<definition....>
  <putList name="myList">
    <add value="myfirst.css" />
    <add value="mysecond.css" />
  </putList>
</definition>

And in your JSP:

<tiles:importAttribute name="myList" />
<c:forEach var="item" items="myList">
  <c:out var="item" />
</c:forEach>


HTH
Antonio

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Tiles-def.xml

Posted by Antonio Petrelli <an...@gmail.com>.
2007/11/12, Zhang, Larry (L.) <lz...@ford.com>:
>
>                 <tiles:getAsString name="myCss1" ignore="true"/>
>                 <tiles:getAsString name="myCss2" ignore="true"/>
>                 <tiles:getAsString name="myCss3" ignore="true"/>
>                 <tiles:getAsString name="myCss4" ignore="true"/>
>                 <tiles:getAsString name="myCss5" ignore="true"/>
>                 <tiles:getAsString name="myCss6" ignore="true"/>
>                 <tiles:getAsString name="myCss7" ignore="true"/>
>                 <tiles:getAsString name="myCss8" ignore="true"/>
>                 <tiles:getAsString name="myCss9" ignore="true"/>
>                 <tiles:getAsString name="myCss10" ignore="true"/>


I think that you could use the list attribute:

<definition....>
  <putList name="myList">
    <add value="myfirst.css" />
    <add value="mysecond.css" />
  </putList>
</definition>

And in your JSP:

<tiles:importAttribute name="myList" />
<c:forEach var="item" items="myList">
  <c:out var="item" />
</c:forEach>


HTH
Antonio

RE: Tiles-def.xml

Posted by "Zhang, Larry (L.)" <lz...@ford.com>.
My problem is:

I have 10 (or more ) css files, some pages use 2 of those, some of those
uses 5 of those, some pages use 6 of those, and this combination is
totally random. Then we I create tiles template, I can't put all these
css in the template, instead I thought I can do the following:

My template:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
	<title><tiles:getAsString name="title" ignore="true"/></title>

	<style type="text/css" media="screen">  
		<tiles:getAsString name="myCss1" ignore="true"/>	
		<tiles:getAsString name="myCss2" ignore="true"/>	
		<tiles:getAsString name="myCss3" ignore="true"/>	
		<tiles:getAsString name="myCss4" ignore="true"/>	
		<tiles:getAsString name="myCss5" ignore="true"/>	
  		<tiles:getAsString name="myCss6" ignore="true"/>	
		<tiles:getAsString name="myCss7" ignore="true"/>	
		<tiles:getAsString name="myCss8" ignore="true"/>	
		<tiles:getAsString name="myCss9" ignore="true"/>	
		<tiles:getAsString name="myCss10" ignore="true"/>	
	</style>

</head>


<body>

	<tiles:insert attribute="header"/><br />
	<tiles:insert attribute="bodyContent"/><br />
	<tiles:insert attribute="footer"/><br />
</body>
</html>

Then  I need to define myCss1 and etc in the tiles-def.xml, I don't know
what is the good one to define this, I don't want to define
them like the following :

<definition name="default" path="/jsp/common/main.jsp">
        <put name="header" value="/jsp/common/header.jsp" />
	   <put name="footer" value="/jsp/common/footer.jsp" />
    </definition>	
    
    <definition name="mySepeficPage" extends="default">
    	<put name="title" value="SMERF &gt; Change Request Queue" />
       put name="myCss1" value="the path to the Css1"
      	<put name="bodyContent" value="/jsp/mySpefic.jsp" />
    </definition>
 
Since that will make my tiles-def.xml too massy. 

So the original issue is how to put the dynamic css files in the
template between <head>....<head>.  I would think this is a common issue
for tiles community. Do you have any better way to resolve this issue?



-----Original Message-----
From: Antonio Petrelli [mailto:antonio.petrelli@gmail.com] 
Sent: Monday, November 12, 2007 10:30 AM
To: Struts Users Mailing List
Subject: Re: Tiles-def.xml

2007/11/12, Zhang, Larry (L.) <lz...@ford.com>:
>
> Can I define a constant in tiles-def.xml so that I can used it in all
> places inside this file?



No, but probably you can do it in another way.
What do you want to do exactly?

Antonio

P.S.: If you are using Tiles 2, please ask this question to Tiles Users
Mailing List:
http://tiles.apache.org/mail.html
If you are using Struts/Tiles, go on here and ignore this P.S. :-)

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Tiles-def.xml

Posted by Antonio Petrelli <an...@gmail.com>.
2007/11/12, Zhang, Larry (L.) <lz...@ford.com>:
>
> Can I define a constant in tiles-def.xml so that I can used it in all
> places inside this file?



No, but probably you can do it in another way.
What do you want to do exactly?

Antonio

P.S.: If you are using Tiles 2, please ask this question to Tiles Users
Mailing List:
http://tiles.apache.org/mail.html
If you are using Struts/Tiles, go on here and ignore this P.S. :-)

Tiles-def.xml

Posted by "Zhang, Larry (L.)" <lz...@ford.com>.
Can I define a constant in tiles-def.xml so that I can used it in all
places inside this file? 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Formatting strings in Struts2/Freemarker

Posted by Dave Newton <ne...@yahoo.com>.
--- Scott Kingdon <sc...@kingdon.net> wrote:
> Thank you for that. That was a huge step in the
> right direction. However, that does not work.

*lol* Huge step off a short pier--I'm good at those.

Hmm, yeah, I didn't know about the lack of JSP tag
file support, but that's a good one to keep a handle
on, so thanks for the extra pursuit :)

On my list to add to the wiki.

d.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Formatting strings in Struts2/Freemarker

Posted by Scott Kingdon <sc...@kingdon.net>.
Thank you for that. That was a huge step in the right direction.
However, that does not work.

freemarker.core.InvalidReferenceException: on line 4, column 146 in
template/xhtml/components/loginlink.ftl unitag.phone not found.


I found a snipped over on Freemarker's forum that says Freemarker does
not support some JSP 2.0 features. Bummer. That means this will
probably work if I just write a taglib instead. Or I have to wait
until they realease 2.3.11. Currently they are on 2.3.10...

https://sourceforge.net/forum/message.php?msg_id=4587560

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Formatting strings in Struts2/Freemarker

Posted by Dave Newton <ne...@yahoo.com>.
http://struts.apache.org/2.x/docs/freemarker.html

Towards the bottom.

--- Scott Kingdon <sc...@kingdon.net> wrote:

> If that works that would be great. I tried it, but
> maybe I am doing
> something wrong.
> 
> In the JSP world, in order to use an el function you
> need to declair
> it at the top of the page like this:
> 
> <%@ taglib prefix="uni" uri="UniFunctions" %>
> 
> When I put this at the top of a Freemarker page, it
> just gets printed
> out to the page as text. So that does not work.
> 
> I tried declaring it in the JSP page before the
> freemarker template is
> called. Then I called the el function like this in
> my template.ftl:
> 
> ${uni:phone(session.USER.customer.phone)}
> 
> This spews out a nasty error. Freemarker complains
> that it does not
> know what to do with uni:phone.
> 
>
===========================================================
> 
> On Nov 9, 2007 4:08 PM, Dave Newton
> <ne...@yahoo.com> wrote:
> > Since you can use any S2 tag inside a FreeMarker
> > template I guess I had assumed you could use the
> same
> > syntax to call static methods you can from JSP,
> does
> > that not work?
> >
> > (AFAIK you can also expose the JSTL library to a
> > FreeMarker page but I'm not sure how the two ELs
> would
> > play together; w/ OGNL you can use the %{...} EL
> > escape.)
> >
> > d.
> >
> >
> > --- Scott Kingdon <sc...@kingdon.net> wrote:
> >
> > > I am very used to using JSP taglibs and el
> functions
> > > for formatting
> > > text. In my current application I am using
> Struts2
> > > with Freemarker as
> > > the templating engine. I need to format a phone
> > > number that is pulled
> > > from the database as 8888888888 to be
> (888)888-8888.
> > > Using el
> > > functions you can just call a static method from
> > > some class. This
> > > would be nice since I already have that static
> > > method and it is used
> > > elsewhere in the app.
> > >
> > > I would use JSP for this, but our template is
> > > already built in
> > > Freemarker. There are also come JSP pages that
> use
> > > this phone
> > > formatter as well. Actually I have already
> created
> > > the el function tag
> > > and have been using it all over in the app.
> > >
> > > I have tried several things including trying to
> use
> > > el functions in
> > > freemarker or even including JSP pages from
> > > freemarker.
> > >
> > > Any ideas how this is done in the
> Struts2/Freemarker
> > > world? I am open
> > > to any idea.
> > >
> > >
> >
>
---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> > > user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail:
> > > user-help@struts.apache.org
> > >
> > >
> >
> >
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> > For additional commands, e-mail:
> user-help@struts.apache.org
> >
> >
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Formatting strings in Struts2/Freemarker

Posted by Scott Kingdon <sc...@kingdon.net>.
If that works that would be great. I tried it, but maybe I am doing
something wrong.

In the JSP world, in order to use an el function you need to declair
it at the top of the page like this:

<%@ taglib prefix="uni" uri="UniFunctions" %>

When I put this at the top of a Freemarker page, it just gets printed
out to the page as text. So that does not work.

I tried declaring it in the JSP page before the freemarker template is
called. Then I called the el function like this in my template.ftl:

${uni:phone(session.USER.customer.phone)}

This spews out a nasty error. Freemarker complains that it does not
know what to do with uni:phone.

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

On Nov 9, 2007 4:08 PM, Dave Newton <ne...@yahoo.com> wrote:
> Since you can use any S2 tag inside a FreeMarker
> template I guess I had assumed you could use the same
> syntax to call static methods you can from JSP, does
> that not work?
>
> (AFAIK you can also expose the JSTL library to a
> FreeMarker page but I'm not sure how the two ELs would
> play together; w/ OGNL you can use the %{...} EL
> escape.)
>
> d.
>
>
> --- Scott Kingdon <sc...@kingdon.net> wrote:
>
> > I am very used to using JSP taglibs and el functions
> > for formatting
> > text. In my current application I am using Struts2
> > with Freemarker as
> > the templating engine. I need to format a phone
> > number that is pulled
> > from the database as 8888888888 to be (888)888-8888.
> > Using el
> > functions you can just call a static method from
> > some class. This
> > would be nice since I already have that static
> > method and it is used
> > elsewhere in the app.
> >
> > I would use JSP for this, but our template is
> > already built in
> > Freemarker. There are also come JSP pages that use
> > this phone
> > formatter as well. Actually I have already created
> > the el function tag
> > and have been using it all over in the app.
> >
> > I have tried several things including trying to use
> > el functions in
> > freemarker or even including JSP pages from
> > freemarker.
> >
> > Any ideas how this is done in the Struts2/Freemarker
> > world? I am open
> > to any idea.
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > user-unsubscribe@struts.apache.org
> > For additional commands, e-mail:
> > user-help@struts.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Formatting strings in Struts2/Freemarker

Posted by Dave Newton <ne...@yahoo.com>.
Since you can use any S2 tag inside a FreeMarker
template I guess I had assumed you could use the same
syntax to call static methods you can from JSP, does
that not work?

(AFAIK you can also expose the JSTL library to a
FreeMarker page but I'm not sure how the two ELs would
play together; w/ OGNL you can use the %{...} EL
escape.)

d.

--- Scott Kingdon <sc...@kingdon.net> wrote:

> I am very used to using JSP taglibs and el functions
> for formatting
> text. In my current application I am using Struts2
> with Freemarker as
> the templating engine. I need to format a phone
> number that is pulled
> from the database as 8888888888 to be (888)888-8888.
> Using el
> functions you can just call a static method from
> some class. This
> would be nice since I already have that static
> method and it is used
> elsewhere in the app.
> 
> I would use JSP for this, but our template is
> already built in
> Freemarker. There are also come JSP pages that use
> this phone
> formatter as well. Actually I have already created
> the el function tag
> and have been using it all over in the app.
> 
> I have tried several things including trying to use
> el functions in
> freemarker or even including JSP pages from
> freemarker.
> 
> Any ideas how this is done in the Struts2/Freemarker
> world? I am open
> to any idea.
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org