Rendering HTML files in Grails

≡放荡痞女 提交于 2019-11-27 16:35:34

问题


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??

Thanks!


回答1:


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.

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"

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
  }
}

You can call this tag from a GSP using

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



回答2:


What is it you are trying to accomplish?

  1. Render html from a controller? In that case, all you should have to do is redirect the user to file from your control.

    redirect(uri:"/html/my.html")

  2. Use html-files instead of gsp template-files? Thing is, Grails is a "Convention over Configuration"-platform and that means you will have to do some things "the Grails way". The files needs the _ and the .gsp but the name can be whatever you like even if it's easier when you use the same name as the controller. What you gain from doing that is the knowledge that every developer that knows grails and comes into your project will understand how things are tied together and that will help them get started quickly.




回答3:


Little bit fixed the Don's example, works fine for me

import org.apache.commons.io.IOUtils

class HtmlTagLib {

    static namespace = 'html'

    def render = {attrs ->

        def filePath = attrs.file

        if (!filePath) {
            throwTagError("'file' attribute must be provided")
        }
        IOUtils.copy(request.servletContext.getResourceAsStream(filePath), out);
    }
}



回答4:


I wanted to write static html/ajax pages hosted in grails app (v2.4.4), but use the controller for the url rewrite. I was able to accomplish this by moving the file to web-app/ (for ease of reference), and simply use the render() method with 'file' and 'contentType' params, such as:

// My controller action
def tmp() {
    render file: 'web-app/tmp.html', contentType: 'text/html'
}

Note: I only tried this using run-app, and haven't packaged a war and deployed to tomcat, yet.

Doc: http://grails.github.io/grails-doc/2.4.4/ref/Controllers/render.html




回答5:


  Closure include = { attr ->
        out << Holders.getServletContext().getResource(attr.file).getContent();
    }

It is relative to the web-app folder of your grails main application




回答6:


I was able to use @momo's approach to allow inclusion of external files for grails rendering plugin, where network paths would get botched in higher environments - mine ended up like:

def includeFile = { attr ->
    URL resource = Holders.getServletContext().getResource(attr.file)
    InputStream content = resource.getContent()
    String text = content?.text
    log.info "includeFile($attr) - resource: $resource, content.text.size(): ${text?.size()}"
    out << text
}


来源:https://stackoverflow.com/questions/2281212/rendering-html-files-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!