简体   繁体   English

从JSP按钮在Spring MVC中执行方法

[英]Executing a method in the Spring MVC from the JSP button

I work with a Spring MVC app where I would like to execute a POST method after clicking a button in the JSP page. 我使用Spring MVC应用程序,在该应用程序中,单击JSP页面中的按钮后,我想执行POST方法。 The landing page looks like, 登陆页面看起来像

在此处输入图片说明

I would like to execute the following Spring method after clicking the Generate Address button, 我想在单击“ Generate Address按钮后执行以下Spring方法,

    @PostMapping(value = "/generateAddress")
    public String generateAddress() {    
        walletService.generateAddress();
        return "redirect:/";
    }

What I would like to achieve

after clicking the Generate Address button, I would like to make a POST request to the generateAddress method. 单击“ Generate Address按钮后,我想向generateAddress方法发出POST请求。

The JSP page with the button code, 带有按钮代码的JSP页面,

<body class="page_container">
<div class="wallets_page">

    <form id="mnfrm" action="/" method="get" target="_blank">
        <div class="buttons_box">

            <button type="button" class="btn btn-default btn-lg active">
                Generate address
            </button>

            <button type="submit" class="btn btn-default btn-lg active" <%= wallets.isEmpty() ? "disabled" : ""%>
                    onclick="setFormAction('mnfrm', '/balance')">Balance
            </button>
            <button type="submit" class="btn btn-default btn-lg active" <%= wallets.isEmpty() ? "disabled" : ""%>
                    onclick="setFormAction('mnfrm', '/transactions')">Transactions
            </button>
            <button type="submit" class="btn btn-default btn-lg active" <%= wallets.isEmpty() ? "disabled" : ""%>
                    onclick="setFormAction('mnfrm', '/sendMoney')">Send money
            </button>
        </div>

        <div class="addresses_box">
            <label for="addressId">Address</label>
            <select id="addressId" name="id" class="form-control">
                <c:forEach var="wallet" items="${wallets}">
                    <option value="${wallet.id}"><c:out value="${wallet.address}"></c:out></option>
                </c:forEach>
            </select>
        </div>
    </form>
</div>

I have tried to set an onclick in the button that doesn't work. 我试图在无效的button中设置onclick

    <button type="button" class="btn btn-default btn-lg active">
          Generate address
    </button>

Any suggestion of how to do it properly? 关于如何正确执行任何建议?

More Info

This is the method opens the landing page of main.jsp , 这是打开main.jsp登陆页面的方法,

@GetMapping(value = "/")
    public String showBitcoinWallet(final Model model) {
        List<WalletInfo> wallets = walletService.getAllWallets();
        model.addAttribute("wallets", wallets);
        return "main";
    }

I have solved this with the button id and an Ajax POST request combined. 我已经通过组合按钮ID和Ajax POST请求解决了这个问题。 The solution is very simple and I needed an mvn clean before it would work out. 解决方案非常简单,我需要先进行mvn clean然后才能解决。

Address button 地址按钮

<button id="genAddress" type="button" class="btn btn-default btn-lg active">
                Generate address
            </button>

The Ajax POST request, Ajax POST请求,

<script>
    $("#genAddress").click(function () {
        $.ajax({
            type: "POST",
            url: 'generateAddress',

            success: function (data) {
                console.log("Wallet address generation success!")
            },

            failure: function (errMsg) {
                console.log(errMsg.toString())
            }
        });
    });
</script>

I assumed we can make the POST request without providing any data, but, wasn't sure about how to do it. 我以为我们可以在不提供任何数据的情况下发出POST请求,但是不确定如何执行。 Ok, just remove the data provision code from the Ajax POST request :) 好的,只需从Ajax POST请求中删除数据提供代码即可:)

As previously provided, this is the method execute the POST request from the Spring controller. 如前所述,这是执行来自Spring控制器的POST请求的方法。

@PostMapping(value = "/generateAddress")
    public String generateAddress() {

        walletService.generateAddress();
        return "redirect:/";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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