Java 单击html普通按钮调用spring MVC控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19954040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:49:03  来源:igfitidea点击:

Call spring MVC controller on click of html normal button?

javaspringjspspring-mvc

提问by user755806

I have below jsp page with one button. On click of the button it has call the controller and the controller has to display the same jsp page. How can i do that?

我有一个按钮下面的jsp页面。单击按钮时,它会调用控制器,并且控制器必须显示相同的 jsp 页面。我怎样才能做到这一点?

Controller.java

控制器.java

 @Controller
    @RequestMapping("/status")
    public class CheckController { 


        @RequestMapping(method = RequestMethod.GET)
        public String loadDetails(ModelMap model) {


            List<Data> details = // fetch data from database
            model.addAttribute("details ", details );
            return "Status";
        }

    }


Status.jsp
----------

    <html>
    <body>
            <h2>Spring MVC and List Example</h2>

        <c:if test="${not empty details}">
            <c:forEach var="listValue" items="${details}">
                <table border="1" cellspacing="1" align="center"
                    style="margin-top: 160px;">
                    <tr>
                        <th>Status</th>
                        <th>Message</th>
                        <th>Last Updated</th>
                    </tr>
                    <tr>
                        <td>OK</td>
                        <td>${listValue.message}</td>
                        <td>${listValue.lastChecked}</td>
                    </tr>
                </table>
            </c:forEach>
        </c:if>
<button>Load</button> //on click of button controller has to be called and again same jsp has to be rendered
    </body>
    </html>

采纳答案by n1ckolas

If you need to display the same JSP page, then regardless actual URL, you can use something like:

如果你需要显示相同的 JSP 页面,那么不管实际的 URL,你可以使用类似的东西:

<button onclick="window.location.href=window.location.href;">Load</button>

Also it can be slightly shorter, but please note, that it won't work for old IE versions.

它也可以稍微短一些,但请注意,它不适用于旧的 IE 版本。

<button onclick="window.location.reload();">Load</button>

回答by Georgy Gobozov

<button onclick="window.location.href='/status'">Load</button>

if your jsp has form you can submit form to action='/status' url

如果您的 jsp 有表单,您可以将表单提交到 action='/status' url

回答by Florian Fankhauser

So you need to make another GET request to the same URI. A clean way that doesn't depend on JavaScript is using a form:

因此,您需要对同一 URI 发出另一个 GET 请求。一种不依赖于 JavaScript 的干净方式是使用表单:

<form>
    <button type="submit">Load</button>
</form>

If you dont't specify an action-attribute on the form element, the GET request is made to the same URI as the current page (in HTML5). In HTML4 the action attribute is required, so you can use the following:

如果您没有在表单元素上指定 action-attribute,则 GET 请求将发送到与当前页面相同的 URI(在 HTML5 中)。在 HTML4 中 action 属性是必需的,因此您可以使用以下内容:

<form action="<c:url value="/status" />">
    <button type="submit">Load</button>
</form>