Olá,
[Desculpem mas a tag <code> não estava funcionando]
Este código está correto?
Posso/devo fazer as chamdas para inserção de Paises e Estados na Action?
Estou usando corretamente o DAO?
Tem uma maneira melhor de tratar a inclusão de Estados(estou pegando array’s de String e populando objetos Estado)?
Alguma sugestão?
//Pais
import java.util.ArrayList;
public class Pais {
private String nome = "";
private String sigla = "";
private String[] siglaestado = null;
private String[] nomeestado = null;
private ArrayList<Estado> estados = new ArrayList();
public String getNome(){
return nome;
}
public void setNome(String nome){
this.nome = nome;
}
public String getSigla(){
return sigla;
}
public void setSigla(String sigla){
this.sigla = sigla;
}
public ArrayList<Estado> getEstados()
{
return estados;
}
public void setEstados( ArrayList<Estado> p_estados )
{
this.estados = p_estados;
}
public String[] getSiglaestado(){
return siglaestado;
}
public void setSiglaestado(String[] siglaEstado){
this.siglaestado = siglaEstado;
}
public String[] getNomeestado(){
return nomeestado;
}
public void setNomeestado(String[] nomeEstado){
this.nomeestado = nomeEstado;
}
}
//Estado
public class Estado {
private String nomeEstado = "";
private String siglaEstado = "";
private String siglaPais = "";
public Estado(String sigla, String nome, String sgPais){
nomeEstado = nome;
siglaEstado = sigla;
siglaPais = sgPais;
}
public String getNomeEstado(){
return nomeEstado;
}
public void setNomeEstado(String nome){
this.nomeEstado = nome;
}
public String getSiglaEstado(){
return siglaEstado;
}
public void setSiglaEstado(String sigla){
this.siglaEstado = sigla;
}
public String getSiglaPais(){
return siglaPais;
}
public void setSiglaPais(String siglaPais){
this.siglaPais = siglaPais;
}
}
//Action
public class PaisAction extends DispatchAction{
java.sql.Connection conn = null;
public ActionForward insert(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
try {
Estado estado = null;
conn = Connect.getConnection();
PaisDAO paisDAO = new PaisDAO();
DynaActionForm paisForm =(DynaActionForm)form;
Pais pais = new Pais();
BeanUtils.copyProperties(pais, paisForm);
String siglas[];
String nomes[];
siglas = (String[])paisForm.get("siglaestado");
nomes = (String[])paisForm.get("nomeestado");
HttpSession session = request.getSession();
paisDAO.conn = conn;
paisDAO.insertPais(pais);
for( int i = 0; i < siglas.length; i++ ){
estado = new Estado(siglas[i], nomes[i], pais.getSigla());
paisDAO.insertEstado(estado);
}
} catch (Exception sqle) {
getServlet().log("Connection.process", sqle);
} finally {
//Devolve connection ao pool
try {
conn.close();
} catch (Exception e) {
getServlet().log("Connection.close", e);
}
}
return (mapping.findForward("success"));
}
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
String sigla = null;
try {
conn = Connect.getConnection();
sigla = request.getParameter("sigla");
PaisDAO pais = new PaisDAO();
pais.conn = conn;
pais.deletePais(sigla);
} catch (Exception sqle) {
getServlet().log("Connection.process", sqle);
} finally {
//Devolve connection ao pool
try {
conn.close();
} catch (Exception e) {
getServlet().log("Connection.close", e);
}
}
return (mapping.findForward("success"));
}
.
.
.
//Form HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<head>
<link href="<c:url value='main.css'/>" rel="stylesheet" type="text/css"/>
<title>Untitled</title>
</head>
<body>
<%
int indice_Estado = 0;
%>
<html:form action="/Incluir?action=insert" method="POST">
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td>Sigla:</td>
<td><html:text property="sigla" name="PaisForm"/>
</td>
</tr>
<tr>
<td>País:</td>
<td><html:text property="nome" name="PaisForm"/>
</td>
</tr>
<tr>
<TD colspan="2" align="right">
<input type="button" value="Salvar" onclick="Salvar();">
</TD>
</tr>
</table>
<P>
<img >
<table border="1" cellspacing="1" cellpadding="1">
<tbody id="TbGrid_Estado">
<tr>
<th>Sigla</th>
<th>Nome</th>
<th> </th>
</tr>
<logic:iterate name="ListaEstados" id="estado">
<%
indice_Estado++;
%>
<tr name="LinEstado<%=indice_Estado%>" id="LinEstado<%=indice_Estado%>">
<td class="nowrap"><input type="text" name="siglaestado" value="<bean:write name="estado" property="siglaEstado"/>"></td>
<td class="nowrap"><input type="text" name="nomeestado" value="<bean:write name="estado" property="nomeEstado"/>"></td>
<td class="nowrap">
<img >
</td>
</tr>
</logic:iterate>
</tbody>
</table>
</html:form>
</body>
</html:html>
<script>
var indice_Estado;
indice_Estado = 0;
function Salvar(){
paisForm.action = "/minhapp/Incluir.do?action=save";
paisForm.submit();
}
function Inserir(){
paisForm.action = "/minhapp/Incluir.do?action=insert";
paisForm.submit();
}
.
.
.
//PaisDao
public class PaisDAO{
protected Connection conn = null;
protected String message = "";
private String nome = "";
private String sigla = "";
public String getNome(){
return nome;
}
public void setNome(String nome){
this.nome = nome;
}
public String getSigla(){
return sigla;
}
public void setSigla(String sigla){
this.sigla = sigla;
}
// Insert
public void insertPais(Pais pais) throws SQLException {
PreparedStatement pstmt = null;
SQLException error = null;
try {
String Sql = "INSERT INTO Pais(Sigla_Pais, Pais) Values (?, ?)";
pstmt = conn.prepareStatement(Sql);
pstmt.setString(1, pais.getSigla());
pstmt.setString(2, pais.getNome());
pstmt.executeUpdate();
message = "País incluído com sucesso!";
}catch (SQLException e) {
error = e;
message = "Erro ao criar País.";
}
pstmt.close();
pstmt = null;
}
public ArrayList getAllPais() throws SQLException {
PreparedStatement pstmt = null;
SQLException error = null;
ResultSet RsPais = null;
ArrayList paises = new ArrayList();
try {
String Sql = "Select * from Pais";
pstmt = conn.prepareStatement(Sql);
RsPais = pstmt.executeQuery();
while (RsPais.next()){
PaisForm pais = new PaisForm();
pais.setSigla(RsPais.getString("Sigla_Pais"));
pais.setNome(RsPais.getString("Pais"));
paises.add(pais);
}
}catch (SQLException e) {
error = e;
message = "Erro ao obter Países!";
}
pstmt.close();
pstmt = null;
return paises;
}
.
.
.