|
Ol� pessoal ! A peia continua ...
Julio, olha s�:
conforme o exemplo http://www.javaworld.com/javatips/jw-javatip136_p.html ,
existe uma classe SynchroAction. Ser� que pra resolver esse meu probleminha tenho
que extender todas as minhas Actions dessa classe ? Detalhe: o exemplo acima
aborda um c�digo exemplo(1.0.2) q n�o condiz com a vers�o(1.1) pra baixar.Como
estou iniciando com esta �ltima, n�o ficou muito claro o
entendimento.
Estou aproveitando e
anexando as minhas duas lindas classes envolvidas neste
probleminha.
Abra�os e
ate+
|
package br.com.unilet.actions.cliente; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
public class ClienteListAction extends Action {
protected Vector getEmployees() {
Cliente cliente = null;
Vector clientes = new Vector(); // era um ArrayList
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ServletContext context = servlet.getServletContext();
DataSource dataSource =
(DataSource) context.getAttribute(Action.DATA_SOURCE_KEY);
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from cliente ");
while (rs.next()) {
cliente = new Cliente();
cliente.setCodigo(new Integer(rs.getInt("codigo")));
cliente.setNome(rs.getString("nome"));
cliente.setEndereco(rs.getString("endereco"));
cliente.setUf(rs.getString("uf"));
cliente.setFone(rs.getString("fone"));
cliente.setSexo(rs.getString("sexo"));
clientes.add(cliente);
}
System.err.println("");
System.err.println("ClienteListAction.execute -->> lista montada(objeto serializado) dos clientes.");
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
conn = null;
}
}
return clientes;
}
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
//Validate transaction token
ActionErrors errors = new ActionErrors();
if (!isTokenValid(request)) {
System.err.println("");
System.err.println("ClienteLIstAction -->> not isToken");
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("Error transaction token"));
}
resetToken(request);
String target = "success";
Vector clientes = getEmployees();
HttpSession session = request.getSession();
session.setAttribute("clientes", clientes);
System.err.println("");
System.err.println("ClienteListAction -->> forward para "+target+", path=/clientelist.jsp");
return (mapping.findForward(target));
}
}
package br.com.unilet.actions.cliente;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import br.com.unilet.portal.LabwebActionMapping;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
public class EditClienteAction extends Action {
protected void updateRegister(ActionForm form) throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ServletContext context = servlet.getServletContext();
DataSource dataSource = (DataSource) context.getAttribute(Action.DATA_SOURCE_KEY);
try {
ClienteForm cForm = (ClienteForm) form;
conn = dataSource.getConnection();
stmt = conn.createStatement();
StringBuffer sqlString = new StringBuffer("update cliente ");
sqlString.append(" set nome='" + cForm.getNome() + "', ");
sqlString.append(" endereco='" + cForm.getEndereco() + "', ");
sqlString.append(" cidade='" + cForm.getCidade() + "', ");
sqlString.append(" uf='" + cForm.getUf() + "', ");
sqlString.append(" fone='" + cForm.getFone() + "', ");
sqlString.append(" sexo='" + cForm.getSexo()+"'");
sqlString.append(" where codigo=" + cForm.getCodigo());
stmt.execute(sqlString.toString());
System.err.println("");
System.err.println("EditClienteAction.execute -->> cliente atualizado.");
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
System.err.println("EditCliente [ActionForward execute]");
ActionErrors errors = new ActionErrors();
String target = "success";
LabwebActionMapping labwebMapping = (LabwebActionMapping) mapping;
if (labwebMapping.isLoginRequired()) {
HttpSession session = request.getSession();
if (session.getAttribute("USER") == null) {
target = "login";
errors.add(
ActionErrors.GLOBAL_ERROR,
new ActionError("errors.login.required"));
//back to the original form
if (!errors.empty()) {
saveErrors(request, errors);
}
System.err.println("EditClienteAction USER == null");
return (mapping.findForward(target));
}
}
if (isCancelled(request)) {
System.err.println("EditClienteAction is Cancelled");
return (mapping.findForward("success"));
}
//Validate transaction token
if (!isTokenValid(request)) {
System.err.println("");
System.err.println("EditClienteAction -->> not isToken");
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("Error transaction token"));
}
resetToken(request);
try {
updateRegister(form);
// Report any errors
if (!errors.isEmpty()) {
saveErrors(request, errors);
saveToken(request);
System.err.println("");
System.err.println("EditClienteAction -->> saveToken ");
return (mapping.getInputForward());
}
} catch (Exception e) {
System.err.println("EditClienteAction. Falha no update");
target = "error";
errors.add(
ActionErrors.GLOBAL_ERROR,
new ActionError("errors.database.error", e.getMessage()));
// Report any errors
if (!errors.empty()) {
saveErrors(request, errors);
}
}
//Forward to the appropriate View
System.err.println("");
System.err.println("EditClienteAction -->> forward para "+target+", path=/ClienteList.do");
return (mapping.findForward(target));
}
}
------------------------------------------------------------------------------------------- Ceara' Java User Group Para cancelar sua assinatura, envie um e-mail para: [EMAIL PROTECTED] Para mais informacoes, mande um e-mail para: [EMAIL PROTECTED] Falar com o administrador? e-mail para: [EMAIL PROTECTED]
