#3 -- fix routing

This commit is contained in:
Anton Romanov 2022-03-11 09:10:48 +04:00
parent 2ac1b3f3c1
commit 76ab0ddf11
12 changed files with 79 additions and 24 deletions

Binary file not shown.

View File

@ -45,7 +45,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
log.debug("Security enabled");
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/login", "/index", "/news/*", "/h2-console/*", "/h2-console").permitAll()
.antMatchers("/login", "/index", "/news/**", "/h2-console/*", "/h2-console").permitAll()
.antMatchers("/swagger-ui.html").hasAuthority(UserRoleConstants.ADMIN)
.anyRequest().authenticated()
.and()

View File

@ -22,7 +22,7 @@ public class IndexController {
@GetMapping("/")
public String index(Model model) {
model.addAttribute("news", newsService.getAll());
model.addAttribute("news", newsService.getLast());
return "index";
}
}

View File

@ -13,12 +13,14 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.ulstu.model.News;
import ru.ulstu.service.NewsService;
import javax.validation.Valid;
@Controller
@RequestMapping("news")
public class NewsController {
private final NewsService newsService;
@ -26,6 +28,12 @@ public class NewsController {
this.newsService = newsService;
}
@GetMapping("/news")
public String index(Model model) {
model.addAttribute("news", newsService.getAll());
return "news";
}
@GetMapping("/editNews/{newsId}")
public String editNews(@PathVariable(value = "newsId") Integer id, Model model) {
model.addAttribute("news", (id != null && id != 0) ? newsService.getById(id) : new News());
@ -35,7 +43,7 @@ public class NewsController {
@GetMapping("/news/{newsId}")
public String viewNews(@PathVariable(value = "newsId") Integer id, Model model) {
model.addAttribute("news", id != null ? newsService.getById(id) : new News());
return "news";
return "viewNews";
}
@PostMapping("saveNews")
@ -45,12 +53,12 @@ public class NewsController {
}
newsService.save(news);
return "redirect:/";
return "redirect:/news/news";
}
@GetMapping("deleteNews/{newsId}")
public String delete(@PathVariable(value = "newsId") Integer id) {
newsService.delete(id);
return "redirect:/";
return "redirect:/news/news";
}
}

View File

@ -3,5 +3,8 @@ package ru.ulstu.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.model.News;
import java.util.List;
public interface NewsRepository extends JpaRepository<News, Integer> {
List<News> findFirst3ByOrderByDateDesc();
}

View File

@ -46,4 +46,8 @@ public class NewsService {
public void delete(Integer id) {
newsRepository.deleteById(id);
}
public List<News> getLast() {
return newsRepository.findFirst3ByOrderByDateDesc();
}
}

View File

@ -29,6 +29,6 @@
color: black;
}
.link-dark, .link-dark:visited, .link-dark:focus {
.link-dark, .link-dark:visited, .link-dark:focus, .link-dark:any-link {
color: black;
}

View File

@ -33,7 +33,7 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/news">Новости</a>
<a class="nav-link" href="/news/news">Новости</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/news">Заседания</a>

View File

@ -10,7 +10,7 @@
layout:decorate="~{default}">
<div class="container" layout:fragment="content">
<h3>Редактирование новости:</h3>
<form action="#" th:action="@{/saveNews}" th:object="${news}" method="post">
<form action="#" th:action="@{/news/saveNews}" th:object="${news}" method="post">
<input type="hidden" th:field="*{id}">
<input type="hidden" th:field="*{date}">
<input type="hidden" th:field="*{version}">

View File

@ -17,13 +17,13 @@
<div class="col-md-8">
<div class="row">
<div class="col-md-10">
<a th:href="@{'/news/' + ${n.id}}" class="link-dark"><h5 th:text="${n.title}"/></a>
<a th:href="@{'/news/news/' + ${n.id}}" class="link-dark"><h5 th:text="${n.title}"/></a>
</div>
<div sec:authorize="hasRole('ROLE_ADMIN')" class="col-md-2" style="text-align: right">
<a th:href="@{'/editNews/' + ${n.id}}" class="link-dark">
<a th:href="@{'/news/editNews/' + ${n.id}}" class="link-dark">
<i class="fa fa-pencil" aria-hidden="true"></i>
</a>
<a th:href="@{'/deleteNews/' + ${n.id}}" class="link-dark"
<a th:href="@{'/news/deleteNews/' + ${n.id}}" class="link-dark"
onclick="return confirm('Удалить новость?')">
<i class="fa fa-trash" aria-hidden="true"></i>
</a>

View File

@ -5,21 +5,36 @@
-->
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
layout:decorate="~{default}">
<div class="container" layout:fragment="content">
<div th:each="n : ${news}" class="news">
<div class="row">
<div class="col-md-4">
<img class="news-image" src="/img/logo.svg"/>
</div>
<div class="col-md-8">
<h5 th:text="${news.title}"/>
<div th:text="${news.text}" class="news-item"></div>
<div class="row">
<div class="col-md-10">
<a th:href="@{'/news/news/' + ${n.id}}" class="link-dark"><h5 th:text="${n.title}"/></a>
</div>
<div sec:authorize="hasRole('ROLE_ADMIN')" class="col-md-2" style="text-align: right">
<a th:href="@{'/news/editNews/' + ${n.id}}" class="link-dark">
<i class="fa fa-pencil" aria-hidden="true"></i>
</a>
<a th:href="@{'/news/deleteNews/' + ${n.id}}" class="link-dark"
onclick="return confirm('Удалить новость?')">
<i class="fa fa-trash" aria-hidden="true"></i>
</a>
</div>
</div>
<div th:text="${'Опубликовано: ' + #calendars.format(news.date, 'dd.MM.yyyy HH:mm')}"
<div th:text="${n.preview}" class="news-item"></div>
</div>
</div>
<div th:text="${'Опубликовано: ' + #calendars.format(n.date, 'dd.MM.yyyy HH:mm')}"
class="news-date"></div>
<a href="javascript:history.back()" class="btn btn-outline-dark" style="text-align: right">Назад</a>
<hr/>
</div>
</div>
</html>

View File

@ -0,0 +1,25 @@
<!--
~ Copyright (C) 2021 Anton Romanov - All Rights Reserved
~ You may use, distribute and modify this code, please write to: romanov73@gmail.com.
~
-->
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
layout:decorate="~{default}">
<div class="container" layout:fragment="content">
<div class="row">
<div class="col-md-4">
<img class="news-image" src="/img/logo.svg"/>
</div>
<div class="col-md-8">
<h5 th:text="${news.title}"/>
<div th:text="${news.text}" class="news-item"></div>
</div>
</div>
<div th:text="${'Опубликовано: ' + #calendars.format(news.date, 'dd.MM.yyyy HH:mm')}"
class="news-date"></div>
<a href="javascript:history.back()" class="btn btn-outline-dark" style="text-align: right">Назад</a>
</div>
</html>