2016-07-21 82 views
2

嘗試包括一堆HTML的是SSP模板,我有以下異常:Scalate模板大小限制爲64k?

Caused by: java.lang.IllegalArgumentException: null 
at scala.tools.asm.ByteVector.putUTF8(ByteVector.java:213) 
at scala.tools.asm.ClassWriter.newUTF8(ClassWriter.java:1092) 
at scala.tools.asm.ClassWriter.newString(ClassWriter.java:1525) 
at scala.tools.asm.ClassWriter.newConstItem(ClassWriter.java:1042) 
at scala.tools.asm.MethodWriter.visitLdcInsn(MethodWriter.java:1134) 
at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder.genConstant(GenASM.scala:1582) 
at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder.scala$tools$nsc$backend$jvm$GenASM$JPlainBuilder$$genInstr$1(GenASM.scala:2296) 
at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder$$anonfun$genBlock$1$2.apply(GenASM.scala:2227) 
at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder$$anonfun$genBlock$1$2.apply(GenASM.scala:2213) 
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33) 
at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186) 
at scala.tools.nsc.backend.icode.BasicBlocks$BasicBlock.foreach(BasicBlocks.scala:195) 
at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder.genBlock$1(GenASM.scala:2213) 
at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder.genBlocks$1(GenASM.scala:2151) 
at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder.genCode(GenASM.scala:2746) 
at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder.genMethod(GenASM.scala:1471) 
at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder.genClass(GenASM.scala:1341) 
at scala.tools.nsc.backend.jvm.GenASM$AsmPhase.emitFor$1(GenASM.scala:198) 
at scala.tools.nsc.backend.jvm.GenASM$AsmPhase.run(GenASM.scala:204) 
at scala.tools.nsc.Global$Run.compileUnitsInternal(Global.scala:1501) 
at scala.tools.nsc.Global$Run.compileUnits(Global.scala:1486) 
at scala.tools.nsc.Global$Run.compileSources(Global.scala:1481) 
at scala.tools.nsc.Global$Run.compile(Global.scala:1582) 
at org.fusesource.scalate.support.ScalaCompiler.compile(ScalaCompiler.scala:100) 
at org.fusesource.scalate.TemplateEngine.compileAndLoad(TemplateEngine.scala:757) 

看着scala.tools.asm.ByteVector我發現以下幾點:

public ByteVector putUTF8(String s) { 
    int charLength = s.length(); 
    if(charLength > '\uffff') { 
     throw new IllegalArgumentException(); 
    } else { .... 

如果模板尺寸更比65536字節,它無法編譯此模板。如何將大的一塊純HTML包含到SSP模板中? Include似乎只適用於模板文件。我應該手動加載和輸出模板中的HTML文件?有沒有更好的方法?

+0

你找到一個解決辦法? –

+0

@FatihDonmez請看我的答案 –

回答

1

看來這是一個固有的限制。

解決方法:手動讀取文件,並輸出到模板 (是的,這是醜陋的,但工程):

<% include("header.ssp") %> 

<%@ val book:Integer %> 

<% val bookHtml = "./public/books/" + "book_" + book + ".html"%> 
<% val source = scala.io.Source.fromFile(bookHtml) 
val lines = try source.mkString finally source.close() 
%> 

${unescape(lines)} 

<% include("footer.ssp") %> 
+0

Yeap,有一些調試,我看到這是jvm的限制。 Scalate模板主要是生成類中的字符串文字。限制來自; CONSTANT_Utf8_info {u1標籤; u2長度; u1 bytes [length]; }。因此,我們將其更改爲Freemarker而不是Scalate :) –