Remplir un selectOneMenu à partir d'une BD oracle

Remplir un selectOneMenu à partir d'une BD oracle - Java - Programmation

Marsh Posté le 22-05-2011 à 00:15:23    

Salut
j'ai une liste déroulante que je veux la remplir via une base de donnée oracle(noté que je travaille avec jsf 2.0 sous netbeans)
mais un message d'erreur s'affiche:

Code :
  1. An Error Occurred:
  2. Cannot convert [javax.faces.model.SelectItem@61d27a] of type class java.util.ArrayList to class javax.faces.model.SelectItem
  3. java.lang.IllegalArgumentException: Cannot convert [javax.faces.model.SelectItem@61d27a] of type class java.util.ArrayList to class javax.faces.model.SelectItem
  4. at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:397)
  5. at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:222)
  6. at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
  7. at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
  8. at javax.faces.component.UISelectItem.getValue(UISelectItem.java:231)
  9. at com.sun.faces.renderkit.SelectItemsIterator.initializeItems(SelectItemsIterator.java:189)
  10. at com.sun.faces.renderkit.SelectItemsIterator.hasNext(SelectItemsIterator.java:135)
  11. at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:760)
  12. at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:842)
  13. at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:296)
  14. at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
  15. at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1620)
  16. at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
  17. at com.sun.faces.application.view.JspViewHandlingStrategy.doRenderView(JspViewHandlingStrategy.java:420)
  18. at com.sun.faces.application.view.JspViewHandlingStrategy.renderView(JspViewHandlingStrategy.java:209)
  19. at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
  20. at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
  21. at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  22. at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
  23. at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
  24. at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
  25. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
  26. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
  27. at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
  28. at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
  29. at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
  30. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
  31. at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
  32. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
  33. at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
  34. at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
  35. at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
  36. at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
  37. at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
  38. at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
  39. at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
  40. at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
  41. at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
  42. at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
  43. at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
  44. at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
  45. at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
  46. at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
  47. at java.lang.Thread.run(Thread.java:619)


voici mon bean:
 

Code :
  1. import java.sql.SQLException;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import javax.faces.model.SelectItem;
  5. import java.sql.*;
  6. import java.sql.DriverManager;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. /*
  10. * To change this template, choose Tools | Templates
  11. * and open the template in the editor.
  12. */
  13. /**
  14. *
  15. * @author utilisateur
  16. */
  17. public class Monbean {
  18. private String maValeur;
  19. Connection con ;
  20. private List<SelectItem> mesElements;
  21. public String getmaValeur() {
  22. return maValeur;
  23. }
  24. public void setmaValeur(String maValeur) {
  25. this.maValeur = maValeur;
  26. }
  27. private Iterable<String> getMaliste() throws SQLException {
  28. ArrayList unelist = new ArrayList();
  29. try
  30. {
  31. Class.forName("oracle.jdbc.driver.OracleDriver" );
  32. System.out.println ("driver etablie" );
  33. }
  34. catch(Exception e)
  35. {
  36. System.out.println ("erreur:Driver int" +
  37. "rouvable" );
  38. }
  39. try
  40. {
  41. String URL = "jdbc:oracle:thin:@localhost:1521:gmao";
  42. String USER = "pfe";
  43. String PASSWD = "gmao";
  44. con =DriverManager.getConnection(URL,USER, PASSWD);
  45. System.out.println ("connexion base pfe etablie" );
  46. }
  47. catch(Exception e)
  48. {
  49. System.out.println ("erreur: base introuvable" );
  50. }
  51. Statement st = con.createStatement();
  52. ResultSet resultats = null;
  53. try {
  54. resultats = st.executeQuery("select * from auteur" );
  55. }
  56. catch (SQLException ex) {
  57. Logger.getLogger(Monbean.class.getName()).log(Level.SEVERE, null, ex);
  58. }
  59. try {
  60. while(resultats.next()){
  61. String num=resultats.getString("nomaut" ).toString();
  62. unelist.add(num);
  63. }
  64. }
  65. //session.close();
  66. catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. return (List) unelist;
  70. }
  71. public List<SelectItem> getMesElements() throws SQLException {
  72. if (mesElements == null) {
  73. mesElements = new ArrayList<SelectItem>();
  74. for (String val : getMaliste()) {
  75. mesElements.add(new SelectItem(val));
  76. }
  77. }
  78. return mesElements;
  79. /** Creates a new instance of Monbean */
  80. }
  81. public Monbean() throws SQLException{
  82. }
  83. }


 
------------------------------
et dans ma page jsf
j'ai mis

Code :
  1. <h:selectOneMenu value=" # {Monbean.maValeur}" >
  2. <f:selectItem value="#{Monbean .mesElements}"/>


Pourriez vous s'il vous plait m'aider à corriger mon code source
je vous remercie

Reply

Marsh Posté le 22-05-2011 à 00:15:23   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed