简体   繁体   English

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

[英]Call spring MVC controller on click of html normal button?

I have below jsp page with one button. 我有一个按钮在jsp页面下面。 On click of the button it has call the controller and the controller has to display the same jsp page. 单击按钮,它已调用控制器,控制器必须显示相同的jsp页面。 How can i do that? 我怎样才能做到这一点?

Controller.java Controller.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>
<button onclick="window.location.href='/status'">Load</button>

如果你的jsp有表格你可以提交表格到action ='/ status'url

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>

So you need to make another GET request to the same URI. 所以你需要对同一个URI发出另一个GET请求。 A clean way that doesn't depend on JavaScript is using a form: 一种不依赖于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). 如果您没有在表单元素上指定action-attribute,则GET请求将与当前页面(在HTML5中)的URI相同。 In HTML4 the action attribute is required, so you can use the following: 在HTML4中,action属性是必需的,因此您可以使用以下内容:

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

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

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