简体   繁体   English

映射 HTML 按钮以调用 controller class 中的方法

[英]Mapping a HTML button to call a method in a controller class in Spring MVC

I am having some difficulties in calling a method when a button is clicked.单击按钮时,我在调用方法时遇到了一些困难。

In my Index.JSP在我的 Index.JSP

I have the following section of HTML code我有 HTML 代码的以下部分

 <div class="hero-copy">
                    <h1 class="hero-title mt-0">Deep Algorithm by Sadman Sakib</h1>
                    <p class="hero-paragraph">A personal portfolio/demonstration of all university and extra-curricular activites, beautifully packaged, in a modern and responsive Spring MVC Web Application</p>
                    <div class="hero-cta">
                        <a class="button button-primary" onclick="">View Projects</a>
                        <div class="lights-toggle">
                            <input id="lights-toggle" type="checkbox" name="lights-toggle" class="switch" checked="checked"  >
                            <label for="lights-toggle" class="text-xs"><span>Turn me <span class="label-text">dark</span></span></label>
                        </div>
                    </div>
                </div>

Now "View Projects" is a button which will print "Hello" out in the console, I am not sure what I should be using to call the method in my controller.现在“查看项目”是一个按钮,它将在控制台中打印“Hello”,我不确定我应该使用什么来调用我的 controller 中的方法。 At the moment I am using onClick but am not sure what I should put as a parameter.目前我正在使用 onClick 但不确定我应该将什么作为参数。

This is my controller class这是我的 controller class

    @Controller
public class HelloController {

    @RequestMapping("/projects")
    public void add()
    {
        System.out.println("Hello");
    }

}

This is my view, user will click view projects and it will print hello out in the console, how would I go about doing this?这是我的观点,用户将单击查看项目,它将在控制台中打印出你好,我将如何 go 这样做?

在此处输入图像描述

EDIT编辑

When I use a form it works, however when using href it does not link to my controller.当我使用表单时它可以工作,但是当使用 href 时它不会链接到我的 controller。

<form action="add">
                    <input type="submit"  class="button button-primary" value="Me">
                </form>

             
                <span>View GitHub</span>
                <a href="${pageContext.request.contextPath}add"><h3>View GitHub</h3></a>

How can I use href to link back to my controller.如何使用 href 链接回我的 controller。 Do I need to import some dependencies/taglines我需要导入一些依赖项/标语吗

First you must understand what the controller is doing.首先你必须了解 controller 在做什么。 The controller is mapping the request which have the path /projects , so usually, the url will be something like http://localhost:8080/projects if your app is running on port 8080. Upon calling '/projects' from your browser, the method add() will be executed. The controller is mapping the request which have the path /projects , so usually, the url will be something like http://localhost:8080/projects if your app is running on port 8080. Upon calling '/projects' from your browser, add()方法将被执行。

The easiest way to trigger the method add() in the controller is using href in the link.在 controller 中触发方法add()的最简单方法是在链接中使用href

The code will be as follows:代码如下:

<a class="button button-primary" href="${pageContext.servletContext.contextPath}/projects">View Projects</a>

If you really want to use the onclick method, then you must create a javascript function and call the url which is mapping to /projects If you really want to use the onclick method, then you must create a javascript function and call the url which is mapping to /projects

Update 28/06/20 2020 年 6 月 28 日更新

The code will not work because the method being invoked is returning void.代码将不起作用,因为被调用的方法返回 void。 Add @ResponseStatus(value = HttpStatus.OK) on the method then it should work just fine.在方法上添加 @ResponseStatus(value = HttpStatus.OK) 然后它应该可以正常工作。

@Controller
public class HelloController {

    @RequestMapping("/projects")
    @ResponseStatus(value = HttpStatus.OK)
    public void add()
    {
        System.out.println("Hello");
    }

}

So basically I managed to fix it, I should call my controller using href like this所以基本上我设法修复它,我应该使用这样的 href 调用我的 controller

href="<c:url value="/projects"/>

Cheers to everyone who helped向所有帮助过的人致敬

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

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