Java velocity模板引擎1.7换行问题
我有一个velocity模板,它生成了一个Java类,其中包含大量的换行符,但这种情况并不总是发生 我的模板看起来像这样Java velocity模板引擎1.7换行问题,java,templates,velocity,Java,Templates,Velocity,我有一个velocity模板,它生成了一个Java类,其中包含大量的换行符,但这种情况并不总是发生 我的模板看起来像这样 #foreach( $str in $elm.mylist ) #if($selected == $str.how) #if($str.how == "value") //some comment #end #if($str.how == "value") //some comment #end #if($str.how == "value") #if($fo
#foreach( $str in $elm.mylist )
#if($selected == $str.how)
#if($str.how == "value")
//some comment
#end
#if($str.how == "value")
//some comment
#end
#if($str.how == "value")
#if($foreach.index == 0 || $foreach.index == 1)
@CustomAnnotation(value = "$!str.value")
#else
//@CustomAnnotation(value = "$!str.value")
#end
#else
@CustomAnnotation(value = "$!str.value")
#end
#end
#end
//some comment
//@CustomAnnotation(value = "sample value
")
//@CustomAnnotation(value = "no value found")
//@CustomAnnotation(value = "change this")
@CustomAnnotation(value = "no value found")
public MyClass varibleName;
//some comment
//@CustomAnnotation(value = "sample value
")
//@CustomAnnotation(value = "no value found")
//@CustomAnnotation(value = "change this")
@CustomAnnotation(value = "no value found")
public MyClass varibleName_1;
生成的java文件如下所示
#foreach( $str in $elm.mylist )
#if($selected == $str.how)
#if($str.how == "value")
//some comment
#end
#if($str.how == "value")
//some comment
#end
#if($str.how == "value")
#if($foreach.index == 0 || $foreach.index == 1)
@CustomAnnotation(value = "$!str.value")
#else
//@CustomAnnotation(value = "$!str.value")
#end
#else
@CustomAnnotation(value = "$!str.value")
#end
#end
#end
//some comment
//@CustomAnnotation(value = "sample value
")
//@CustomAnnotation(value = "no value found")
//@CustomAnnotation(value = "change this")
@CustomAnnotation(value = "no value found")
public MyClass varibleName;
//some comment
//@CustomAnnotation(value = "sample value
")
//@CustomAnnotation(value = "no value found")
//@CustomAnnotation(value = "change this")
@CustomAnnotation(value = "no value found")
public MyClass varibleName_1;
如何避免这种新的换行?在
示例值
之后的换行符肯定包含在$str.value
字符串中。因此,您可以执行以下操作:
$!str.value.trim()
要移除它们
顺便说一句,如果您想消除Velocity指令引入的换行符,可以使用##
结束该行: