将Spring Security JspTagLib添加到Freemarker模板 – 控制器unit testing问题

我已经将spring jsp security taglib添加到freemarker模板,因为我使用freemarker来查看我的web应用程序而不是jsps。 对于任何搜索如何进行设置的人,我发现添加弹簧库以便在Freemarker中使用JSP Taglibs以获得安全性是一个非常有用的问题。 总之,将以下内容添加到您希望使用标记的* .ftl文件中:

 

然后假设您正在使用maven将以下内容添加到您的pom.xml:

  org.springframework.security spring-security-taglibs ${spring.security.version}  

一旦我设置了它,我运行我的弹簧控制器unit testing,他们都失败了。 问题是他们需要el-api.jar和jsp-api.jar来解决如何呈现Jsp标签。 它们作为Web应用程序运行的容器(tomcat)的一部分包含在内,因此应用程序的正常运行不需要它们。 所以我在测试范围内添加了这些作为maven依赖项。

   javax.el el-api 2.2 test    javax.servlet jsp-api 2.0 test  

通过这个修复我的测试也抛出了一个错误,他们无法找到.tld文件的映射,即使它在你添加spring-security-taglibs maven依赖项时包含它。

 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] 

为了解决这个问题,我还必须将security.tld添加到WEB-INF / lib /中。 现在一切正常,但我需要在两个地方安装标签库security.tld(META-INF和WEB-INF / lib下的spring-security-taglibs-3.1.3.RELEASE.jar)并不好。

我的问题是有没有人知道如何避免重复使用security.tld在两个地方只是为了能够运行unit testing,我宁愿把它包含在jar文件中而不是将它添加到WEB-INF / lib ?