在Grails中渲染HTML文件 [英] Rendering HTML files in Grails

查看:159
本文介绍了在Grails中渲染HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,但找不到在Grails中简单包含或渲染* .html文件的方法。我的应用程序需要以文件形式发布的 g.render < g:render> 模板。为此,正如我们所知,html文件必须转换为 _foo.gsp 文件才能呈现。我为什么没有直接支持html或者是否有一个?我非常惊讶。

I've looked around but could not find a way of simply including or rendering *.html files in Grails. My application needs to g.render or <g:render> templates which are delivered as html files. For this, as we know, html files have to be converted to _foo.gsp files in order to get rendered. I am totally surprised as to why isn't there a direct support for html or is there one??

谢谢!

推荐答案

一个明显的选择是简单地将您的HTML文件从 foo.html 重命名为 _foo。 gsp ,然后使用< render template =foo> 。但是,这很明显,我相信你已经想到了它。

One obvious option is to simply rename your HTML files from foo.html to _foo.gsp and then use <render template="foo">. However this is so obvious that I'm sure you've already thought of it.

如果你只是想在控制器中渲染一个HTML文件,你可以使用 text render的参数 控制器方法

If you simply want to render a HTML file from within a controller you can use the text parameter of the render controller method

def htmlContent = new File('/bar/foo.html').text
render text: htmlContent, contentType:"text/html", encoding:"UTF-8"

如果你想在.gsp中做同样的事情,你可以写一个标签。像下面这样(未经测试)应该可以工作:

If you want to do the same thing from within a .gsp, you could write a tag. Something like the following (untested) should work:

import org.springframework.web.context.request.RequestContextHolder

class HtmlTagLib {

  static namespace = 'html'

  def render = {attrs ->

    def filePath = attrs.file

    if (!file) {
      throwTagError("'file' attribute must be provided")
    }

    def htmlContent = new File(filePath).text
    out << htmlContent
  }
}

您可以使用

<html:render file="/bar/foo.html"/>

这篇关于在Grails中渲染HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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