Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### TarefasController V2
- ```java
- package br.com.caelum.tarefas.controller;
- import java.util.List;
- import javax.validation.Valid;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.validation.BindingResult;
- import org.springframework.web.bind.annotation.RequestMapping;
- import br.com.caelum.tarefas.dao.JdbcTarefaDao;
- import br.com.caelum.tarefas.modelo.Tarefa;
- @Controller
- public class TarefasController {
- private JdbcTarefaDao tarefaDao;
- public TarefasController() {
- this.tarefaDao = new JdbcTarefaDao();
- }
- @RequestMapping("/novaTarefa")
- public String form() {
- return "tarefas/formulario";
- }
- @RequestMapping("adicionaTarefa")
- public String adiciona(@Valid Tarefa tarefa, BindingResult result) {
- if (result.hasFieldErrors("descricao")) {
- return "tarefas/formulario";
- }
- this.tarefaDao.adiciona(tarefa);
- return "tarefas/adicionada";
- }
- @RequestMapping(value = {"listaTarefas", "/"})
- public String lista(Model model) {
- List<Tarefa> tarefas = this.tarefaDao.lista();
- model.addAttribute("tarefas", tarefas);
- return "tarefas/lista";
- }
- @RequestMapping("removeTarefa")
- public String remove(Tarefa tarefa) {
- this.tarefaDao.remove(tarefa);
- return "redirect:listaTarefas";
- }
- @RequestMapping("mostraTarefa")
- public String mostra(Long id, Model model) {
- Tarefa tarefa = this.tarefaDao.buscaPorId(id);
- model.addAttribute("tarefa", tarefa);
- return "tarefas/mostra";
- }
- @RequestMapping("alteraTarefa")
- public String altera(@Valid Tarefa tarefa, BindingResult result) {
- if (result.hasErrors()) {
- return "tarefas/mostra";
- }
- this.tarefaDao.altera(tarefa);
- return "redirect:listaTarefas";
- }
- @RequestMapping("finalizaTarefa")
- public String finaliza(Long id, Model model) {
- this.tarefaDao.finaliza(id);
- model.addAttribute("tarefa", this.tarefaDao.buscaPorId(id));
- return "tarefas/finalizada";
- }
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement