小胡子和 Internalviewresolvers - Spring MVC

mustache and Internalviewresolvers - Spring MVC

我正在使用 spring mvc 项目试用小胡子模板。我有一些使用 JSPs 的代码,我只想为某些页面添加小胡子模板。为此,我添加了一个新的 mustacheViewResolver 和我现有的 InternalResourceViewResolver,如下所示。

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/jsp/");
    viewResolver.setSuffix(".jsp");
    viewResolver.setOrder(2);
    return viewResolver;
}

@Bean
public ViewResolver mustacheViewResolver() {
    ScriptTemplateViewResolver viewResolver = new ScriptTemplateViewResolver();
    viewResolver.setPrefix("/static/templates/");
    viewResolver.setSuffix(".html");
    viewResolver.setOrder(1);
    return viewResolver;
}

只有其中一个有效,但不能同时有效。当打算去userform.jsp时,它会去userform.html。

[WARNING] /myproject/users/add
java.io.FileNotFoundException: class path resource [static/templates/users/userform.html] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
    at org.springframework.web.servlet.view.script.ScriptTemplateView.getTemplate(ScriptTemplateView.java:369)
    at org.springframework.web.servlet.view.script.ScriptTemplateView.renderMergedOutputModel(ScriptTemplateView.java:349)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:499)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
    at org.eclipse.jetty.io.AbstractConnection.run(AbstractConnection.java:540)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Thread.java:745)

关于如何同时使用模板和 JSP 视图解析器的任何线索?

由于 InternalResourceViewResolver 在层次结构中实现 ViewResolver,您需要从第二个解析器 return ScriptTemplateViewResolver 让 spring 知道两者不同解析器。

ScriptTemplateView 没有正确检查模板文件是否可用,根据它的 Javadoc 应该是这样。这是一个错误 - 检查 SPR-14729.

请升级至相关Spring版本。

请注意,您还应该保持视图解析器这样排序,因为与其他模板引擎不同,InternalResourceViewResolver 和 JSP 无法检查模板是否存在并且确实会抛出异常,这就是它们经常出现的原因配置在链的末端。