estou iniciando estudos em JavaWeb, já consegui fazer um CRUD, só que na hora que escolho um objeto para ser editado, ao montar esse objeto no formulário e quando vou atualizar, está duplicando o Objeto, ou seja ele cria um novo objeto e permanece o que está. Segue meu codigo.
<%@ 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>Atualização de Tarefa</title>
<link href="css/jquery-ui.min.css" rel="stylesheet">
<link href="css/estilo.css" rel="stylesheet">
<script src="js/jquery.js"></script>
<script src="js/jquery-ui.min.js"></script>
</head>
<body>
<h3>Atualização de Tarefa - ${tarefa.id}</h3>
<form action="mvc?logica=AdicionaAlteraTarefaLogic" method="post">
Data: <input type="text" name="data" value="<fmt:formatDate value="${tarefa.data.time}" pattern="dd/MM/yyyy" />" /> <br />
Descricao: <textarea rows="7" cols="20" name="descricao">${tarefa.descricao}</textarea> <br>
<input type="submit" value="Atualizar">
</form>
</body>
</html>
public class AdicionaAlteraTarefaLogic implements Logica {
@Override
public String executa(HttpServletRequest request, HttpServletResponse response) {
Integer numId;
String id = request.getParameter("id");
String data = request.getParameter("data");
String descricao = request.getParameter("descricao");
Calendar dataTarefa = Calendar.getInstance();
try {
Date dataconvertida = new SimpleDateFormat("dd/MM/yyyy").parse(data);
dataTarefa.setTime(dataconvertida);
Tarefa tarefa = new Tarefa();
tarefa.setData(dataTarefa);
tarefa.setDescricao(descricao);
TarefaDAO dao = new TarefaDAO();
if(id != null){
numId = Integer.parseInt(id);
tarefa.setId(numId);
dao.atualizaTarefa(tarefa);
}else{
dao.adiciona(tarefa);
}
} catch (ParseException | SQLException e) {
e.printStackTrace();
}
return "mvc?logica=ListarTarefaLogic";
}
public void adiciona(Tarefa tarefa) {
String sql = "insert into tarefa(descricao,data) values (?,?) ";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
montaTarefa(tarefa, stmt);
stmt.execute();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void montaTarefa(Tarefa tarefa, PreparedStatement stmt) throws SQLException {
stmt.setString(1, tarefa.getDescricao());
stmt.setDate(2, new Date(tarefa.getData().getTimeInMillis()));
}
public void atualizaTarefa(Tarefa tarefa){
String sql = "update tarefa set descricao = ?, data = ? where id = ?";
try (PreparedStatement pstm = connection.prepareStatement(sql)){
pstm.setString(1, tarefa.getDescricao());
pstm.setDate(2, new Date(tarefa.getData().getTimeInMillis()));
pstm.setInt(3, tarefa.getId());
pstm.execute();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
package br.com.triadworks.todoList.model;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Tarefa {
private Integer id;
private String descricao;
private Calendar data;
public Tarefa(String descricao, Calendar data) {
this.descricao = descricao;
this.data = data;
}
public Tarefa() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public Calendar getData() {
return data;
}
public void setData(Calendar data) {
this.data = data;
}
public String getDataFormatada() {
return new SimpleDateFormat("dd/MM/yyyy").format(this.data.getTime());
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Tarefa other = (Tarefa) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}