Havia feito um filtro em servlet que abria e fechava conexão com o banco, mas no struts 2 meu filtro abre e fecha a conexão com o banco e só depois a action é chamada.
Segui o mesmo padrão de quando usei servlets, talvez esse seja o erro, mas quando procurei algo sobre no struts só achei o pessoal falando de interceptadores =/
[code]…
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*.do
[code]package tarefas.filter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import tarefas.dao.ConnectionFactory;
public class ConnectionFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
Connection cnn = null;
System.out.println("Got Here! ");
try {
cnn = new ConnectionFactory().getConnection();
request.setAttribute(“cnn”, cnn);
chain.doFilter(request, response);
//super.doFilter(request, response, chain);
cnn.close();
System.out.println(“Fechou!!”);
} catch (SQLException e) {
System.out.println(e.getMessage());
throw new ServletException(e);
}
}
@Override
public void destroy() { }
@Override
public void init(FilterConfig arg0) throws ServletException {}
}[/code]
[code]package tarefas.action;
import java.sql.Connection;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.testng.annotations.Parameters;
import com.opensymphony.xwork2.ActionContext;
import tarefas.dao.UsuarioDao;
import tarefas.modelo.Usuario;
public class AutorizadorAction {
private Usuario usuario;
private Connection cnn;
@Action(value="login", results= {
@Result(name="ok", type="redirectAction",
params={"actionName", "listaTarefas"}),
@Result(name="invalido", location="/login.jsp")
})
public String login(){
cnn =(Connection) ActionContext.getContext().get("cnn");
System.out.println(" --- Conexão: " + cnn);
UsuarioDao dao = new UsuarioDao();
dao.setCnn(cnn);
if(dao.existeUsuario(usuario)){
ActionContext.getContext().getSession().put("usuarioLogado", usuario);
return "ok";
}else{
return "invalido";
}
}
public Usuario getUsuario() {return usuario; }
public void setUsuario(Usuario usuario) {this.usuario = usuario;}
public void setCnn(Connection cnn) {this.cnn = cnn;}
}[/code]