2015-05-22 7 views
16

मैं वसंत के साथ थाइमेलीफ टेम्पलेट इंजन का उपयोग कर रहा हूं और मैं एक मल्टीलाइन टेक्स्टरेरा के माध्यम से संग्रहीत टेक्स्ट प्रदर्शित करना चाहता हूं।थाइमेलीफ + वसंत: लाइन ब्रेक कैसे रखें?

मेरी डेटाबेस बहु स्ट्रिंग में के साथ "\ n" इस तरह की दुकान कर रहे हैं: "Test1 \ nTest2 \ N ...."

वें के साथ

: पाठ मैं मिल गया है: "Test1 Test2" कोई लाइन के साथ टूटना।

मैं थूमेलीफ का उपयोग करके लाइन ब्रेक कैसे प्रदर्शित कर सकता हूं और < br /> के साथ मैन्युअल रूप से \ n "प्रतिस्थापित कर सकता हूं और फिर th: utext (xss इंजेक्शन के लिए यह खुला फॉर्म) का उपयोग करने से बचें?

धन्यवाद!

उत्तर

18

आपके पास दो विकल्प:

  1. उपयोग वीं: utext - आसान सेटअप विकल्प है, लेकिन कठिन पढ़ने और याद
  2. एक कस्टम प्रोसेसर और बोली बनाने के लिए - और अधिक शामिल सेटअप, लेकिन आसान, अधिक पठनीय भविष्य का उपयोग।

विकल्प 1:

आप वें उपयोग कर सकते हैं: utext अगर आप अभिव्यक्ति उपयोगिता विधि #strings.escapeXml(text) का उपयोग कर XSS इंजेक्शन और अवांछित स्वरूपण को रोकने के लिए पाठ से बच - http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#strings

इस मंच स्वतंत्र बनाने के लिए , आप पंक्ति विभाजक को पकड़ने के लिए T(java.lang.System).getProperty('line.separator') का उपयोग कर सकते हैं।

मौजूदा Thymeleaf अभिव्यक्ति उपयोगिताओं का उपयोग करना, यह काम करता है:

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

विकल्प 2:

सेटअप पूरा होने, तुम सब करने की जरूरत होगी संरक्षित पंक्ति विराम के साथ भाग textline उत्पादन पूरा करने के लिए :

<p david:lstext="${ text }"></p> 

काम करने वाला मुख्य टुकड़ा प्रोसेसर है। निम्नलिखित कोड चाल करना होगा:

package com.davidjanney.foo.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; 
    } 


} 

अब आप प्रोसेसर है तो आप उसे करने के लिए प्रोसेसर को जोड़ने के लिए एक कस्टम बोली की जरूरत है।इसलिए

आप एक वसंत MVC आवेदन लिख रहे हैं, तो आप सिर्फ खाका इंजन सेम के additionalDialects संपत्ति में यह स्थापित करने के लिए,:

package com.davidjanney.foo.thymeleaf.dialects; 

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

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

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

public class DavidDialect extends AbstractDialect{ 

    public DavidDialect(){ 
     super(); 
    } 

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

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

} 

अब आप अपने xml या जावा विन्यास में जोड़ने के लिए की जरूरत है कि यह डिफ़ॉल्ट SpringStandard बोली में जोड़ा जाता है:

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"> 
    <property name="templateResolver" ref="templateResolver" /> 
    <property name="additionalDialects"> 
    <set> 
     <bean class="com.davidjanney.foo.thymeleaf.dialects.DavidDialect"/> 
    </set> 
    </property> 
    </bean> 

या आप वसंत का उपयोग कर रहे हैं और नहीं बल्कि JavaConfig का प्रयोग करेंगे आप एक वर्ग अपने आधार पैकेज में @Configuration साथ एनोटेट कि किसी प्रबंधित सेम के रूप में बोली बना सकते हैं यदि:

package com.davidjanney.foo; 

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

import com.davidjanney.foo.thymeleaf.dialects.DavidDialect; 

@Configuration 
public class TemplatingConfig { 

    @Bean 
    public DavidDialect davidDialect(){ 
     return new DavidDialect(); 
    } 
} 

यहां कस्टम प्रोसेसर और बोलियों बनाने पर कुछ आगे के संदर्भ हैं: http://www.thymeleaf.org/doc/articles/sayhelloextendingthymeleaf5minutes.html, http://www.thymeleaf.org/doc/articles/sayhelloagainextendingthymeleafevenmore5minutes.html और http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html

+0

मेरे लिए बिल्कुल सही समाधान! आपकी मदद के लिए धन्यवाद – Chettor

+1

@Chettor मुझे यकीन नहीं है कि अगर उत्तर संशोधित है या नहीं, तो स्टैक ओवरफ्लो आपको अपडेट करता है, लेकिन मैंने कस्टम प्रोसेसर और बोली दृष्टिकोण के लिए एक उदाहरण कार्यान्वयन शामिल किया है। आशा करता हूँ की ये काम करेगा! –

+0

यह मेरी मदद करेगा, धन्यवाद! –

0

प्रयास करें इस

<p th:utext="${#strings.replace(#strings.escapeJava(description),'\n','&lt;br /&gt;')}" ></p> 
+0

यह पास सही (। एचटीएमएल 5 शैली Thymeleaf का उपयोग करना)! धन्यवाद लाइनब्रेक सही तरीके से प्रदर्शित होते हैं लेकिन जब उपयोगकर्ता टाइप से बच निकले चरित्र "या" यह \ "या \ '(बैकस्लैश के साथ) दिखाता है। क्या इसे रोकने का कोई तरीका है? – Chettor

3

मेरे मामले escapeJava() सिरिलिक प्रतीकों के लिए यूनिकोड मान रिटर्न में, तो मैं हल करने के लिए unescapeJava() विधि मदद में सभी लपेट मेरी मुसीबत।

<div class="text" th:utext="${#strings.unescapeJava(#strings.replace(#strings.escapeJava(comment.text),'\n','&lt;br /&gt;'))}"></div> 
4

क्या हो सकता है कि नहीं ओपी मन में था, लेकिन यह काम करता है और कोड इंजेक्शन से बचाता है:

<p data-th-utext="${#strings.replace(#strings.escapeXml(text),'&#10;','&lt;br&gt;')}"></p> 

संबंधित मुद्दे