Action não funciona ao se concluir o cadastro

5 respostas
E

Pessoal, tô fazendo um sisteminha do zero pra aprender mais struts, bem do zero mesmo, somente com struts 1.3.5, sem hibernate/spring, pois sempre trabalhei com sistemas “já montados”. mas qdo clico no botão enviar do CadastroClienteCadastro.jsp não funciona, a pag fica na mesma… a seguir coloco os meus códigos. alguém sabe oq fazer?

public class CadastroClienteAction extends Action{
	public ActionForward execute(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.getInputForward();
	}

	public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, 
			HttpServletResponse response) {
		
		CadastroClienteForm meuForm = (CadastroClienteForm) form;
		CadastroClienteModel.insert(meuForm.getNome(), meuForm.getEndereco(), meuForm.getTelefone(), 
				meuForm.getEmail());
		return mapping.findForward("cadastroClienteConsulta");
	}	
}
public class CadastroClienteForm extends ActionForm{
	
	private String nome;
	private String endereco;
	private String telefone;
	private String email;
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEndereco() {
		return endereco;
	}

	public void setEndereco(String endereco) {
		this.endereco = endereco;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getTelefone() {
		return telefone;
	}

	public void setTelefone(String telefone) {
		this.telefone = telefone;
	}

	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		if (StringUtils.isNumeric(nome)) {
			errors.add("org.apache.struts.action.GLOBAL_MESSAGE", 
					new ActionMessage("error.nome.invalid"));
		}
		return errors;
	}
}
public class CadastroClienteModel {

	static final String JDBC_DRIVER = "org.postgresql.Driver";        
	static final String DATABASE_URL = "jdbc:postgresql://localhost:5432/agenda";
	static final String LOGIN_ROOT = "postgres";
	static final String SENHA_ROOT = "netel";   

	public static Connection getConnection() {
		Connection connection = null; // gerencia a conexão
		   
		try {
			Class.forName( JDBC_DRIVER ); // carrega classe de driver do banco de dados
			connection =  DriverManager.getConnection( DATABASE_URL, LOGIN_ROOT, SENHA_ROOT );// estabelece conexão
			System.out.println("Sucesso ao conectar ao banco");
		}  // fim do try
		   
		catch (SQLException sqlException) {                                                                  
			sqlException.printStackTrace();
			System.exit( 1 );                                               
		} // fim do catch
		   
		catch (ClassNotFoundException classNotFound){                                                                  
			classNotFound.printStackTrace();            
			System.exit( 1 );                                               
		} // fim do catch
		   
		return connection;
	}//fim de getConnection()

	public static void insert(String nome, String endereco, String telefone, String email) {
		
		try {
			Statement statement = getConnection().createStatement();
			ResultSet resultSet = statement.executeQuery("INSERT INTO CONTATOS (nome, endereco, telefone, email) " +
					"VALUES ("+  nome +","+ endereco +","+ telefone +","+ email + ")");
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		   
	}//fim de insert(String nome, String endereco, String telefone, String email)
}

meu struts-config.xml ta mapeado assim com form-bean e action:

<form-bean name="CadastroClienteForm"
               type="teste.form.CadastroClienteForm" />
<action   path="/cadastroCliente"
               type="teste.action.CadastroClienteAction"
               name="CadastroClienteForm"
               scope="request"
           	   validate="true"
           	   input="/WEB-INF/pages/CadastroClienteCadastro.jsp"
           	   parameter="method">     
			     
			   <forward name="cadastroClienteConsulta" path="/WEB-INF/pages/CadastroClienteConsulta.jsp"></forward>    	   			   
     </action>

e meu CadastroClienteCadastro.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ include file="/WEB-INF/pages/aqua/taglib.jsp" %> 
<%@ include file="/WEB-INF/pages/aqua/menu.jsp" %>

<html:form action="cadastroCliente">
	<bean:message key="cadastroCliente.nome.label"/>
	<html:text property="nome" size="50" maxlength="30"></html:text>
	<br><br>

	&lt;bean:message key="cadastroCliente.endereco.label"/&gt;
	&lt;html:text property="endereco" size="50" maxlength="30"&gt;&lt;/html:text&gt;
	<br><br>
	
	&lt;bean:message key="cadastroCliente.telefone.label"/&gt;
	&lt;html:text property="telefone" size="50" maxlength="30"&gt;&lt;/html:text&gt;
	<br><br>
	
	&lt;bean:message key="cadastroCliente.email.label"/&gt;
	&lt;html:text property="email" size="50" maxlength="30"&gt;&lt;/html:text&gt;
	<br><br>
		&lt;center&gt;&lt;input type="submit"&gt;
		&lt;/center&gt;	
&lt;/html:form&gt;

5 Respostas

vivi_grieco

tente mudar seu input type p/ html:submit

E

não deu certo…queria q quando eu clicar no submit do CadastroClienteCadastro.jsp ele fizesse o método insert da action e fosse pro CadastroClienteConsulta.jsp (esse arquivo mostra só uma saída dizendo ok, só pra ter certeza de q tud foi feito corretamente). pelo menos foi isso q tentei com os códigos acima mas não deu certo…

vivi_grieco

Tente mudar o teu CadastroClienteAction, extende ele ao in´ves de action, de DispatchAction

e no teu jsp:

&lt;html:form action="cadastroCliente?method=insert"&gt;
E

no struts 1.3.5 não tem a DispatchAction:
http://struts.apache.org/1.3.5/struts-core/apidocs/index.html

com o código no jsp que vc colocou acima, na parte de endereços ficou:
http://localhost:8080/StrutsTest/cadastroCliente.do?method=insert

mas ainda não acessa o método insert de CadastroClienteAction. no método insert ele puxa uma conexão com o bd de CadastroClienteModel, mas ele nem chega a conectar.

E

aliás, tem a DispatchAction sim, ela ta no struts-extras…mesmo assim não deu certo…

Criado 31 de janeiro de 2007
Ultima resposta 1 de fev. de 2007
Respostas 5
Participantes 2