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.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable; 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.model.News;
import ru.ulstu.service.NewsService; import ru.ulstu.service.NewsService;
import javax.validation.Valid;
@Controller @Controller
public class NewsController { public class NewsController {
private final NewsService newsService; private final NewsService newsService;
@ -30,8 +33,12 @@ public class NewsController {
} }
@PostMapping("saveNews") @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); newsService.save(news);
return "redirect:/"; return "redirect:/";
} }
} }

View File

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

View File

@ -19,4 +19,8 @@
.news-image { .news-image {
width: 300px; width: 300px;
}
.error {
color: red;
} }

View File

@ -11,14 +11,17 @@
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<h3>Редактирование новости:</h3> <h3>Редактирование новости:</h3>
<form action="#" th:action="@{/saveNews}" th:object="${news}" method="post"> <form action="#" th:action="@{/saveNews}" th:object="${news}" method="post">
<p style="color:red" th:text="${error}"></p>
<div class="form-group"> <div class="form-group">
<label for="title">Заголовок</label> <label for="title">Заголовок</label>
<input type="text" class="form-control" id="title" th:field="*{title}" placeholder="Заголовок новости"> <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>
<div class="form-group"> <div class="form-group">
<label for="text">Текст новости</label> <label for="text">Текст новости</label>
<textarea class="form-control" id="text" th:field="*{text}" placeholder="Текст новости"></textarea> <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> </div>
<button type="submit" class="btn btn-primary">Сохранить</button> <button type="submit" class="btn btn-primary">Сохранить</button>
</form> </form>