2016-06-11 243 views
0

this question的变体中,我想使用Thymeleaf将多行字符串呈现到HTML表格中。呈现Thymeleaf中的多行字符串

也就是说,我怎么转换像

Cronut fixie tousled migas. 
Whatever neutra offal fanny pack, photo booth kitsch bespoke hammock swag. 
Keffiyeh yuccie meditation mustache cornhole paleo. 

字符串转换成

<table class="table"> 
    <tr> 
     <td>Cronut fixie tousled migas.</td> 
    </tr> 
    <tr> 
     <td>Whatever neutra offal fanny pack, photo booth kitsch bespoke hammock swag.</td> 
    </tr> 
    <tr> 
     <td>Keffiyeh yuccie meditation mustache cornhole paleo.</td> 
    </tr> 
</table> 

回答

0

与包含在模型中的变量model.text,一个可能的解决方案(使用Spring的方言时)的字符串这样的:

<table> 
    <tr th:each="line : ${#strings.arraySplit(model.text, T(org.apache.commons.lang3.StringUtils).LF)}"> 
     <td th:text="${line}"></td> 
    </tr> 
</table> 

之所以使用T(org.apache.commons.lang3.StringUtils).LF代替只是\n是(也在this answer中描述)SpEL转义反斜杠,然后决定拆分'n'字母而不是换行符。

当然,还应该考虑直接在控制器中将字符串拆分为数组的解决方案(即使用普通Java)。

1

我原来在前面的SO question上发布了这个答案,但是这个也似乎弹出来了。我已经修改它来输出一个表格而不是仅仅换行符。

可以使用自定义方言和属性处理器来处理此操作,而不需要大量内联SpEl或黑客。

创建自定义属性处理器

public class NewlineAttrProcessor extends AbstractUnescapedTextChildModifierAttrProcessor 
{ 
    public NewlineAttrProcessor() 
    { 
     super("nl2br"); 
    } 

    @Override 
    protected String getText(Arguments arguments, Element element, String attributeName) 
    { 
     final Configuration configuration = arguments.getConfiguration(); 

     final IStandardExpressionParser parser = 
      StandardExpressions.getExpressionParser(configuration); 

     final String attributeValue = element.getAttributeValue(attributeName); 

     final IStandardExpression expression = 
      parser.parseExpression(configuration, arguments, attributeValue); 

     final String value = (String)expression.execute(configuration, arguments); 
     final String[] lines = StringUtils.split(value, "\n"); 

     return "<table><td>" + StringUtils.join(lines, "</td><td>") + "</td></table>"; 
    } 

    @Override 
    public int getPrecedence() 
    { 
     return 10000; 
    } 
} 

您必须扩展AbstractUnescapedTextChildModifierAttrProcessor处理器,否则你会得到<table>...</table>的HTML实体标签,你不会真正得到的HTML。

创建自定义方言

要实现你需要一个方言类,像这样一个自定义的话:

public class MyCustomDialect extends AbstractDialect 
{ 
    @Override 
    public String getPrefix() 
    { 
     return "cd"; 
    } 

    @Override 
    public Set<IProcessor> getProcessors() 
    { 
     final Set<IProcessor> processors = new HashSet<>(); 
     processors.add(new NewlineAttrProcessor()); 
     return processors; 
    } 
} 

getPrefix方法的返回值是你会用什么来调用任何定制处理器是你做。例如,Thymeleaf使用th。我们上面实现的定制处理器正在寻找nl2br,所以要调用它,您将使用cd:nl2br属性而不是th:text

注册新方言

在主类中,你只需要创建一个@Bean将返回你的方言类的新实例。

@SpringBootApplication 
public class MyApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(MyApplication.class, args); 
    } 

    @Bean 
    public MyCustomDialect myCustomDialect() 
    { 
     return new MyCustomDialect(); 
    } 
} 

使用定制处理器

最后,在你的模板文件,你将有一个HTML标签是这样的:

<div cd:nl2br="${myObject.myField}">MY MULTILINE FIELD</div>

有关更详尽的指导,实现自定义的方言我推荐Thymeleaf文档: