Controller
package br.com.caelum.contas.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
import br.com.caelum.contas.dao.ContaDAO;
import br.com.caelum.contas.modelo.Conta;
@Controller
public class ContaController {
@RequestMapping("/form")
public String form() {
return "formulario";
}
@RequestMapping("/adicionaConta")
public String adicionaConta(Conta c) {
ContaDAO dao = new ContaDAO();
dao.adiciona(c);
return "sucesso";
}
@RequestMapping("/deletaConta")
public String deleta(Conta conta) {
System.out.println(conta.getId());
new ContaDAO().remove(conta);
return "redirect:listaContas";
}
@RequestMapping("/listaContas")
public String lista(Model mv) {
ContaDAO dao = new ContaDAO();
List<Conta> contas = dao.lista();
//ModelAndView mv = new ModelAndView("conta/lista");
//mv.addObject("contas", contas);
System.out.println(contas);
mv.addAttribute("contas",contas);
return "conta/lista";
}
@RequestMapping("/mostraConta")
public String mostra(Long id, Model model) {
ContaDAO dao = new ContaDAO();
model.addAttribute("conta", dao.buscaPorId(id));
return "conta/mostra";
}
@RequestMapping("/alteraConta")
public String altera(Conta conta) {
System.out.println(conta.getId());
ContaDAO dao = new ContaDAO();
dao.altera(conta);
return "redirect:listaContas";
}
}
JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Detalhe conta</title>
</head>
<body>
<h3>Alterar Conta</h3>
<form action="alteraConta" method="post">
Descrição: <br/>
<textarea name="descricao" rows="5" cols="100">${conta.descricao}</textarea>
<br/>
Valor: <br/>
<input type="text" name="valor" />
Tipo: <br/>
<select name="tipo">
<option value="ENTRADA" ${conta.tipo=='ENTRADA' ? 'selected':''}>Entrada</option>
<option value="SAIDA" ${conta.tipo=='SAIDA' ? 'selected':''}>Saída</option>
</select>
<br/>
Pago? <input type="checkbox" name="paga" ${conta.paga?'checked':''} />
<br/>
Data de Pagamento: <input type="text" name="dataPagamento" value="<fmt:formatDate value="${conta.dataPagamento.time}" pattern="dd/MM/yyyy" />" />
<br/>
<input type="hidden" name="id" value="${conta.id}"/>
<input type="submit" value="Alterar"/>
</form>
</body>
</html>
Erro que aparece no navegador:
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect.
Apache Tomcat/7.0.42