从JSP按钮

时间:2017-09-03 06:54:26

标签: java spring jsp spring-mvc

我使用Spring MVC应用程序,我希望在单击JSP页面中的按钮后执行POST方法。着陆页看起来像,

enter image description here

我想在点击Generate Address按钮

后执行以下Spring方法
    @PostMapping(value = "/generateAddress")
    public String generateAddress() {    
        walletService.generateAddress();
        return "redirect:/";
    }

What I would like to achieve

点击Generate Address按钮后,我想对generateAddress方法发出POST请求。

带有按钮代码的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>

我试图在onclick中设置button无效。

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

有关如何正确操作的任何建议吗?

More Info

此方法会打开main.jsp

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

1 个答案:

答案 0 :(得分:1)

我已经通过按钮ID和Ajax POST请求相结合解决了这个问题。解决方案非常简单,我需要mvn clean才能解决问题。

地址按钮

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

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>

我认为我们可以在不提供任何数据的情况下发出POST请求,但是,我不确定如何执行此操作。好的,只需从Ajax POST请求中删除数据提供代码:)

如前所述,这是执行POST请求的方法 来自Spring控制器。

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

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