Grails 将blob内容呈现为代码而不是文本

Grails 将blob内容呈现为代码而不是文本,grails,groovy,blob,render,Grails,Groovy,Blob,Render,我在oracle数据库中以blob字段的形式保存了以下代码,基本上是html和嵌入式groovy代码,其中包含返回一些值的sql <% import groovy.sql.Sql def sql = Sql.newInstance("jdbc:mysql://localhost:3306/myDB","root", "root", "com.mysql.jdbc.Driver") %> <html> <head&g

我在oracle数据库中以blob字段的形式保存了以下代码,基本上是html和嵌入式groovy代码,其中包含返回一些值的sql

<%
        import groovy.sql.Sql

        def sql = Sql.newInstance("jdbc:mysql://localhost:3306/myDB","root", "root", "com.mysql.jdbc.Driver")
%>

<html>
        <head>
                <title>Database Example</title>
        </head>
        <body>
                <table align="center" border="1">
                        <tr>
                                <td>Id</td>
                                <td>LastName</td>
                                <td>FirstName</td>
                        </tr>
                        <% sql.eachRow("select profile_id profileId, last_name lastName, first_name firstName from profile") {profile->  %>
                                <tr>
                                        <td>${profile.profileId}</td>
                                        <td>${profile.lastName}</td>
                                        <td>${profile.firstName}</td>
                                </tr>
                        <% } %>
                </table>
        </body>
</html>
现在在gsp中,我只需执行
${rendertemplate}
来打印整个blob。 它应该以代码的形式执行整个blob并呈现输出。 但它实际上是将blob的内容打印为普通字符串,而HTML和groovy代码都不会在目标GSP中执行。 如何让目标GSP将blob内容实际呈现为代码而不是字符串/文本 谢谢
普里扬克

你不能。出于安全原因,字符串中的特殊符号必须转义,否则黑客可能会输入以下名称:
me';从用户中删除
这将在您在网页上显示名称时立即删除用户表中的所有数据(简化的示例,但您明白了)

您可以使用
contentType:“text/html”
,但这不会在
块中运行代码

解决方案是创建自己的动态模板。为此,您需要两件事:

  • 在控制器中创建一个新字段:

    GroovyPagesTemplateEngine groovyPagesTemplateEngine
    
  • 使用以下代码渲染blob:

    groovyPagesTemplateEngine.createTemplate(blobAsString, “somepage.gsp”).make(model).writeTo(out)
    

  • 代码示例来自:

    感谢您的提示和参考,我们将尝试此功能!我有个问题,每次打电话都会有一个新的gsp模板吗。。取决于blobasStringSure;它不是完整的GSP,而是从
    .GSP
    文件构建模板的代码。它还应该如何从blob编译和运行Groovy代码?如果这是一个性能问题,我建议在启动应用程序时创建一次模板,然后在此共享模板上调用
    make()
    groovyPagesTemplateEngine.createTemplate(blobAsString, “somepage.gsp”).make(model).writeTo(out)