9

I am using spring with freemarker as the template engine. Freemarker allows to use the Jsp Taglibs, for security for example, by adding

    <#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />

to the templates, what allows me to use for example

    <@security.authorize ifNotGranted="ROLE_ADMIN">
        whatever
    </@security.authorize>

But, Spring/Freemarker cannot find the taglibs, unless they are added to the classpath, so I added

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${spring.version}</version>
    </dependency>

to my pom.xml in my project.

But anyway, the tags couldn't be found! I had to add the spring-security-taglibs.jar into WEB-INF/lib folder for the tags to be found.

Does someone know why the jar has to be added explicitly into the lib folder?? Why aren't they found by tomcat, in my case?

EDIT @ddekany

Thank you. The stacktrace is the following, if the spring-security-taglibs.jar is not copied into the WEB-INF/lib directory

    No mapping defined for http://www.springframework.org/security/tags 
    The problematic instruction: ---------- ==> assignment: 
            security=JspTaglibs["http://www.springframework.org/security/tags"] 
            [on line 12, column 1 in home.ftl] in user-directive content.main 
            [on line 8, column 9 in home.ftl] in user-directive layout.global 
            [on line 2, column 1 in home.ftl] 
    ---------- Java backtrace for programmers: ----------      
    freemarker.template.TemplateModelException: 
            No mapping defined for http://www.springframework.org/security/tags at         
    freemarker.ext.jsp.TaglibFactory.get(TaglibFactory.java:180) at 
    ...
1
  • I think you should include the exact error message in your question. Also, I don't think that the JSP specification sates that it looks for the taglibs (tld-s) using the current(?) class-loader or such, but I'm maybe wrong in that.
    – ddekany
    Aug 22, 2012 at 9:53

3 Answers 3

7

In case anyone else runs into this...

You need to add the spring support files, as outlined here (just some cut & paste) http://static.springsource.org/spring-webflow/docs/2.2.x/reference/html/ch13s09.html.

And then add some dependencies:

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-taglibs</artifactId>
  <version>2.0.0</version>
</dependency>
<dependency>
  <groupId>org.springframework.webflow</groupId>
  <artifactId>spring-faces</artifactId>
  <version>2.3.1.RELEASE</version>
</dependency>

Assuming you have everything else working, you should now be able to add the taglib to your pages. For example:

xmlns:sec="http://www.springframework.org/security/tags"

< sec:authorize ifAllGranted="USER_ROLE">
Hello user
< /sec:authorize>

*had to add a space b/f 'sec' to post it

1
  • 1
    The last version of Spring that have spring-faces was 2.4.1.
    – Raedwald
    Jan 8, 2015 at 11:12
2

Use this Maven dependency:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-taglibs</artifactId>
    <version>3.2.5.RELEASE</version>
</dependency>

org.springframework and org.springframework.security are different frameworks with different version numbers.

0

Did you include the JspSupportServlet as stated here and here

[EDIT] After reading your post a bit more careful I advise you the read section "JSP.7.3.2" (and onwards) from the JSP specification.

4
  • Yes, I have added the JspSupportServlet as you proposed, but the error is still the same. Looking at Section "JSP.7.3.2" in JSP specification the essential part I guess is this: "The TLD resource path is interpreted relative to the root of the web application and should resolve to a TLD file directly, or to a JAR file that has a TLD file at location META-INF/taglib.tld". Is this the answer why tomcat cannot find the META-INF/*.tld file? Because it I included it via a maven dependency?
    – matthaeus
    Aug 23, 2012 at 9:24
  • The META-INF/*.tld files are loaded from jar's. The jar files need to be in the classpath which is WEB-INF/lib. If it is not found there no J2EE container will load the tld.
    – tom
    Aug 23, 2012 at 9:45
  • Ok, I understand that, but why isn't it sufficient to add the maven dependency to the spring taglib library? Like can be seen in this and this examples?
    – matthaeus
    Aug 24, 2012 at 8:59
  • This can only mean that your maven project is setup wrong. When I create a 'war' project in maven it always places the libs in the web-inf/lib folder.
    – tom
    Aug 24, 2012 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.