İki seçeneğiniz vardır deneyin:
- Kullanım inci: utext - okumak ve
- özel işlemci ve lehçesi oluştur hatırlaması kolay kurulum seçeneği, ama zor - daha ilgili kurulum, ancak daha kolay, daha okunabilir gelecek kullanımı.
Seçenek 1:
Sen th kullanabilirsiniz: utext Eğer XSS enjeksiyonu ve istenmeyen biçimlendirme önlemek için ifade yarar yöntemini #strings.escapeXml(text)
kullanarak metin kaçış eğer - http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#strings
Bu platform bağımsız hale getirmek için satır ayırıcısını almak için T(java.lang.System).getProperty('line.separator')
'u kullanabilirsiniz.
mevcut Thymeleaf ifade programlar kullanan bu işleri:
<p th:utext="${#strings.replace(#strings.escapeXml(text),T(java.lang.System).getProperty('line.separator'),'<br />')}" ></p>
Seçenek 2:
kurulumu tamamlandıktan sonra yapmanız gereken tüm korunmuş satır sonları ile kaçan TextLine çıkışını gerçekleştirmek için :
<p david:lstext="${ text }"></p>
Bu işi yapan ana parça işlemcidir. Aşağıdaki kod hile olacaktır: Artık işlemci olduğunu
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;
}
}
yapmanız işlemci eklemek için özel bir lehçesi gerekir.böylece, bir bahar MVC uygulama yazıyorsanız
, sadece Şablon Motor fasulye additionalDialects tesiste ayarlamak zorunda:
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;
}
}
Artık xml veya java yapılandırmasına eklemeye gerek varsayılan SpringStandard lehçesi eklendiğini:
<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>
Yoksa bahar kullanıyor ve oldukça yönetilen bir fasulye olarak lehçesini içeren baz paketinde @Configuration ile açıklamalı bir sınıf oluşturabilir JavaConfig kullanmak olsaydı:İşte
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();
}
}
özel işlemciler ve lehçeyi oluşturmaya yönelik bazı başka referanslar şunlardır:
http://www.thymeleaf.org/doc/articles/sayhelloextendingthymeleaf5minutes.html,
http://www.thymeleaf.org/doc/articles/sayhelloagainextendingthymeleafevenmore5minutes.html ve kiril semboller için unicode değerleri döndürür
escapeJava()
Benim durumumda
http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html
Benim için mükemmel çözüm! Yardımınız için teşekkürler. – Chettor
@Chettor Yanıtı değiştirdiğinizde veya değiştirmediğinizde Stackoverflow'un sizi güncellediğinden emin değilim, ancak özel işlemci ve lehçe yaklaşımı için örnek bir uygulama ekledim. Umarım yardımcı olur! –
Bana yardımcı olacak, teşekkürler! –