0

I am trying to call a method that returns a model and prints out a simple message to the console using Thymeleaf. However, I realize that whenever I setup the action to be called it does not work the way I want it to. The function does not get called. I have also tried to use th:action instead but still get the same issue of it not getting the model and not doing the System.out.print("Works") line of code. I have been stuck so long on this issue and have read some documentation, but I can't seem to figure out a way to solve it.

Java:

@GetMapping("/createSelector")
    @ResponseBody
    public String getCreationSelector() {

        selectorList.add(new Selector());
        System.out.println("Test Works");
        return "The size is: " + selectorList.size();


    }

Thymeleaf:

<div class="row justify-content-md-center">
                        <a th:href="@{/createSelector}" id="selectorBtn"><button
                                class="btn btn-sm btn-block eNew">New Tier</button></a>

                    </div>
kane_004
  • 253
  • 3
  • 18
  • `th:href` just results in a regular `href`. If you visit /createSelector directly, does the `println` get called? If not, the issue probably doesn't have to do with Thymeleaf. – Knox Jan 09 '20 at 19:53
  • @Knox Yes, when I visit using ```/createSelector``` it does print it out – kane_004 Jan 09 '20 at 20:00
  • Can you edit in the generated HTML from your Thymeleaf segment from the browser? – Knox Jan 09 '20 at 20:03
  • @Knox What do you mean by that? – kane_004 Jan 09 '20 at 20:06
  • Thymeleaf is a template engine, so you provide a Thymeleaf template (like your "Thymeleaf" snippet above), and it processes it into plain old HTML (which the browser can interpret). I'm looking for what the processed template looks like. On whichever page you are seeing the button on, I'm asking for the relevant HTML from the browser (via right click -> "Inspect Element" or a "View Page Source" option). – Knox Jan 09 '20 at 20:09
  • @Knox Yeah ok, so it shows this `````` – kane_004 Jan 09 '20 at 20:16
  • @Knox And when I click it, it actually does what I want. Also, FYI I am using ```event.preventDefault();``` in my jquery to allow it to run the process without changing screens. – kane_004 Jan 09 '20 at 20:16
  • I'm not sure if you're saying the problem is resolved, but if not, keep in mind that `event.preventDefault();` will also prevent the browser from submitting the request. It sounds like you want to submit an AJAX request at the same time you call that: https://stackoverflow.com/q/8567114/1526322 – Knox Jan 09 '20 at 20:24
  • 2
    You asked the exact same question here: https://stackoverflow.com/questions/59667627/how-to-fix-calling-java-method-using-thymeleaf Please avoid doing this. – Wim Deblauwe Jan 10 '20 at 07:34

0 Answers0