[FIXED] How do you access a model attribute with javascript variable

Issue

I’m adding an attribute to my ModelAndView in spring and after this i forwarding it to my thymeleaf view.

In the view i have the following code:

<script th:inline="javascript">
        /*<![CDATA[*/
            var applicationName = /*[[${T(com.sample.constants.ApplicationConstants).MODEL_ATTR_COLLECTED_VALUES}]]*/ "Test";
            var collectedValueJson = [[${collectedValues}]];
            console.log(collectedUserJson);
        /*]]>*/
</script>

Result from this is

var applicationName = 'collectedValues';
var collectedUserJson = '[{\"givenname\":\"Muster\",\"surname\":\"Peter\"}]';

That’s fine. Now my wish for this is, that i can take the var application and access with this variable the modelattribute, but that’s not working.

Result is this:

var tmp2 = ${applicationName};

An other try was, that i have access to the modelattribute with the syntax /*[[ ]]*/ from the first try:

var applicationName = ${/*[[${T(com.sample.constants.ApplicationConstants).MODEL_ATTR_COLLECTED_VALUES}]]*/};

But result will be:

var tmp = ${'collectedValues'

i have no idea what can i try.

Any other suggestions?

Thank’s in advance.

Solution

there is no way you can access model atributes hence they are on server side and they where lost when rendering jsp into HTML

Answered By – Juanr

Answer Checked By – Marilyn (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published