@Controller
public class ArticleController {
@Autowired
UserRepository userRepository;
@Autowired
ArticleRepositoryImpl articleRepository;
@Autowired
CategoryRepository categoryRepository;
@PostMapping("/finishArticle")
public String saveArticle(@ModelAttribute("article") Article article, @AuthenticationPrincipal UserDetails userDetails){
User user = userRepository.findUserByName(userDetails.getUsername());
article.setWriter(user);
user.addWrittenArticles(article);
articleRepository.save(article);
return "home";
}
@PostMapping("/saveArticle")
public String saveTheArticle(@ModelAttribute("tempArticles") Article article, @AuthenticationPrincipal UserDetails userDetails){
System.out.println(article);
/*
User user = userRepository.findUserByName(userDetails.getUsername());
Article dbArticle = articleRepository.findById(article.getId());
dbArticle.addSaver(user);
articleRepository.save(article);
*/return "home";
}
@PostMapping("/search")
public String showSearchedArticles(@ModelAttribute("tempCategory") Category category, Model model){
//Category dbCategory = categoryRepository.findByTitle(category.getTi tle());
System.out.println(category);
// List<Article> articleList = dbCategory.getArticles();
//model.addAttribute("articlelist",articleList);
return "home";
}
}
@Controller
@RequestMapping("/*{username}")
public class DemoController {
@Autowired
private CategoryRepository categoryRepository;
@Autowired
private ArticleRepository articleRepository;
@Autowired
private UserRepository userRepository;
@GetMapping()
public String showHome(Model model){
List<Category> categories = categoryRepository.findAll();
Category category = new Category();
model.addAttribute("category",category);
model.addAttribute("categories",categories);
return "home";
}
@GetMapping("/write")
public String showWritingPage(Model model){
Article article = new Article();
List<Category> categories = categoryRepository.findAll();
model.addAttribute("categories",categories);
model.addAttribute("article", article);
model.addAttribute("category",article.getCategory());
model.addAttribute("writer",article.getWriter());
return "writing-page";
}
@GetMapping("/showwritedArticles")
public String showWritedArticles(Model model, @AuthenticationPrincipal UserDetails userDetails){
User user = userRepository.findUserByName(userDetails.getUsername());
List<Article> articles= userRepository.findUserByName(user.getName()).getWrittenArticles();
model.addAttribute("articles",articles);
return "writedArticles";
}
@GetMapping("/showsavedArticles")
public String showSavedArticles(@AuthenticationPrincipal UserDetails userDetails, Model model){
User user = userRepository.findUserByName(userDetails.getUsername());
model.addAttribute("savedarticles",user.getSavedArticles());
return"savedArticles";
}
home.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h2>
Hi
</h2>
<form action="#" th:action="@{/logout}" method="post">
<input type="submit" value="Logout"/>
</form>
<a th:href="@{${#authentication.name}+'/write'}"> MAKALE YAZ</a>
<br><br>
<a th:href="@{${#authentication.name}+'/showwritedArticles'}"> YAZILAN MAKALELER</a>
<br><br>
MAKALE ARA <br><br>
<br><br>
<a th:href="@{${#authentication.name}+'/showsavedArticles'}">KAYDEDİLMİŞ MAKALELER</a>
<br><br>
<form action="#" th:action="@{/search}" th:object="${category}" method="post">
<select th:object="${category}">
<option th:each="tempCategory : ${categories}" th:value="${tempCategory.id}"th:text="${tempCategory.title}"></option>
</select>
<input type="submit"value="ARA">
</form>
</body>
</html>
writedArticles.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Medium Makale Görüntüleme</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
.container {
max-width: 800px;
margin: auto;
}
.article {
margin-bottom: 40px;
}
.article-title {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.article-meta {
color: #666;
margin-bottom: 20px;
}
.article-content {
line-height: 1.6;
}
</style>
</head>
<body>
<div class="container">
<!-- Makale bilgileri -->
<div class="article" th:each="tempArticles : ${articles}">
<h1 class="article-title" th:text="${tempArticles.title}">Makale Başlığı</h1>
<p class="article-meta" th:text="${tempArticles.writer}">Yazar - Tarih</p>
<div class="article-content" th:utext="${tempArticles.text}">
Makale içeriği buraya gelecek.
</div>
<div>
<form action="#" th:action="@{/saveArticle}" th:object="${tempArticles}" method="post">
<input type="submit" value="KAYDET">
</form>
</div>
</div>
</div>
</body>
</html>
</body>
</html>
Hi everyone!
I try the search articles by category in home.html category is null on DemoController class
and when i try the save article which one i’ve writeen before in writedArticles.html article is null on ArticleController class?
I don’t know why? Is there anyone can help?
It’s my first question on Stackoverflow so sorry for my mistakes in the question :/
By the way i am beginner in Spring MVC/Boot and Thymeleaf
Hi everyone!
I try the search articles by category in home.html category is null on DemoController class
and when i try the save article which one i’ve writeen before in writedArticles.html article is null on ArticleController class?
I don’t know why? Is there anyone can help?
It’s my first question on Stackoverflow so sorry for my mistakes in the question :/
By the way i am beginner in Spring MVC/Boot and Thymeleaf
Ayberk Tore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.