Adding Spring Security JspTagLib to a Freemarker template - issues with controller unit tests

怎甘沉沦 提交于 2019-12-08 17:56:09

问题


I have added Springs jsp security taglib to a freemarker template because I'm using freemarker for the view of my web app rather than jsps. For anyone searching for how to set this up, I found Adding spring library for usage of JSP Taglibs for security in Freemarker to be a very helpful question. In summary, add the following to the *.ftl file you wish to use the tags in:

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

Then assuming you're using maven add the following to your pom.xml:

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

Once I set this up I ran my spring controller unit tests and they all failed. The problem was they needed el-api.jar and jsp-api.jar to resolve how to render the Jsp tags. These are included as part of the container in which the web app runs (tomcat), hence they weren't needed for normal running of the application. So I added these as maven dependencies within test scope.

<!-- Required for spring controller junit tests, due to use of jspTagLib for security -->
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>el-api</artifactId>
        <version>2.2</version>
        <scope>test</scope>
    </dependency>

<!-- Required for spring controller junit tests, due to use of jspTagLib for security -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>test</scope>
    </dependency>

With this fixed my tests also threw an error that they couldn't find the mapping for the .tld file even though it is included when you add the spring-security-taglibs maven dependency.

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is freemarker.template.TemplateModelException: No mapping defined for http://www.springframework.org/security/tags
The failing instruction (print stack trace for 2 more):
==> #assign security = JspTaglibs["http:/...  [in template "global/menu.ftl" at line 1, column 1]

In order to fix this I also had to add security.tld to WEB-INF/lib/. Everything works now but it's not good that I need to have the tag library security.tld in two places (spring-security-taglibs-3.1.3.RELEASE.jar under META-INF and WEB-INF/lib).

My question is does anyone know how I can avoid this duplication of having security.tld in two places just to be able to run unit tests, I would rather just have it included in the jar file and not add it to WEB-INF/lib?

来源:https://stackoverflow.com/questions/20100276/adding-spring-security-jsptaglib-to-a-freemarker-template-issues-with-controll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!