Search code examples
javascripthtmlcssspring-bootthymeleaf

Spring Boot & Thymeleaf : FAQ Schema Markup to retrieve text dynamically from a Thymeleaf properties file


I created this script:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [{
    "@type": "Question",
    "name": "[(${faq.question1})]",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "[(${faq.answer1})]"
    }
  }]
}
</script>

and in the property file messages.properties:

faq.question1=What is Mystic Planets?
faq.answer1=Mystic Planets is an astrology app that helps users understand their birth chart and align with their purpose.

but the page does not load:

enter image description here

also tried with :

<script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "FAQPage",
        "mainEntity": [{
            "@type": "Question",
            "name": "<th:block th:text="${faq.question1}"></th:block>",
            "acceptedAnswer": {
                "@type": "Answer",
                "text": "<th:block th:text="${faq.answer1}"></th:block>"
            }
        }]
    }
</script>

but then I have this error:

2025-02-28 20:20:45.040 [http-nio-8080-exec-1] ERROR [] o.a.c.c.C.[.[.[.[dispatcherServlet]@log(175) - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "faq.question1" (template: "index-new" - line 404, col 32)] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'question1' cannot be found on null
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:225)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorValueRef.getValue(PropertyOrFieldReference.java:376)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:97)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:116)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:338)
    at org.thymeleaf.spring6.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:265)
    at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
    at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)

with the proposed solution:

enter image description here


Solution

  • Use #{key.name} to get value from messages.properties.

    In your case it will look like:

    "name": "<th:block th:text=#{faq.question1}></th:block>",
    

    or

    "name": "[(<th:block th:text=#{faq.question1}></th:block>)]",