1-db #2

Merged
romanov73 merged 6 commits from 1-db into master 2022-03-09 17:28:19 +04:00
5 changed files with 19 additions and 2 deletions
Showing only changes of commit 6207da8fa5 - Show all commits

Binary file not shown.

View File

@ -8,6 +8,7 @@ package ru.ulstu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
@ -15,6 +16,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import ru.ulstu.model.News;
import ru.ulstu.service.NewsService;
import javax.validation.Valid;
@Controller
public class NewsController {
private final NewsService newsService;
@ -30,8 +33,12 @@ public class NewsController {
}
@PostMapping("saveNews")
public String saveNews(@ModelAttribute News news) {
public String saveNews(@Valid @ModelAttribute News news, BindingResult result) {
if (result.hasErrors()) {
return "editNews";
}
newsService.save(news);
return "redirect:/";
}
}

View File

@ -2,17 +2,20 @@ package ru.ulstu.model;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
@Entity
public class News extends BaseEntity {
private final static int MAX_NEWS_TEXT_PREVIEW_LENGTH = 800;
@NotEmpty(message = "Заголовок не может быть пустым")
private String title;
private Date date;
@Lob
@NotEmpty(message = "Текст новости не может быть пустым")
private String text;
public News() {

View File

@ -20,3 +20,7 @@
.news-image {
width: 300px;
}
.error {
color: red;
}

View File

@ -11,14 +11,17 @@
<div class="container" layout:fragment="content">
<h3>Редактирование новости:</h3>
<form action="#" th:action="@{/saveNews}" th:object="${news}" method="post">
<p style="color:red" th:text="${error}"></p>
<div class="form-group">
<label for="title">Заголовок</label>
<input type="text" class="form-control" id="title" th:field="*{title}" placeholder="Заголовок новости">
<p th:if="${#fields.hasErrors('title')}" th:class="${#fields.hasErrors('title')}? error">
Не может быть пустым</p>
</div>
<div class="form-group">
<label for="text">Текст новости</label>
<textarea class="form-control" id="text" th:field="*{text}" placeholder="Текст новости"></textarea>
<p th:if="${#fields.hasErrors('text')}" th:class="${#fields.hasErrors('text')}? error">
Не может быть пустым</p>
</div>
<button type="submit" class="btn btn-primary">Сохранить</button>
</form>