2

Spring Boot(1.4.1)とThymeleaf(3)でMessageSourceメッセージに関する質問があります。 page.htmlでSpring Boot(1.4.1)とThymeleaf(3)MessageSource一重引用符

ファイルapp.properties

app.quotes=Francesco's title 
app.quotes2=Francesco''s title 

私はメッセージ

<h2 th:text="#{app.quotes}"></h2> 
<h2 th:utext="#{app.quotes}"></h2> 
<h2 th:text="#{app.quotes2}"></h2> 
<h2 th:utext="#{app.quotes2}"></h2> 

を印刷するとき、私は(TH:テキストや目:のutextはどんな違いがありません)を正確に取得

  • フランチェスコのタイトル
  • フランチェスコのタイトル
  • フランチェスコ 'のタイトル
  • フランチェスコ' のタイトル私のコントローラで

private final Logger log = LoggerFactory.getLogger(this.getClass());  

@Autowired 
private MessageSource messages; 

@RequestMapping(value="/page", method = RequestMethod.GET) 
public String page() { 

    String text = messages.getMessage("app.quotes", null, LocaleContextHolder.getLocale()); 
    String text2 = messages.getMessage("app.quotes2", null, LocaleContextHolder.getLocale()); 

    log.debug("text = " + text); 
    log.debug("text2 = " + text2); 

    // Output 
    return "page"; 
} 

私が記録されますテキストが

  • text = Francescos title
  • text2 = Francesco's title
  • です

プロパティメッセージの一重引用符は、二重一重引用符( "Francescoのタイトル"は正しいテキストと見なされます)でエスケープする必要があるため、これは予測できます。 Thymeleafは、MessageSourceのように二重一重引用符をエスケープするメッセージを印刷するにはどうしたらいいですか? 私は、呼び出し元に基づいて異なるキー/値を使用しないことを望みます。

ご協力いただきありがとうございます。

答えて

1

春は限りそれはない引数(app.quote=John's message)が含まれてメッセージを解析しませんが、それはあなたがsetAlwaysUseMessageFormatでこの動作をオーバーライドすることができます(app.quote={0}'s message

1持っている場合はそうなります

@Bean 
MessageSource defaultMessageSource(){ 
org.springframework.context.support.ReloadableResourceBundleMessageSource source = new org.springframework.context.support.ReloadableResourceBundleMessageSource(); 
source.setAlwaysUseMessageFormat(true); 
return source; 
} 

resolveCodeWithoutArgumentsもご覧ください。

+2

ありがとうございます。 プロパティ** spring.messages.always-use-message-format = true **を設定しました。これで、thymeleafは二重引用符ですべてのテキストを正しく出力します。 – user3137952

関連する問題