最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Insert values from messages.properties into script in Thymeleaf - Stack Overflow

programmeradmin1浏览0评论

This is the script:

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

And 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:

When I tried this:

<script type="application/ld+json">
{
    "@context": ";,
    "@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>

Then I got 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: .thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "faq.question1" (template: "index-new" - line 404, col 32)] with root cause
.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'question1' cannot be found on null
    at .springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:225)
    at .springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
    at .springframework.expression.spel.ast.PropertyOrFieldReference$AccessorValueRef.getValue(PropertyOrFieldReference.java:376)
    at .springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:97)
    at .springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:116)
    at .springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:338)
    at .thymeleaf.spring6.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:265)
    at .thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
    at .thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
    at .thymeleaf.standard.expression.Expression.execute(Expression.java:109)

This is the script:

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

And 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:

When I tried this:

<script type="application/ld+json">
{
    "@context": "https://schema.",
    "@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>

Then I got 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: .thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "faq.question1" (template: "index-new" - line 404, col 32)] with root cause
.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'question1' cannot be found on null
    at .springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:225)
    at .springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
    at .springframework.expression.spel.ast.PropertyOrFieldReference$AccessorValueRef.getValue(PropertyOrFieldReference.java:376)
    at .springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:97)
    at .springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:116)
    at .springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:338)
    at .thymeleaf.spring6.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:265)
    at .thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
    at .thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
    at .thymeleaf.standard.expression.Expression.execute(Expression.java:109)
Share Improve this question edited Mar 29 at 16:31 Anish B. 17.1k4 gold badges28 silver badges50 bronze badges asked Feb 28 at 16:25 Nuñito CalzadaNuñito Calzada 1,76854 gold badges202 silver badges330 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 3 +250

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>)]",

Try to update your script to use th:text or th:utext for inserting dynamic content:

<script type="application/ld+json">
{
  "@context": "https://schema.",
  "@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>
发布评论

评论列表(0)

  1. 暂无评论