Estou fazendo alguns testes com jsf(java server faces) e estou com uma dúvida que é o seguinte:
Criei um pagina jsp que solicita nome senha do usuario e pede para ele informar em qual empresa quer se logar(similar ao logon do windows).
A lista de empresas é consultada através do banco de dados access que coloco dentro de um array utilizo o SelectItem, para exibir na web.
O problema é que quando coloco está lista de empresas o botão acessar para de funcionar, se retiro o SelectItem funciona.
Aqui vai meu código:
PAGINA JSP:
[color=blue]<%@ taglib uri=“http://java.sun.com/jsf/core” prefix=“f” %>
<%@ taglib uri=“http://java.sun.com/jsf/html” prefix=“h” %>
<title>PROJETO AM - KATRINA</title>
</head>
<body>
<f:view>
<h:form>
<center>
<br><br><br>
<table border="1">
<tr>
<td>DIGITE O NOME DO USUARIO:</td>
<td><h:inputText value="#{ac.nome}"/> </td>
</tr>
<tr>
<td>DIGITE A SENHA DO USUARIO:</td>
<td><h:inputText value="#{ac.senha}"/></td>
</tr>
<tr>
<td>SELECIONE A EMPRESA:</td>
<td><h:selectOneMenu value="#{ac.options}">
<f:selectItems value="#{ac.options}" />
</h:selectOneMenu> </td>
</tr>
<tr>
<td colspan="2"><center><h:commandButton value=" ACESSAR " action="#{ac.acessar}"/></center> </td>
</tr>
</table>
</center>
</h:form>
</f:view>
</body>
[/color]
CÓDIGO DA CLASSE:
[color=darkred]package com.am;
import javax.faces.model.SelectItem;
import java.util.<em>;
import java.sql.</em>;
/**
*
-
@author victor */ public class acesso { // private String nome; private String senha; private String empresa = “”;
//private List options; List options = new ArrayList(); SelectItem option = new SelectItem();
static Connection cn = null; static PreparedStatement st = null; static ResultSet rs = null;
public acesso() { String select = “SELECT * FROM TAB_EMPRESA”; String emp=""; try{ Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); cn = DriverManager.getConnection(“jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/projetos/am/data.mdb”); st = cn.prepareStatement(select); rs = st.executeQuery(); while(rs.next()) { emp = rs.getString(2); option = new SelectItem(emp,emp); options.add(option); }
}catch(Exception e) { System.out.println("Erro = " + e.getMessage()); }
}
public void setOptions(List options) { this.options = options; }
public List getOptions() { return options; }
public String getNome() { return nome; }
public void setNome(String nome) { this.nome = nome; }
public String getSenha() { return senha; }
public void setSenha(String senha) { this.senha = senha; }
public String getEmpresa() { return empresa; }
public void setEmpresa(String empresa) { this.empresa = empresa; } public String acessar() { String result = “”; String select = “SELECT * FROM TAB_USUARIO WHERE NOME_USUARIO = ? AND SENHA_USUARIO = ?”; try{ st = cn.prepareStatement(select); st.setString(1,nome); st.setString(2,senha); rs = st.executeQuery(); if(rs.next()) { result = “sucesso”; } else { result = “falha”; } }catch(Exception e) { System.out.println(e.getMessage()); } return result; } }[/color]