आपके पास दो विकल्प:
- उपयोग वीं: utext - आसान सेटअप विकल्प है, लेकिन कठिन पढ़ने और याद
- एक कस्टम प्रोसेसर और बोली बनाने के लिए - और अधिक शामिल सेटअप, लेकिन आसान, अधिक पठनीय भविष्य का उपयोग।
विकल्प 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'),'<br />')}" ></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
मेरे लिए बिल्कुल सही समाधान! आपकी मदद के लिए धन्यवाद – Chettor
@Chettor मुझे यकीन नहीं है कि अगर उत्तर संशोधित है या नहीं, तो स्टैक ओवरफ्लो आपको अपडेट करता है, लेकिन मैंने कस्टम प्रोसेसर और बोली दृष्टिकोण के लिए एक उदाहरण कार्यान्वयन शामिल किया है। आशा करता हूँ की ये काम करेगा! –
यह मेरी मदद करेगा, धन्यवाद! –