Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. ### TarefasController V2
  2.  
  3. ```java
  4. package br.com.caelum.tarefas.controller;
  5.  
  6. import java.util.List;
  7.  
  8. import javax.validation.Valid;
  9.  
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.ui.Model;
  12. import org.springframework.validation.BindingResult;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14.  
  15. import br.com.caelum.tarefas.dao.JdbcTarefaDao;
  16. import br.com.caelum.tarefas.modelo.Tarefa;
  17.  
  18. @Controller
  19. public class TarefasController {
  20.  
  21. private JdbcTarefaDao tarefaDao;
  22.  
  23. public TarefasController() {
  24. this.tarefaDao = new JdbcTarefaDao();
  25. }
  26.  
  27.  
  28. @RequestMapping("/novaTarefa")
  29. public String form() {
  30. return "tarefas/formulario";
  31. }
  32.  
  33. @RequestMapping("adicionaTarefa")
  34. public String adiciona(@Valid Tarefa tarefa, BindingResult result) {
  35. if (result.hasFieldErrors("descricao")) {
  36. return "tarefas/formulario";
  37. }
  38. this.tarefaDao.adiciona(tarefa);
  39. return "tarefas/adicionada";
  40. }
  41.  
  42. @RequestMapping(value = {"listaTarefas", "/"})
  43. public String lista(Model model) {
  44. List<Tarefa> tarefas = this.tarefaDao.lista();
  45. model.addAttribute("tarefas", tarefas);
  46. return "tarefas/lista";
  47. }
  48.  
  49. @RequestMapping("removeTarefa")
  50. public String remove(Tarefa tarefa) {
  51. this.tarefaDao.remove(tarefa);
  52. return "redirect:listaTarefas";
  53. }
  54.  
  55. @RequestMapping("mostraTarefa")
  56. public String mostra(Long id, Model model) {
  57. Tarefa tarefa = this.tarefaDao.buscaPorId(id);
  58. model.addAttribute("tarefa", tarefa);
  59. return "tarefas/mostra";
  60. }
  61.  
  62. @RequestMapping("alteraTarefa")
  63. public String altera(@Valid Tarefa tarefa, BindingResult result) {
  64. if (result.hasErrors()) {
  65. return "tarefas/mostra";
  66. }
  67. this.tarefaDao.altera(tarefa);
  68. return "redirect:listaTarefas";
  69. }
  70.  
  71. @RequestMapping("finalizaTarefa")
  72. public String finaliza(Long id, Model model) {
  73. this.tarefaDao.finaliza(id);
  74. model.addAttribute("tarefa", this.tarefaDao.buscaPorId(id));
  75. return "tarefas/finalizada";
  76. }
  77. }
  78. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement