Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all available model attributes in Thymeleaf

Tags:

java

thymeleaf

For debugging purposes I would like to list all model attributes available to my thymeleaf template while it is rendering.

Something like:

<table>
    <tr th:each="model : ${*}">
        <td th:text="${model}"></td>
    </tr>
</table>

But obviously that's nonsense, and I get a well-deserved error. (org.springframework.expression.spel.SpelParseException: EL1070E:(pos 0): Problem parsing left operand)

Is there a way of outputting such debug information? I'd settle even for some logging output.

Or, does Thymeleaf provide something similar to Struts 2's struts.devMode where it added a debug section at the bottom of the page listing all available properties?

like image 962
David Lavender Avatar asked Jul 13 '15 15:07

David Lavender


People also ask

How do you get the model attribute in Thymeleaf?

In Thymeleaf, these model attributes (or context variables in Thymeleaf jargon) can be accessed with the following syntax: ${attributeName} , where attributeName in our case is messages . This is a Spring EL expression.

What is #{} in Thymeleaf?

#{} is used for message (i18n) expressions. Used to retrieve locale-specific messages from external sources.


2 Answers

Try this:

<table>
    <tr th:each="var : ${#vars}">
        <td th:text="${var.key}"></td>
        <td th:text="${var.value}"></td>
    </tr>
</table>
like image 158
the4dK Avatar answered Sep 22 '22 20:09

the4dK


The accepted answer does not seem to work for Thymeleaf 3; here's an update. Please note that I'm using Spring; this might not work for non-Spring apps.

<table>
    <tr th:each="var : ${#vars.getVariableNames()}">
        <td th:text="${var}"></td>
        <td th:text="${#vars.getVariable(var)}"></td>
    </tr>
    <!-- 
        Adding these manually because they are considered special.
        see https://github.com/thymeleaf/thymeleaf/blob/thymeleaf-3.0.3.RELEASE/src/main/java/org/thymeleaf/context/WebEngineContext.java#L199
    -->
    <tr>
        <td>param</td>
        <td th:text="${#vars.getVariable('param')}"></td>
    </tr>
    <tr>
        <td>session</td>
        <td th:text="${#vars.getVariable('session')}"></td>
    </tr>
    <tr>
        <td>application</td>
        <td th:text="${#vars.getVariable('application')}"></td>
    </tr>
</table>

That said, what I've done is created a standalone Bean that makes things a bit prettier and dumps to logs instead of to HTML:

@Component
public class ThymeleafDumper {

    private Logger log = LoggerFactory.getLogger(ThymeleafDumper.class);

    public void dumpToLog(WebEngineContext ctx) {
        log.debug("Thymeleaf context: {}", formatThisUpNicely(ctx));
    }

    // ... etc
}

Where formatThisUpNicely can use ctx.getVariableNames(), put the results into a SortedMap, export to json, whatever. Don't forget those three 'special' variables!

Then expose an instance of it as a @ModelAttribute in a Controller or a ControllerAdvice:

@ControllerAdvice
public class SomeControllerAdvice {

    @Autowired
    private ThymeleafDumper thymeleafDumper;

    @ModelAttribute("dumper")
    public ThymeleafDumper dumper() {
        return this.thymeleafDumper;
    }
}

Then in my template run:

<div th:text="${dumper.dumpToLog(#vars)}"/>
like image 45
Roy Truelove Avatar answered Sep 23 '22 20:09

Roy Truelove