Skip to content
Advertisement

How to properly define a Controller in Spring MVC compared to a JavaFX Controller

I’m having a hard time trying to understand the differences between a JavaFX Controller and a Spring MVC Controller. I came from a JavaFX background and I have seen many differences between a Spring MVC Controller vs a JavaFX Controller and I am currently working with Spring MVC. There are important differences between them, for example, in JavaFX, each fxml view has its own controller e.g. LoginViewController.java with loginview.fxml, RegisterViewController.java and registerview.fxml.

This is the code of a simple ToDo List in JavaFX to be more clear in my question:

ToDoListViewController.java:

public class ToDoListViewController {

    // private properties tied to the fxml view via @FXML annotation

    private List<TodoItem> todoItems;

    @FXML
    private ListView<TodoItem> todoListView;

    @FXML
    private TextArea itemDetailsTextArea;
    
    // ...

    

    public void initialize() {

       // code to initialize stuff
       // ...
    }


    @FXML
    public void showUpdateItemDialog() {
         //get the item to be updated
        // do stuff to update item
       // ....
    }

}

This Controller is tied to a todolistview.fxml, which I think is pretty straight forward.

Now in Spring, I have seen some controllers been oriented by the view and other by routes and it kind of confuses me. I have seen controllers like:

HomeController.java: (View Oriented)

@Controller
public class HomeController {
    
    @RequestMapping("/")
    public String showPage() {
        return "main-menu";
    }

}

CustomerController.java: (Route Oriented)

@Controller
@RequestMapping("/customer")
public class CustomerController {
    
    private CustomerDAO customerDAO;
    
    public CustomerController(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }
    
    @RequestMapping("/list")
    public String listCustomers(Model theModel) {
        
        // get the customers from the dao
        
        
        // add the customers to the model
        
        return "list-customers";
    }

    @RequestMapping("/add")
    public String addCustomer() {
        // return the jsp view to add a new customer
        
        return "customer-form";
    }

   // same goes for get by id, update and delete

   //...
}

Which is the best way to understand these differences? Which approach to use in Spring MVC? View oriented, Route oriented? Thanks for reading!

Advertisement

Answer

It all depends on requirement, for example:

In your case, if you want to access anything directly, (as homepage) you can go with view oriented one.

and if anything you want the access the like CUSTOMERS , so in your case, you can use view orientation, for example for viewing customer you can just create a method with just “/customerList” and you will also get the required result, but every time for the customers you will need to do this for everything, Instead what you can do is Use route mapping since the (customer)feature establishes a route with it’s creation, so you can use route mapping, as you have post in second example, in that case all the request with “/customer” will come there and then will get to the exact method which you want(have written in the method’s mapping). It is good for end mapping and can use to pass parameter id needed.

So it all depends on requirement and level of code one is writing.

Advertisement