Exemplo de select apartir de outro select com taglibs

6 respostas
D

exemplo de select para ser preenchido apartir de outro select…

taglib para preencher selects de clientes

package tags;
import conexao.Conexao;
import java.io.*;
import java.sql.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public final class TagClientes implements BodyTag{
	private PageContext pc=null;
	private BodyContent body=null;
	private StringBuffer sb=new StringBuffer();
	private ResultSet rs=null;
	private Conexao con=null;
		
	public void setPageContext(PageContext p){
		pc=p;
	}
	public void setParent(Tag t){}
	public Tag getParent(){
		return null;
	}
	
	public int doStartTag() throws JspException{
		try{
			int tipo=1;
			con=new Conexao();
			rs=con.executeQuery("select codigo,nome from os_colaboradores where tipo="+tipo+" order by codigo");
			setVariaveis();
		}catch(SQLException e){
			System.out.println(e);
		}
		if(pc.getAttribute("codigo")==null)
		return SKIP_BODY;
		
		return EVAL_BODY_BUFFERED;
	}
	public void setBodyContent(BodyContent b){
		body=b;
	}
	public void doInitBody()throws JspException{}
	
	public boolean setVariaveis()throws JspTagException{
		try{
			String cod="";
				if(rs.next()){
				cod=""+rs.getInt("codigo");
				pc.setAttribute("codigo",cod);
				pc.setAttribute("nome",rs.getString("nome"));
				return true;
			}else{
				return false;
			}
		}catch(SQLException e){
			System.out.println(e);
			return false;
		}
	}
	public int doAfterBody() throws JspException{
		try{
			sb.append(body.getString());
			body.clear();
		}catch (IOException e){
			throw new JspException("erro fatal: IOException!");
		}
		if(setVariaveis()){
			return EVAL_BODY_AGAIN;
		}
		try{
			body.getEnclosingWriter().write(sb.toString());
		}catch(IOException e){
			throw new JspException("Erro fatal:IOException!");
		}
		return SKIP_BODY;
	}
	public int doEndTag() throws JspException{
		try{
		body.clear();
		int i=sb.length();
		sb.delete(0,i);
		}catch(IOException e){throw new JspException("erro io");}
		try{
			if(rs!=null){
				rs.close();
				rs=null;
			}
			if(con!=null){
				con.fecharConexao();
				con=null;
			}
		}catch(SQLException e){}
		return EVAL_PAGE;
	}
	public void release(){
		pc=null;
		body=null;
		sb=null;
	}

}

tagExtraInfo do tagClientes

package tags;

import javax.servlet.jsp.tagext.*;

public class TagTEIClientes extends TagExtraInfo{
	
	public VariableInfo[] getVariableInfo(TagData data){
		return new VariableInfo[]{
			new VariableInfo("codigo","java.lang.String",true,VariableInfo.NESTED),
			new VariableInfo("nome","java.lang.String",true,VariableInfo.NESTED),
		};
	}
}

a tag que preenche o select de tecnicos eh exatamente igual, soh mudam as variaveis de acordo com os campos da tabela tecnicos…

Bom o detalhe eh: quando eh escolhido um cliente(empresa) o select de contatos deve ser montado para exibir os contatos daquele cliente(as pessoas na empresa que solicitam o servico)…
codigo html

<select name="cliente" id="cliente" onChange="buscaContato(this)">
		<option value="">Selecione o cliente</option>
		<cliente:mostreclientes>
						<option value=<%=codigo%>><%=codigo%>-<%=nome%></option>
		</cliente:mostreclientes>
          </select>

select do contato(solicitante)

<select name="solicitante" id="select2">
		<option value="">selecione o Contato</option>
		<%
		String cliente=request.getParameter("cliente");
		if(cliente!=null){
			rs=con.executeQuery("select codigo,nome from os_contatos where cliente="+cliente+"order by codigo");
			while(rs.next()){
			int codigo=rs.getInt("codigo");
			String nome=rs.getString("nome");
			System.out.println("tentou");
			%><option value=<%=codigo%>><%=codigo%>-<%=nome%></option>
			<%}
		}
		
		%>
		
          </select>

quem tiver duvidas eh soh manda uma mensagem.

[]'s

6 Respostas

J

Oi

Diana, não testei ainda, mas ta rodando direitinho? :roll:

T+

D

tah rodando sim…
a unica coisa que tem que fazer eh: na acao que chama ele passar o parametro cliente com 0…

document.location=('exemplo.jsp?cliente='+0);

e depois quando for escolhido um cliente no select o parametro vai mudar e o select vai ser preenchido de acordo com o cliente escolhido…
eu postei de exemplo, pq pesquisei um monte para fazer(apesar de ser simples), mas eh que cada pessoa dah uma dica diferente…e realmente, poderia ser feito ateh com Taglib o que talvez fosse ficar mais encapsulado…se eu refizer com taglib, posto novamente, mas esse jah funciona e cumpri o seu papel.

[]'s

P

Diana, uma dúvida, pois não entendi muito bem no javadoc, para o que realmente serve o EVAL_BODY_BUFFERED no doStartTag(), sei que somente serve para BodyTag.

M

ia ser melhor se estivesse usando as novas tags…

G

tag files ???

:wink:

M

tag files ???

:wink:

Não. TagFiles não passam de includes, estou falando de Simple Tag

Criado 7 de julho de 2004
Ultima resposta 21 de mai. de 2005
Respostas 6
Participantes 5