diff --git a/data/db.mv.db b/data/db.mv.db index db9ad7d..f2a5f01 100644 Binary files a/data/db.mv.db and b/data/db.mv.db differ diff --git a/src/main/java/ru/ulstu/controller/NewsController.java b/src/main/java/ru/ulstu/controller/NewsController.java index d76fe1f..61cca27 100644 --- a/src/main/java/ru/ulstu/controller/NewsController.java +++ b/src/main/java/ru/ulstu/controller/NewsController.java @@ -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:/"; } } diff --git a/src/main/java/ru/ulstu/model/News.java b/src/main/java/ru/ulstu/model/News.java index 7efb6dc..af66265 100644 --- a/src/main/java/ru/ulstu/model/News.java +++ b/src/main/java/ru/ulstu/model/News.java @@ -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() { diff --git a/src/main/resources/public/css/main.css b/src/main/resources/public/css/main.css index c207eae..b2b570d 100644 --- a/src/main/resources/public/css/main.css +++ b/src/main/resources/public/css/main.css @@ -19,4 +19,8 @@ .news-image { width: 300px; +} + +.error { + color: red; } \ No newline at end of file diff --git a/src/main/resources/templates/editNews.html b/src/main/resources/templates/editNews.html index 96aa36e..a6df9fb 100644 --- a/src/main/resources/templates/editNews.html +++ b/src/main/resources/templates/editNews.html @@ -11,14 +11,17 @@

Редактирование новости:

-

+

+ Не может быть пустым

+

+ Не может быть пустым