Skip to content
Advertisement

How to send the data finded in the controller to a view?

I try to do a java web application using:
SpringBoot
Mysql
JDBC
Design pattern: MVC, DAO
And Thymeleaf
I think i understood than:
I must create a class for every Entity and a DAO classe who use the annotation @Repository:
Diabetic.java(Entity):

JavaScript

DiabeticDAO.java:

JavaScript

And create a controller like this where I use the method from the DAO:

JavaScript

What i don’t understand:

  1. How to do a link in a view(html) to go to a controller ?
  2. How to send the data finded in the controller to a view for displaying them ?

Sorry if my questions seems strange but it’s the first time i ask for help on here and english is not my first language

Advertisement

Answer

You don´t have to link a controller in the view. The connection is handled trough the URL calls of the controller.

I´m taking examples from this introduction: https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

You can use several methods to add data to the view.

1. Option:

JavaScript

In this method we put “Model model” as a parameter to the method. You can now fill the model with some data.

2. Option:

JavaScript

In this method we create a ModelAndView object, setting the url which should be called and fill it with data. Again the data is called “messages”. The return value is the created object.

Access data in view:

After you implemented one of the two options, you can access the data like this:

JavaScript

Let´s try to implement it into your current code:

JavaScript

Now you can access the data in your view:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement