哪些Java MVC框架可以轻松地与StringTemplate集成? [英] Which Java MVC frameworks integrate easily with StringTemplate?

查看:118
本文介绍了哪些Java MVC框架可以轻松地与StringTemplate集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很难看出 StringTemplate 如何与流行的 Java Web MVC框架轻松集成(或不集成)。

It's hard to see how StringTemplate integrates easily (or not) with popular Java web MVC frameworks.

哪些Java MVC框架可以轻松地与StringTemplate集成?

一个很好的答案:


  • 提及一个解决方案与框架集成,

  • 包含一个有用且适用的链接 ,例如:


    • 教程

    • 文档

    • 或对源代码的引用:


      • 免费

      • 开源公共领域

      • mentions one solution to integrate with a framework,
      • includes a link to something useful and applicable, like:
        • a tutorial,
        • or documentation,
        • or a reference to source code:
          • free,
          • and open source or public domain.

          读者/选民,请投票选择解决方案你知道这是真的很棒。

          Readers/Voters, please vote for a solution if you know it's true and great.

          在这个问题的范围内,我对任何其他模板引擎不感兴趣比StringTemplate

          In the scope of this question, I am not interested in any other templating engine than StringTemplate.

          推荐答案

          我已经使用StringTemplate来使用Spring。基本上,它只需要一个自定义视图。

          I've gotten StringTemplate to work with Spring. Basically, all it took was a custom view.

          但首先,免责声明:这是一个实验性的黑客攻击。我从来没有在生产代码中使用它,它可以在发生之前使用一些改进。我认为回答你关于StringTemplate如何轻松地与Web MVC框架集成的问题就足够了。

          But first, a disclaimer: This is an experimental hack. I've never used this in production code, and it could use some improvement before that happens. I think it is adequate to answer your question about how easily StringTemplate integrates with a Web MVC framework, however.

          参考: Spring Web MVC文档

          StringTemplateView.java :

          StringTemplateView.java:

          import java.io.PrintWriter;
          import java.util.Map;
          
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          
          import org.antlr.stringtemplate.StringTemplate;
          import org.antlr.stringtemplate.StringTemplateGroup;
          import org.springframework.core.io.Resource;
          import org.springframework.web.servlet.view.InternalResourceView;
          
          public class StringTemplateView extends InternalResourceView {
          
              @Override
              protected void renderMergedOutputModel(Map model, HttpServletRequest request,
                      HttpServletResponse response) throws Exception {
          
                  // Provides a Spring resource descriptor referring to the .st file
                  Resource templateFile = getApplicationContext().getResource(getUrl());
          
                  // Kind of redundant...
                  StringTemplateGroup group = new StringTemplateGroup("group", templateFile.getFile().getParent());
                  StringTemplate template = group.getInstanceOf(getBeanName());
                  template.setAttributes(model);
          
                  // Output to client
                  PrintWriter writer = response.getWriter();
                  writer.print(template);
                  writer.flush();
                  writer.close();
              }
          }
          

          示例视图解析器定义:

          <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="viewClass" value="myapp.web.view.StringTemplateView"/>
              <property name="prefix" value="/WEB-INF/st-views/"/>
              <property name="suffix" value=".st"/>
          </bean>
          

          这篇关于哪些Java MVC框架可以轻松地与StringTemplate集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆