0

I have the following code:

@RestController
public class RestTmp {

    @Autowired
    SchemasService schemasService;

    @RequestMapping(path = "/editSchema/{id}")
    public void editSchemaById(Model model, @PathVariable("id") Integer id, HttpServletResponse response) throws IOException {
        String schemaERD = schemasService.editUser(id);
        model.addAttribute("message", schemaERD);
        response.sendRedirect("/drawdiagram");
    }
}

Can anyone tell me how i can get value from that variable "message" now?
I need to handle that value in my "/drawdiagram" ---> (drawdiagram.html)

I tried with thymeleaf something like that:

<script th:inline="javascript">
    /*<![CDATA[*/

    var message = /*[[${message}]]*/ 'default';
    console.log(message);

    /*]]>*/
</script>

but i am getting null all the time ... Can someone help me? :(

Brarord
  • 611
  • 5
  • 18

2 Answers2

1

inside your script tag, try this:

 var message = '${message}';
user404
  • 1,934
  • 1
  • 16
  • 32
  • Thanks man but this is not solution for me :( https://i.imgur.com/TbZWPNs.png I don't know why but this is just normal "string" for compiler. – Brarord Jan 29 '20 at 05:52
  • 1
    may be, this one can help: [Setting up a JavaScript variable from Spring model by using Thymeleaf](https://stackoverflow.com/questions/25687816/setting-up-a-javascript-variable-from-spring-model-by-using-thymeleaf) – user404 Jan 29 '20 at 05:57
  • I tried that also and i am getting null :( I mentioned that in my post. – Brarord Jan 29 '20 at 06:03
  • check if your `message` variable really sets null in controller. – user404 Jan 29 '20 at 06:04
  • Variable "message" in controller is ok, i tried also put sample string like "whatever string" into that "message" and i am getting null also. https://i.stack.imgur.com/sBMw8.png – Brarord Jan 29 '20 at 06:08
1

It seems you are adding the "comment-statement" (/* and */) once too many times. Remove them in the inner part. And keep the square brackets.

<script th:inline="javascript">
    /*<![CDATA[*/

    var message = [[${message}]];   <--- Keep square brackets on this line.
    console.log(message);

    /*]]>*/
</script>

I used the same link user404 mentions in the comment-section in that persons answer and that is working for me.

kometen
  • 6,536
  • 6
  • 41
  • 51