Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Using taglibs in freemarker

  1. #1
    Join Date
    Aug 2004
    Location
    Karlsruhe, Germany
    Posts
    17

    Default Using taglibs in freemarker

    When using Freemarker with it's own FreemarkerServlet it is possible to use JSP taglibs in a freemarker template using something like
    Code:
    <#assign portlet=JspTaglibs&#91;"http&#58;//java.sun.com/portlet"&#93;>
    Is this possible using Springs freemarker view?

    Thanks,
    Rainer

  2. #2
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    There was a similar request on the FreeMarker lists a couple of days ago - I didn't know you could do that until I saw that post. The documentation for this is hidden at the bottom of the page on "using FreeMarker with servlets" on the FreeMarker site!

    There's certainly no current support for this in Spring. It works using the FM servlet because their servlet creates its own custom taglib loader. It doesn't look as though it would be too difficult to add support for it to the Spring classes - some checking of variable scoping would be required (Page, Request, Session and Application) since Spring's FM support doesn't scope this way. Maybe it needs to.

    If you needed support for this right now, the following may work (completely untested):
    • 1. define the FreeMarker servlet in your web.xml and map it to *.ftl as recommended.
      2. Set the TemplatePath init-param to be /WEB-INF/freemarker
      3. declare a view resolver in your Spring context like so,
      Code:
      <bean 
          id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass">
          <value>org.springframework.web.servlet.view.InternalResourceView</value>
        </property>
        <property name="prefix"><value></value></property>
        <property name="suffix"><value>.ftl</value></property>
       </bean>
      4. From your controller, return a view name of "foo" to load the template /WEB-INF/freemarker/foo.ftl


    Templates loaded this way should be able to load the taglibs - I'll try to have a look at this today to see what's involved.

    Let us know how you get on if you try this?
    Darren Davison.
    Public Key: 0xE855B3EA

  3. #3
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    Quote Originally Posted by davison
    (completely untested)
    curiosity got the better of me. It does work. You can load the spring taglibs with

    Code:
    <#assign springtags=JspTaglibs&#91;"http&#58;//www.springframework.org/tags"&#93;>
    and then use, say, the spring:message tag to pull a string from your messages.properties (or whatever)..

    Code:
    <@springtags.message code="my.message" text="default message"/>
    cool. I wonder if we can make Velocity do the same?
    Darren Davison.
    Public Key: 0xE855B3EA

  4. #4
    Join Date
    Aug 2004
    Location
    Karlsruhe, Germany
    Posts
    17

    Default

    Darren,

    I tried myself meanwhile: yes, it works!

    I got some error messages when trying to use the spring:bind tag, though. In addition the spring freemarker convenience macros don't work when using the FreemarkerServlet because the "SpringMacroHelpers" are missing. So you can't mix tags and spring macros using the workaround you described.

    Taglib support was the main reason for me to switch from velocity to freemarker. Often some useful jsp tags are provided (e.g. for portlets), and it saves a lot of time not being forced to convert those tags to macros or find another workaround.
    I guess what I'm trying to say is: freemarker taglib support within spring would be very nice! :wink:

    Cheers,
    Rainer

  5. #5
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    Quote Originally Posted by swift
    So you can't mix tags and spring macros using the workaround you described.
    correct - you're now bypassing the Spring classes almost entirely. It was a quick and dirty solution

    Quote Originally Posted by swift
    it saves a lot of time not being forced to convert those tags to macros or find another workaround.
    I guess what I'm trying to say is: freemarker taglib support within spring would be very nice!
    indeed! Re-use is always good. As mentioned, I think this should be reasonably straightforward (time permitting as ever) so I'll endeavour to add it.
    Darren Davison.
    Public Key: 0xE855B3EA

  6. #6
    Join Date
    Aug 2004
    Location
    Karlsruhe, Germany
    Posts
    17

    Default

    As mentioned, I think this should be reasonably straightforward (time permitting as ever) so I'll endeavour to add it.
    Darren, have you had time to work on this issue?

    Rainer

  7. #7
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    Actually I did have a look at it, but unfortunately it's less easy than it should be to implement. The FreeMarker servlet relies on several non-public classes in the same package in freemarker.jar to make the taglibs available and all of this would need to be duplicated by Spring.

    I had a brief conversation with one of the FM developers on their dev mailing list (which you can search at gmane if interested) but to be honest I think the developers have pretty much lost interest in FreeMarker (see other threads on their dev list discussing Revusky who already left, and Dekany/Szegedi who are about to or have been considering it).

    I'll take another look at some point but not for a few days as I'm really busy at work. Pity, but it won't be the quick win I'd hoped it might be.

    Regards,
    Darren Davison.
    Public Key: 0xE855B3EA

  8. #8
    Join Date
    Aug 2004
    Location
    Karlsruhe, Germany
    Posts
    17

    Default

    Quote Originally Posted by davison
    I had a brief conversation with one of the FM developers on their dev mailing list (which you can search at gmane if interested) but to be honest I think the developers have pretty much lost interest in FreeMarker (see other threads on their dev list discussing Revusky who already left, and Dekany/Szegedi who are about to or have been considering it).
    That's bad news. Are there any alternatives? Progress on Velocity is very slow, too. Do we all end using JSP after all?

    Regards,
    Rainer

  9. #9
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    Quote Originally Posted by swift
    That's bad news. Are there any alternatives? Progress on Velocity is very slow, too. Do we all end using JSP after all?
    Well, both Velocity and FreeMarker still offer significant advantages to some projects over JSP - particularly if those projects are running on containers like WebSphere 4.x (as many people still do) which only implements v2.2 of the Servlet API. Even if development of both languages stopped, they are still useful as they are.

    The problem (if it can be called one) is that there's not a great deal else that either Velocity or FreeMarker can do. They were created to serve a specific need and do it pretty well. As the FM devs have said, the "only" thing that really could be done is a rewrite to produce a better design and possibly a faster engine.
    Darren Davison.
    Public Key: 0xE855B3EA

  10. #10
    Join Date
    Sep 2004
    Location
    Boston, US
    Posts
    130

    Default

    I would also like to use FreeMarker with the JSP support as we have to use existing JSP tag libraries. Given the extensive third party tag libraries available, I don't see how people are using Velocity or the non-JSP form of Freemarker in thier projects. IMO the Freemarker JSP tag lib support is huge as it gives you the best of both worlds.

    Darren, would creating a custom view resolver that is a cross of the InternalResourceView and FreeMarkerView view classes not work?

    ie a version of InternalResourceView that (among other things that I'm not aware of) adds the following to the model.

    Code:
    model.put&#40;"springMacroRequestContext", new RequestContext&#40;request, model&#41;&#41;;
    Would this allow the Spring Freemarker tags to function correctly? I've only recently started looking into using Freemarker w/ Spring and I'm sure there's more to it but would like to know the extra work required to get this to function.

    Thanks,
    Sanjiv

Similar Threads

  1. Replies: 3
    Last Post: Jan 8th, 2010, 02:50 AM
  2. Replies: 3
    Last Post: Oct 28th, 2005, 01:49 PM
  3. FreeMarker vs Velocity
    By Martin Kersten in forum Architecture
    Replies: 8
    Last Post: May 30th, 2005, 09:21 AM
  4. Freemarker and jsp taglibs - again
    By iserv in forum Web
    Replies: 0
    Last Post: Mar 14th, 2005, 09:10 AM
  5. Freemarker is impressive!
    By j2eeguru in forum Web
    Replies: 1
    Last Post: Dec 2nd, 2004, 03:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •