如何使用Thymeleaf修复调用Java方法? [英] How to fix calling Java method using Thymeleaf?

查看:306
本文介绍了如何使用Thymeleaf修复调用Java方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用一个方法,该方法返回模型并使用Thymeleaf将简单消息输出到控制台.但是,我意识到,每当我设置要调用的动作时,它就不会按照我想要的方式工作.该函数不会被调用.虽然当我进入视图page source并单击按钮时,它仍会执行所需的操作.我尝试使用th:action代替,但是仍然遇到相同的问题,即没有获取模型并且没有执行System.out.print("Works")行代码.我已经在这个问题上停留了很长时间,并且已经阅读了一些文档,但是我似乎无法找到解决问题的方法.

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. Though when I go into view page source and click the button it does the desired action. I have 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.

HTML:


    <div class="modal-footer">
            <a th:href="@{/mqlGetSecondQuery}" class="selectorBtn">
                  <button type="button" class="btn btn-primary modalPathContentBtn" 
                   data-dismiss="modal">Continue</button>
            </a>
    </div>

Java:

@RequestMapping(value = "/mqlGetSecondQuery", method = RequestMethod.GET)
    public void getMQLSecondQuery(Model model) {

         model.addAttribute("queryResult", stringFromClass);
         System.out.println("Works");
    }

推荐答案

您在<a>元素内有<button>元素,这意味着您在同一位置有2个可单击的元素.基本上,这是无效的HTML语法.我怀疑这可能会触发动作.

You have <button> element inside <a> element, meaning you have 2 clickable elements in the same place. Basically it is not valid HTML syntax. I suspect this could mess up with triggering an action.

请删除

<button type="button" class="btn btn-primary modalPathContentBtn" 
                   data-dismiss="modal">Continue</button>

从您的角度来看,并提供任何有效的内容(例如纯文本Continue).

from your view and provide any valid content instead (e.g. plain text Continue).

这篇关于如何使用Thymeleaf修复调用Java方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆