#8 -- Add router component. You can access to url's in mvc controllers also in web page templates.

merge-requests/15/head
Anton Romanov 3 years ago
parent e348b37625
commit 33bf0bd8f3

@ -1,3 +1,8 @@
/*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/
package ru.ulstu.extractor.config;
import org.springframework.context.annotation.Configuration;
@ -5,12 +10,14 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import static ru.ulstu.extractor.controller.Route.LIST_INDEXED_REPOSITORIES;
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/{articlename:\\w+}");
registry.addRedirectViewController("/", "/newRepo");
registry.addRedirectViewController("/", LIST_INDEXED_REPOSITORIES);
registry.addRedirectViewController("/default", "/home");
}

@ -1,3 +1,8 @@
/*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/
package ru.ulstu.extractor.controller;
import org.springframework.stereotype.Controller;
@ -7,6 +12,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.extractor.repository.BranchRepository;
import ru.ulstu.extractor.repository.RepositoryRepository;
import static ru.ulstu.extractor.controller.Route.LIST_REPOSITORY_BRANCHES;
@Controller
public class BranchController {
private final RepositoryRepository repositoryRepository;
@ -17,12 +24,12 @@ public class BranchController {
this.branchRepository = branchRepository;
}
@GetMapping("/details")
@GetMapping(LIST_REPOSITORY_BRANCHES)
public String indexBranch(
Model model,
@RequestParam int repositoryId) {
model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId));
model.addAttribute("repository", repositoryRepository.findById(repositoryId).get());
return "indexBranch";
return LIST_REPOSITORY_BRANCHES;
}
}

@ -1,3 +1,8 @@
/*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/
package ru.ulstu.extractor.controller;
import org.springframework.data.domain.Page;
@ -18,6 +23,8 @@ import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static ru.ulstu.extractor.controller.Route.FILTER_COMMITS;
@Controller
public class GitFilteringController {
private final FilteringService filteringService;
@ -40,7 +47,7 @@ public class GitFilteringController {
return "resultRepo";
}*/
@RequestMapping(value = "/filtering", method = RequestMethod.GET)
@RequestMapping(value = FILTER_COMMITS, method = RequestMethod.GET)
public String listCommits(
Model model,
@ModelAttribute FilterForm filterForm,
@ -64,6 +71,6 @@ public class GitFilteringController {
filterForm.setBranch(branchName);
filterForm.setUrl(repositoryUrl);
model.addAttribute("filterForm", filterForm);
return "filtering";
return FILTER_COMMITS;
}
}

@ -1,3 +1,8 @@
/*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/
package ru.ulstu.extractor.controller;
import org.springframework.stereotype.Controller;
@ -15,6 +20,9 @@ import ru.ulstu.extractor.service.IndexService;
import java.util.List;
import static ru.ulstu.extractor.controller.Route.FILTER_COMMITS;
import static ru.ulstu.extractor.controller.Route.INDEXING_NEW_REPOSITORY;
@Controller
public class GitIndexingController {
private final GitRepositoryService gitRepositoryService;
@ -26,40 +34,39 @@ public class GitIndexingController {
this.indexService = indexService;
}
@GetMapping("/newRepo")
@GetMapping(INDEXING_NEW_REPOSITORY)
public String indexNewRepo(Model model) {
model.addAttribute(new RepoForm());
return "newRepo";
return INDEXING_NEW_REPOSITORY;
}
@RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "send")
@RequestMapping(value = INDEXING_NEW_REPOSITORY, method = RequestMethod.POST, params = "send")
public String getBranch(@ModelAttribute RepoForm repoForm, Model model) {
try {
gitRepositoryService.cloneOrUpdateRepo(repoForm.getRepo());
List<Branch> branches = gitRepositoryService.getBranches(repoForm.getRepo());
model.addAttribute("branches", branches);
return "newRepo";
} catch (Exception ex) {
model.addAttribute("error", ex.getMessage());
return "newRepo";
}
return INDEXING_NEW_REPOSITORY;
}
@RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "next")
@RequestMapping(value = INDEXING_NEW_REPOSITORY, method = RequestMethod.POST, params = "next")
public String setBranch(@ModelAttribute RepoForm repoForm, Model model, RedirectAttributes redirectAttributes) {
model.addAttribute("filterForm", new FilterForm(repoForm.getRepo()));
if (repoForm.getBranch() == null) {
return "newRepo";
return INDEXING_NEW_REPOSITORY;
} else {
try {
indexService.index(repoForm.getRepo(), repoForm.getBranch());
} catch (Exception ex) {
model.addAttribute("error", ex.getMessage());
return "newRepo";
return INDEXING_NEW_REPOSITORY;
}
redirectAttributes.addAttribute("repositoryUrl", repoForm.getRepo());
redirectAttributes.addAttribute("branchName", repoForm.getBranch());
return "redirect:/filtering";
return "redirect:/" + FILTER_COMMITS;
}
}
}

@ -1,3 +1,8 @@
/*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/
package ru.ulstu.extractor.controller;
import org.springframework.stereotype.Controller;
@ -5,17 +10,19 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import ru.ulstu.extractor.repository.RepositoryRepository;
import static ru.ulstu.extractor.controller.Route.LIST_INDEXED_REPOSITORIES;
@Controller
public class BaseIndexingController {
public class RepositoryController {
private final RepositoryRepository repositoryRepository;
public BaseIndexingController(RepositoryRepository repositoryRepository) {
public RepositoryController(RepositoryRepository repositoryRepository) {
this.repositoryRepository = repositoryRepository;
}
@GetMapping("/indexRepo")
@GetMapping(LIST_INDEXED_REPOSITORIES)
public String indexNewRepo(Model model) {
model.addAttribute("repositories", repositoryRepository.findAll());
return "indexRepo";
return LIST_INDEXED_REPOSITORIES;
}
}

@ -0,0 +1,32 @@
/*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/
package ru.ulstu.extractor.controller;
import org.springframework.stereotype.Component;
@Component
public class Route {
public static final String LIST_INDEXED_REPOSITORIES = "listRepositories";
public static final String LIST_REPOSITORY_BRANCHES = "listBranches";
public static final String INDEXING_NEW_REPOSITORY = "indexNewRepository";
public static final String FILTER_COMMITS = "filterCommits";
public static String getLIST_INDEXED_REPOSITORIES() {
return LIST_INDEXED_REPOSITORIES;
}
public static String getLIST_REPOSITORY_BRANCHES() {
return LIST_REPOSITORY_BRANCHES;
}
public static String getINDEXING_NEW_REPOSITORY() {
return INDEXING_NEW_REPOSITORY;
}
public static String getFILTER_COMMITS() {
return FILTER_COMMITS;
}
}

@ -1,3 +1,8 @@
<!--
~ Copyright (C) 2021 Anton Romanov - All Rights Reserved
~ You may use, distribute and modify this code, please write to: romanov73@gmail.com.
-->
<!DOCTYPE html>
<html lang="ru"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml">
@ -22,10 +27,12 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/" th:text="#{messages.menu.new-repo}">Link</a>
<a class="nav-link" th:href="${@route.INDEXING_NEW_REPOSITORY}"
th:text="#{messages.menu.new-repo}">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/indexRepo" th:text="#{messages.menu.indexed-repos}">Link</a>
<a class="nav-link" th:href="${@route.LIST_INDEXED_REPOSITORIES}"
th:text="#{messages.menu.indexed-repos}">Link</a>
</li>
</ul>
</div>

@ -1,13 +1,18 @@
<!--
~ 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:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
<title>Простая обработка формы на Spring MVC</title>
<title>Фильтрация коммитов репозитория</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<form action="#" th:action="@{/sendFilter}" th:object="${filterForm}" method="post">
<form action="#" th:action="${@route.FILTER_COMMITS}" th:object="${filterForm}" method="post">
<p><b>Фильтровать данные:</b><Br></p>
По автору
<select class="selectpicker" data-live-search="true">

@ -1,32 +0,0 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
<title>Простая обработка формы на Spring MVC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<h1>Форма</h1>
<form action="#" th:action="@{/sendEmail}" th:object="${emailForm}" method="post">
<p style="color:red" th:text="${error}"></p>
<table>
<tr>
<td>Тема:</td>
<td><input type="text" th:field="*{subject}"/></td>
</tr>
<tr>
<td>Кому:</td>
<td><input type="text" th:field="*{to}"/></td>
</tr>
<tr>
<td>Сообщение:</td>
<td><textarea th:field="*{message}"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Отправить"/></td>
</tr>
</table>
</form>
</div>
</html>

@ -1,3 +1,8 @@
<!--
~ 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:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
@ -7,7 +12,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<form action="#" th:action="@{/newRepo}" th:object="${repoForm}" method="post">
<form action="#" th:action="${@route.INDEXING_NEW_REPOSITORY}" th:object="${repoForm}" method="post">
<p style="color:red" th:text="${error}"></p>
<button class="btn btn-outline-dark dropdown-toggle" type="button" data-toggle="collapse"
data-target="#collapseOne" aria-expanded="false" aria-controls="collapseExample"

@ -1,3 +1,8 @@
<!--
~ 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:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
@ -15,7 +20,8 @@
</thead>
<tbody>
<tr th:each="branch: ${branches}">
<td><a th:href="@{'/filtering?branchName='+${branch.name}+'&repositoryUrl='+${repository.url}}"
<td>
<a th:href="@{${@route.FILTER_COMMITS} + '?branchName='+${branch.name}+'&repositoryUrl='+${repository.url}}"
th:text="${branch.name}"/></td>
</tr>
</tbody>

@ -1,3 +1,8 @@
<!--
~ 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:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
@ -15,7 +20,8 @@
</thead>
<tbody>
<tr th:each="repo: ${repositories}">
<td><a th:href="@{'/details?repositoryId='+${repo.id}}" th:text="${repo.url}"></td>
<td><a th:href="@{${@route.LIST_REPOSITORY_BRANCHES} + '?repositoryId=' + ${repo.id}}"
th:text="${repo.url}"></a></td>
</tr>
</tbody>
</table>

@ -1,18 +0,0 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
<title>Простая обработка формы на Spring MVC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<body>
<form oninput="result">
<p>Данные репозитория:</p>
<p>
<output name="result"></output>
</form>
</body>
</div>
</html>
Loading…
Cancel
Save