2

I'm using thymeleaf and spring 3 mvc. When I try to perform Ajax POST request I don't understand how can I show a response message from a controller on my html page:

Here's a code snippet:

$.ajax({
    type: "POST",
    url: "/settings",
    data: "request=" + request,
    success: function (response) {
        $('#msg').replaceWith('<div id="msg" th:text="response"></div>');
    },
});

'response' is a i18n message from controller. Now, I want to show this message at using thymeleaf (th:text="response"). Of course, this code does not work, because it thinks that response variable is a plain string.

The question is how to show i18n response message using thymeleaf. Or maybe there are some other methods to show i18n messages on html page (not using jsp) through js?

1 Answer 1

2

Thymeleaf attributes (such as th:text) will only be parsed and replaced on the server. Since this ajax response is processed on the browser, th:text will not process. If "settings" is already a Thymeleaf-resolved page, it is likely already i18n'd and you can simply do something like:

$('#msg').html(response);

However, if you are truly looking for client-side javascript processing of Thymeleaf tags, consider Thymol.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.