简体   繁体   中英

javascript error when accessing flash redirect attribute (thymeleaf, springmvc)

I'm currently stuck with the following problem with SpringMVC, Thymeleaf and Javascript.

1) Server-Side: Under some circumstances I add a flash redirect attribute in my controller before redirecting to a certain page.

redirectAttributes.addFlashAttribute("myCondition", true);

2) Client-Side: In case that attribute exists, I want my javascript function to execute additional code.

window.onload = function() {
    ...
    if([[${myCondition}]]) {
        //do something
    }
}

This works fine after the redirect, but on first enter the redirect attribute does simply not exist, which results in the following javascript code being rendered:

    window.onload = function() {
    if() {
        //do something
    }
}

This code is not valid and therefore produces a javascript error. I'm not sure how to check with javascript if the redirect attribute exists?

can you try the following code and let me know if this works for you declare a variable mycondition

var mycondition = [[${myCondition}]];

if(mycondition) {
   //do something
}

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM