2012-03-07 77 views
0

我已保存在Oracle數據庫中的BLOB字段,下面的代碼基本上HTML和用SQL嵌入的Groovy代碼,返回一些值渲染BLOB內容爲代碼,而不是文本

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

現在我有在變量thistemplate.contents,而我試圖對renderthistemplate.gsp呈現如下圖所示上述BLOB值:

def renderString= g.render(template:"myController/renderthistemplate",model:[rendertemplate:thistemplate.contents]) 

現在在普惠制我只是做${rendertemplate}打印整個BLOB。 預期它應該執行整個blob作爲代碼並呈現輸出。 但它實際上將blob的內容作爲普通字符串打印,無論是HTML還是Groovy代碼都在目標GSP中執行。 怎樣纔可以有目標GSP實際呈現的BLOB內容的代碼,而不是作爲字符串/文本 感謝 Priyank

回答

1

你不能。出於安全原因,字符串中的特殊符號必須轉義或者黑客可以輸入以下名稱:me'; delete from user;只要您在網頁上顯示名稱,就會刪除用戶表中的所有數據(簡化示例,但您明白了)。

你可以render the blob as text使用contentType:"text/html"但不會運行<% %>塊中的代碼。

解決方案是創建您自己的動態模板。您需要兩兩件事是:

  1. 在控制器中創建一個新的領域:

    GroovyPagesTemplateEngine groovyPagesTemplateEngine 
    
  2. 使用此代碼呈現斑點:

    groovyPagesTemplateEngine.createTemplate(blobAsString, 「somepage.gsp」).make(model).writeTo(out) 
    

代碼示例來自:Grails – Rendering a Template from a String

+0

感謝fo提示和參考,我們會試試這個! – 2012-03-07 09:59:41

+0

只是有一個問題,這個調用會每次創建一個新的模板gsp ..取決於blobasString – 2012-03-07 10:16:18

+0

當然;它不是一個完整的GSP,而是從'.gsp'文件構建模板的代碼。還應該如何編譯和運行blob中的Groovy代碼?如果這是性能問題,我建議您在啓動應用程序時創建一次模板,然後在該共享模板上調用'make()'。 – 2012-03-07 17:19:19