PlayFrameworkのViewでHTMLエスケープをさせない方法
この方法が正しいかどうかはわからないけど一応メモ。
下のような形でHTMLを出力させるメソッドがあったとして…
@Entity public class MyModel extends Model { @Transient public final static HashMap typeMap = new HashMap(){{ put(0, "Type1"); put(1, "Type2"); }}; public static String getTypeOptions(){ StringBuffer sb = new StringBuffer(); for(Iterator it = typeMap.entrySet().iterator(); it.hasNext();){ Map.Entry entry = (Map.Entry)it.next(); Integer key = (Integer)entry.getKey(); String value = (String)entry.getValue(); sb.append("<option value='"+key+"'>"+value+"</option>\n"); } return sb.toString(); } }
このままviewで出力するとHTMLがエスケープされてしまうため、raw()を使うことでそのままの文字列を出力できる。
<select id="modelType">${models.MyModel.getTypeOptions().raw()}</select>
https://groups.google.com/forum/?fromgroups=#!topic/play-framework/9xF7Gq7sWKA
http://www.playframework.org/documentation/1.0.1/releasenotes