# 4. Вывод данных из базы данных

Для вывода данных на странице index, необходимо выполнить следующие действия:

1. обратиться к базе данных для получения списка студентов;
2. передать список студентов в View;
3. предусмотреть вывод полей каждого студента в нужных ячейках таблицы.

Модифицируем метод контроллера, который отвечает за обработку запроса "**\\**"

{% code title="StudentController.java" %}

```java
@Controller
public class StudentController {

    private StudentService service;

    @Autowired
    public void setService(StudentService service) {
        this.service = service;
    }
    
    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("students", service.getAllStudents());
        return "index";
    }
    
    ...
}
```

{% endcode %}

Модифицируем страницу **index.html**. Добавим вывод полей каждого объекта типа **Student** в таблице

{% code title="index.html" %}

```markup
<table class="table table-striped">
    <thead>
    <tr>
        <th class="text-justify">#</th>
        <th class="text-justify">Фамилия</th>
        <th class="text-justify">Имя</th>
        <th class="text-justify">Отчество</th>
        <th class="text-justify">Почта</th>
        <th class="text-justify">Телефон</th>
        <th class="text-justify">Адрес</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="student : ${students}">
        <td class="align-middle"><span th:text="${student.id}"/></td>
        <td class="align-middle"><span th:text="${student.lastName}"/></td>
        <td class="align-middle"><span th:text="${student.firstName}"/></td>
        <td class="align-middle"><span th:text="${student.patronymic}"/></td>
        <td class="align-middle"><span th:text="${student.email}"/></td>
        <td class="align-middle"><span th:text="${student.phone}"/></td>
        <td class="align-middle"><span th:text="${student.address}"/></td>
    </tr>
    </tbody>
</table>
```

{% endcode %}

Запустим приложение и посмотрим на результат. Изначально таблица студентов пустая

![](/files/-Lcd9EgsdOBoiO54ydq_)

Добавляем нового студента

![](/files/-Lcd9_TT8EKj1XyTVGy9)

После добавления студента, нас перенаправляют на страницу **index**

![](/files/-Lcd9iWCSxw3rkboFWir)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://opu.gitbook.io/oop2/practice-4/4.-vyvod-dannykh-iz-bazy-dannykh.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
