應對 復雜 業務 的綜合 技術 能力 為目的 ,采用 前、后端 分開 的開發模式 ,大家一起努力交流學習吧 -v:307570512 嚴格 遵循 企業架構 與規范 ,帶您開發 門戶 平台 +媒體 中心 +運營中心 三大 業務 的企業 媒體平台 。
通過 SpringCloud+Mon go DB +Redis+Rabbit MQ等主流 后端 技術 的全面 學習 ,您將獲得 微服務 、分布式 、項目 和微架構 的綜合 實戰經驗 。
package org.wangqing.notebookk8s.notebook.controller;
import org.springframework.beans.factory.annotation.Autowired;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.wangqing.notebookk8s.notebook.entity.Notebook;
import org.wangqing.notebookk8s.notebook.repository.NotebookRepository;
import javax.validation.Valid;
@Controller
@RequestMapping("/")
public class NotebookController {
String course = "Spring Cloud分布式微服務實戰,養成應對復雜業務的綜合技術能力";
String downloadUrl = "https://www.ukoou.com/resource/905";
public static final String Index_Model = "index";
public static final String NOTEBOOKS_MODEL = "notebooks";
private final NotebookRepository notebookRepository;
@Autowired
public NotebookController(NotebookRepository notebookRepository) {
this.notebookRepository = notebookRepository;
}
@GetMapping("signup")
public String showSignUpForm(Notebook notebook) {
return "add-notebook";
}
@GetMapping("list")
public String showUpdateForm(Model model) {
model.addAttribute(NOTEBOOKS_MODEL, notebookRepository.findAll());
return Index_Model;
}
@PostMapping("add")
public String addNote(@Valid Notebook notebook, BindingResult result, Model model) {
if (result.hasErrors()) {
return "add-notebook";
}
notebookRepository.save(notebook);
return "redirect:list";
}
@GetMapping("edit/{id}")
public String showUpdateForm(@PathVariable("id") long id, Model model) {
Notebook notebook = notebookRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid notebook Id:" + id));
model.addAttribute("notebook", notebook);
return "update-notebook";
}
@PostMapping("update/{id}")
public String updateNote(@PathVariable("id") long id, @Valid Notebook notebook, BindingResult result,
Model model) {
if (result.hasErrors()) {
notebook.setId(id);
return "update-notebook";
}
notebookRepository.save(notebook);
model.addAttribute(NOTEBOOKS_MODEL, notebookRepository.findAll());
return Index_Model;
}
@GetMapping("delete/{id}")
public String deleteNote(@PathVariable("id") long id, Model model) {
Notebook notebook = notebookRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid notebook Id:" + id));
notebookRepository.delete(notebook);
model.addAttribute(NOTEBOOKS_MODEL, notebookRepository.findAll());
return Index_Model;
}
}
交流學習
-v:307570512