gpt4 book ai didi

spring-mvc - 如何在 Spring/Junit 测试工具中引用 Freemarker 模板中的 jsp 标签

转载 作者:行者123 更新时间:2023-12-03 15:58:54 26 4
gpt4 key购买 nike

我在解析 Spring 测试工具内的 Freemarker 模板中的 JSP 标记时遇到问题。

我的问题是:

为什么 JSP 引用没有在 JUnit 环境中解析?我该如何让他们解决?

模板中的代码是:

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

这两个导入在 Tomcat 环境中都可以正常工作,并且类路径具有正确的 jar 集,至少据我所知:

这些是从测试运行顶部获取的相关类路径条目。

:/Users/.m2/repository/org/springframework/security/spring-security-core/3.2.0.RELEASE/spring-security-core-3.2.0.RELEASE.jar

:/Users/.m2/repository/org/springframework/security/spring-security-web/3.2.0.RELEASE/spring-security-web-3.2.0.RELEASE.jar

:/Users/.m2/repository/org/springframework/security/spring-security-config/3.2.0.RELEASE/spring-security-config-3.2.0.RELEASE.jar

:/Users/.m2/repository/org/springframework/security/spring-security-taglibs/3.2.0.RELEASE/spring-security-taglibs-3.2.0.RELEASE.jar

:/Users/.m2/repository/org/springframework/security/spring-security-acl/3.2.0.RELEASE/spring-security-acl-3.2.0.RELEASE.jar

:/Users/.m2/repository/org/springframework/spring-webmvc/4.1.4.RELEASE/spring-webmvc-4.1.4.RELEASE.jar

导致问题的不是某个特定的库,如果我注释掉安全标签,springJSP 的下一次导入也会失败。

异常堆栈跟踪:

Error executing FreeMarker template freemarker.template.TemplateModelException: No mapping defined for http://www.springframework.org/security/tags
==> #assign sec = JspTaglibs["http://www... [in template "admin/layouts/admin_layouts.ftl" at line 1, column 1]
at freemarker.ext.jsp.TaglibFactory.get(TaglibFactory.java:179) ~[freemarker-2.3.20.jar:2.3.20]
at freemarker.core.DynamicKeyName.dealWithStringKey(DynamicKeyName.java:151) ~[freemarker-2.3.20.jar:2.3.20]
at freemarker.core.DynamicKeyName._eval(DynamicKeyName.java:108) ~[freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Expression.eval(Expression.java:111) ~[freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Assignment.accept(Assignment.java:106) ~[freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.visit(Environment.java:265) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.MixedContent.accept(MixedContent.java:93) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.visit(Environment.java:265) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.include(Environment.java:1712) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.importLib(Environment.java:1784) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.LibraryLoad.accept(LibraryLoad.java:121) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.visit(Environment.java:265) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.MixedContent.accept(MixedContent.java:93) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.visit(Environment.java:265) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.include(Environment.java:1712) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Include.accept(Include.java:172) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.visit(Environment.java:265) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.MixedContent.accept(MixedContent.java:93) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.visitByHiddingParent(Environment.java:286) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.visitAndTransform(Environment.java:377) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.CompressedBlock.accept(CompressedBlock.java:73) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.visit(Environment.java:265) [freemarker-2.3.20.jar:2.3.20]
at freemarker.core.Environment.process(Environment.java:243) [freemarker-2.3.20.jar:2.3.20]
at freemarker.template.Template.process(Template.java:277) [freemarker-2.3.20.jar:2.3.20]
at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:366) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:283) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:233) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.test.web.servlet.TestDispatcherServlet.render(TestDispatcherServlet.java:105) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) [servlet-api.jar:?]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) [servlet-api.jar:?]
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:144) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at com.ideafactory.mvc.catalog.ProductControllerTest.testProductsNew(ProductControllerTest.java:67) [test-classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_45]
at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_45]
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) [junit-4.11.jar:?]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.11.jar:?]
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) [junit-4.11.jar:?]
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.11.jar:?]
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) [junit-4.11.jar:?]
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) [junit-4.11.jar:?]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) [junit-4.11.jar:?]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) [junit-4.11.jar:?]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) [junit-4.11.jar:?]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) [junit-4.11.jar:?]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) [junit-4.11.jar:?]
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:309) [junit-4.11.jar:?]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163) [spring-test-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.junit.runner.JUnitCore.run(JUnitCore.java:160) [junit-4.11.jar:?]
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) [junit-rt.jar:?]
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) [junit-rt.jar:?]
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) [junit-rt.jar:?]

编辑和更新

此处提供更多信息:https://theodoreyoung.wordpress.com/2011/02/19/freemarker-jsp-taglibs-outside-war/#more-227

并不是说它有多大帮助。当 tld 不在 Web 模块中时,我如何解决它?单元测试的类路径上没有 WEB-INF/,只是包含了一堆 jar 文件,因为 IDE 使用模块类路径。

两天来我一直在努力让它工作,因为我无法执行单个测试!!

最佳答案

我正在使用 Freemarker 解决与 Spring 引导的依赖关系。我通过添加此依赖项解决了问题:

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

在确保您的项目处于war 模式并且所有依赖项都已解决之前:

dependency:resolve

记住它不是在 Java 代码中调用的。

关于spring-mvc - 如何在 Spring/Junit 测试工具中引用 Freemarker 模板中的 jsp 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com