MSOUFAN
MSOUFAN

Reputation: 101

Link a method from a controller to a button click in Spring boot Java

I am working on a spring java project. I have a view that contains multiple buttons. Each one will fire a method from the controller. So how can I link my button click function to a specific method in my controller?

Here is some of the HTML from the MainView.html

 <button class="btn btn-success btn-default">
 <span class="glyphicon glyphicon-play-circle" aria-hidden="true"></span>
 </button>

Here is the MainController class:

import org.springframework.stereotype.Controller;


@Controller
public class MainController
{

    @GetMapping(value="/")
    public String home() 
    {
         return "views/MainView";
    }

     public void run() //I want to link the button click to this method
     {  
         System.out.println("Test1");
         System.out.println("Test2");
         System.out.println("Test3");
         System.out.println("Test4");
     }
}

Upvotes: 1

Views: 5224

Answers (2)

Guilherme Iobbi
Guilherme Iobbi

Reputation: 9

Your page has forms and buttons which makes requests to the server.

Spring handles the requests on the server-side.

If you're using Chrome, you can monitor the network behavior on the Network tab when you press the referred button to see the request and response between the browser and the server.

Upvotes: 1

metaldino21
metaldino21

Reputation: 326

You could add a mapping to your method and call that from a js function. I'm not sure what your goal is. On your controller:

@GetMapping(value="/run")
public void run() //I want to link the button click to this method
     {  
         System.out.println("Test1");
         System.out.println("Test2");
         System.out.println("Test3");
         System.out.println("Test4");
     }

Then your button can just call a javascript function to get data from /run

Upvotes: 0

Related Questions