【问题标题】:How to call a method in Managed beans when I click in thymeleaf button当我单击 thymeleaf 按钮时如何在托管 bean 中调用方法
【发布时间】:2016-10-07 17:18:28
【问题描述】:

我想知道当我单击按钮时是否可以调用托管 bean 中存在的方法(该按钮是用 Thymeleaf 创建的)。

谢谢

【问题讨论】:

    标签: ejb thymeleaf managed-bean


    【解决方案1】:

    我不知道是否有直接做的选项,我不这么认为。但解决方案可能更简单:为什么不将您的按钮提交到执行此方法的端点?

    <button onclick="execute()">Click me</button>
    
    <script>
        function execute() {
            //$.get() or
            $.post( "/the/endpoint", function(data) {
                 $('#test').html(data);
            });
        }
    </script>
    

    那么/the/endpoint就是你的@Requestmethod,它在内部执行你想要的方法:

    @Controller
    public class YourController {
    
        @GET
        @Path("/the/endpoint")
        public Response executeMethod() {
            yourMethod(); //Executes your method
            return Response.status(200);
        }
    }
    

    【讨论】:

    • 感谢您的回答,但我不想使用 Javascript。
    猜你喜欢
    • 2014-01-19
    • 2013-09-10
    • 2023-02-21
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2011-04-06
    相关资源
    最近更新 更多