当前位置: 首页 > 知识库问答 >
问题:

沉香+Spring:如何保持断线?

莘欣怿
2023-03-14

我使用Thymeleaf模板引擎与spring和我想显示文本存储通过一个多行textarea。

在我的数据库中,多行字符串是用“\n”存储的,如下所示:“test1\ntest2\n....”

对于th:text,我得到了:“test1 test2”,没有换行符。

共有1个答案

秦承允
2023-03-14

你有两个选择:

  1. 使用th:utext-简单的设置选项,但更难阅读和记住
  2. 创建自定义处理器和方言-更多涉及的设置,但更容易,更易读的未来使用。

备选办法1:

<p th:utext="${#strings.replace( #strings.escapeXml( text ),T(java.lang.System).getProperty('line.separator'),'&lt;br /&gt;')}" ></p>
<p fd:lstext="${ text }"></p>
package com.foo.bar.thymeleaf.processors 

import java.util.Collections;
import java.util.List;

import org.thymeleaf.Arguments;
import org.thymeleaf.Configuration;
import org.thymeleaf.dom.Element;
import org.thymeleaf.dom.Node;
import org.thymeleaf.dom.Text;
import org.thymeleaf.processor.attr.AbstractChildrenModifierAttrProcessor;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
import org.thymeleaf.standard.expression.StandardExpressions;
import org.unbescape.html.HtmlEscape;

public class HtmlEscapedWithLineSeparatorsProcessor extends
        AbstractChildrenModifierAttrProcessor{

    public HtmlEscapedWithLineSeparatorsProcessor(){
        //only executes this processor for the attribute 'lstext'
        super("lstext");
    }

    protected String getText( final Arguments arguments, final Element element,
            final 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);

        //return the escaped text with the line separator replaced with <br />
        return HtmlEscape.escapeHtml4Xml( value ).replace( System.getProperty("line.separator"), "<br />" );


    }



    @Override
    protected final List<Node> getModifiedChildren(
            final Arguments arguments, final Element element, final String attributeName) {

        final String text = getText(arguments, element, attributeName);
        //Create new text node signifying that content is already escaped.
        final Text newNode = new Text(text == null? "" : text, null, null, true);
        // Setting this allows avoiding text inliners processing already generated text,
        // which in turn avoids code injection.
        newNode.setProcessable( false );

        return Collections.singletonList((Node)newNode);


    }

    @Override
    public int getPrecedence() {
        // A value of 10000 is higher than any attribute in the SpringStandard dialect. So this attribute will execute after all other attributes from that dialect, if in the same tag.
        return 11400;
    }


}

既然有了处理器,就需要一个自定义方言来添加处理器。

package com.foo.bar.thymeleaf.dialects;

import java.util.HashSet;
import java.util.Set;

import org.thymeleaf.dialect.AbstractDialect;
import org.thymeleaf.processor.IProcessor;

import com.foo.bar.thymeleaf.processors.HtmlEscapedWithLineSeparatorsProcessor;

public class FooDialect extends AbstractDialect{

    public FooDialect(){
        super();
    }

    //This is what all the dialect's attributes/tags will start with. So like.. fd:lstext="Hi David!<br />This is so much easier..."
    public String getPrefix(){
        return "fd";
    }

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

}

现在需要将其添加到xml或java配置中:

如果您正在编写一个Spring MVC应用程序,只需在模板引擎bean的additionalDialects属性中设置它,以便将其添加到默认的SpringStandard方言中:

    <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
  <property name="templateResolver" ref="templateResolver" />
  <property name="additionalDialects">
    <set>
      <bean class="com.foo.bar.thymeleaf.dialects.FooDialect"/>
    </set>
  </property>
    </bean>
package com.foo.bar;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.foo.bar.thymeleaf.dialects.FooDialect;

@Configuration
public class TemplatingConfig {

    @Bean
    public FooDialect fooDialect(){
        return new FooDialect();
    }
}
 类似资料:
  • 我需要创建一个新任务。但当我点击“保存按钮”时,什么都没有。操作窗体(“@{/add}”)中的URL等于控制器中的URL,但方法不调用。 请帮帮忙。谢谢!

  • 我是thymeleaf的新手,在执行if条件时遇到了问题。我编写的代码是 问题是这段代码写完后,web页面会不断加载。谁能告诉我执行这个操作的正确方法。

  • 当测试/调试Java应用程序时,如何将断点放在某个被抓住的地方,而不挂起整个应用程序? 我有一个简单的Spring Boot应用程序,当我在我的IntelliJ社区集成开发环境中的某个地方设置断点时,整个应用程序会挂起。 我如何允许应用程序运行,即使我持有一个线程? 我能从两个试图同时登录的用户那里同时获得两个断点吗?

  • 问题内容: 我正在使用POST方法。我需要创建一次,并且应该使用Keep Alive Connection。但是我认为,它每次都会建立一个新的连接。 因此,我需要使用 保持活动 连接。 这是我的代码段,很多帮助将不胜感激。 而且logcat日志是: 问题答案: 10:07:29.746:D / org.apache.http.headers(1529):>>连接:保持活动 您正在要求保持活动状态。

  • 我的短信有点小问题。在文本的某些部分,我使用了' '来确保该特定单词不会中断(或移动)到新行。举个例子,我做了这样的事情: 所以从理论上讲,这个文本('-is')不应该以一个单词的形式出现在一个新的行中,对吧?我想那是对的。但是...这个技巧给我带来了一个问题,就是在“-”和“is”之间增加了很大的空格...而且我当然不想有那个“大空位”。我将给出结果示例: 如你所见,有一个巨大的空间。所以,我的

  • 我使用spring boot 1.5.6版本和thymeleaf 3.0.0。 我有一个控制器,很简单: start应用程序如下: 当我运行该应用程序时,我将http://localhost:8080放在chrome地址栏中。我得到的是你好世界。所以我想spring boot&thymeleaf模板解析器可以找到index.html文件。但是根本不解析和。我通过chrome开发工具检查了页面的源代