Boa tarde, antes de de criar este post procurei muito aq no GUJ e em outras comunidades, mas ñ tive sucesso, tenho um projeto em STRUTS2 na qual tenho 2 combobox na pagina jsp, ñ to conseguindo preencher-las com os valores vindos do BD, criei uma ACTION para passar estes valores. Vou postar os códigos para q alguém possa ter a solução.
Classe CarregaComboAction:package br.com.eibsb.action;
import java.sql.SQLException;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import br.com.eibsb.Dao.ClubeDAO;
import br.com.eibsb.Dao.PosicaoDao;
import br.com.eibsb.bean.Clube;
import br.com.eibsb.bean.Posicao;
public class CarregaComboAction {
private List<Posicao> posicoes;
private List<Clube> clubes;
@Action(value = "carregaCombo", results = {
@Result(name = "ok", location = "adicionaJogador"),
@Result(name = "erro", location = "/erro.jsp")
})
public String execute() {
try {
posicoes = new PosicaoDao().getPosicoes();
clubes = new ClubeDAO().getClubes();
} catch (SQLException e) {
e.printStackTrace();
return "erro";
}
return "ok";
}
public List<Posicao> getPosicoes() {
return posicoes;
}
public void setPosicoes(List<Posicao> posicoes) {
this.posicoes = posicoes;
}
public List<Clube> getClubes() {
return clubes;
}
public void setClubes(List<Clube> clubes) {
this.clubes = clubes;
}
}
nter>
<form action="carregaCombo" method="post">
<table border="5">
<input type="hidden" name="id" value="${jogador.id }">
<tr>
<td>Nome:</td>
<td><input type="text" value="${jogador.nomeJogador }"name="jogador.nomeJogador" size="40">
</tr>
<tr>
<td>Data de Nascimento:</td>
<td><input type="text"value="<fmt:formatDate value='${jogador.dtNascimento.time}' pattern='dd/MM/yyyy'/> "
name="jogador.dtNascimento" size="40" maxlength="10"> </td>
</tr>
<tr>
<td>Selecione Posição:</td>
<td><select name="jogador.posicao.id">
<c:forEach var="posicao" items="${posicoes}">
<option value="${posicao.id}">${posicao.nomePosicao}</option>
</c:forEach>
</select></td>
</tr>
<tr>
<td>Selecione Times:</td>
<td><select name="jogador.time.id">
<c:forEach var="clube" items="${clubes}">
<option value="${clube.id}">${clube.nomeClube}</option>
</c:forEach>
</select></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"name="Gravar" value="Gravar"></td>
</tr>
</table>
</form>
</center>
public List<Clube> getClubes() throws SQLException {
String sql = "select * from clube";
PreparedStatement stm = conexao.prepareStatement(sql);
ResultSet rs = stm.executeQuery();
List<Clube> clubes = new ArrayList<Clube>();
while (rs.next()) {
Clube c = new Clube();
c.setId(rs.getLong("id"));
c.setNomeClube(rs.getString("nome_clube"));
Calendar data = Calendar.getInstance();
data.setTime(rs.getDate("data_fundacao"));
c.setDtFundacao(data);
c.setCdRegistro(rs.getInt("cd_registro"));
clubes.add(c);
}
rs.close();
conexao.close();
stm.close();
return clubes;
}
package br.com.eibsb.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.eibsb.bean.Posicao;
public class PosicaoDao {
private Connection conexao;
public Connection getConexao() {
return conexao;
}
public void setConexao(Connection conexao) {
this.conexao = conexao;
}
public List<Posicao> getPosicoes() throws SQLException {
String sql = "select * from posicao";
PreparedStatement stm = conexao.prepareStatement(sql);
ResultSet rs = stm.executeQuery();
List<Posicao> posicoes = new ArrayList<Posicao>();
while (rs.next()) {
Posicao p = new Posicao();
p.setId(rs.getLong("id_posicao"));
p.setNomePosicao(rs.getString("nomePosicao"));
posicoes.add(p);
}
rs.close();
conexao.close();
stm.close();
return posicoes;
}
}
?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>projetoStruts</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
ñ to conseguindo fazer esta implementação. Valeu.
