Java Beans

6 respostas
jvictorcf

Pessoal,

Estou tentando passar parametros pelo <jsp:setProperty… />

Aqui estão os códigos

index.jsp (um html simples)

&lt;html&gt;
	&lt;body&gt;
		&lt;form action="index2.jsp" method="post"&gt;
			Nome: &lt;input TYPE=text NAME=nome&gt;<br>
			Endereço: &lt;input type="text" name=end&gt;<br>
			Telefone: &lt;input type="text" name="tel"&gt;<br>
			Sobrenome: &lt;input type="text" name="lastnome"&gt;<br>
			&lt;input type="submit"&gt;<br>
		&lt;/form&gt;
	&lt;/body&gt;
&lt;/html&gt;

index2.jsp (beans)

&lt;jsp:useBean id="theBean" scope="page" class="teste.beans.TesteSQL"&gt;
	&lt;jsp:setProperty name="theBean" property="*" /&gt;
	&lt;%
		theBean.setSql();
	%&gt;
&lt;/jsp:useBean&gt;

teste.beans.TesteSQL

/*
 * Created on 22/11/2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package teste.beans;

/**
 * @author João Victor
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.sql.*;

public class TesteSQL {

	private String nome = "";
	private String end;
	private String lastname;
	private String tel;
	
	private String sql;
	
	public void banco()
	{
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection con = DriverManager.getConnection("jdbc:odbc:teste");
			Statement sta = con.createStatement();
			sta.executeUpdate(sql);
		}
		catch (Exception ex)
		{
			System.out.println("erro");		
		}
	}

	public void setSql()
	{
		sql = "INSERT INTO TESTE (nome, end, tel, lastname) values ('";
		sql = sql + nome + "', '" + end + "', '" + tel + "', '" + lastname + "')";
		System.out.println(sql);
	}
	
}

E ele “printa” os valores todos como null, a não ser a String nome, que ele printa como “” (string vazia).

Obrigado

6 Respostas

leonardom

Na página index2.jsp, mude o scope do bean para scope=“request”

jvictorcf

Cara, mudei mas não funcionou.

Aliás mudei as variaveis para public, mas tbm não funcionou…

L
jvictorcf:
Pessoal,

Estou tentando passar parametros pelo <jsp:setProperty... />

Aqui estão os códigos

teste.beans.TesteSQL

/*
 * Created on 22/11/2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package teste.beans;

/**
 * @author João Victor
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.sql.*;

public class TesteSQL {

	private String nome = "";
	private String end;
	private String lastname;
	private String tel;
	
	private String sql;
	
	public void banco()
	{
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection con = DriverManager.getConnection("jdbc:odbc:teste");
			Statement sta = con.createStatement();
			sta.executeUpdate(sql);
		}
		catch (Exception ex)
		{
			System.out.println("erro");		
		}
	}

	public void setSql()
	{
		sql = "INSERT INTO TESTE (nome, end, tel, lastname) values ('";
		sql = sql + nome + "', '" + end + "', '" + tel + "', '" + lastname + "')";
		System.out.println(sql);
	}
	
}

E ele "printa" os valores todos como null, a não ser a String nome, que ele printa como "" (string vazia).

Obrigado

Acho que vc precisa de ter os metodos de set para as propriedades do seu objecto, tipo :

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

etc......
}

e mantenha as propriedades como privadas.

C

Você tem que criar os métodos get e set dos atributos.

leonardom

Nossa é verdade!!! Tem que criar os gets e sets. Eu nem tinha reparado no código do Bean.

jvictorcf

Valeu galera!!!
Funcionou :smiley:

Criado 23 de novembro de 2004
Ultima resposta 23 de nov. de 2004
Respostas 6
Participantes 4