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
