Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java velocity模板引擎1.7换行问题_Java_Templates_Velocity - Fatal编程技术网

Java velocity模板引擎1.7换行问题

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

我有一个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($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指令引入的换行符,可以使用
##
结束该行: