2011-06-07 64 views
1

我想填充selectOneMenu和來自我的DataBase 中的字段,然後在另一個請求SQL中使用所選值。 爲了解我有我的selectOneMenu從我的數據填充,但我不知道如何檢索選定的值,並將其顯示到另一個頁面。錯誤是: org.apache.jasper.JasperException:發生異常處理JSP頁面/Result.jsp 堆棧跟蹤: 在org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553) 在組織.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:442)如何將頁面JSF中的值(從selectOneMenu中選擇)傳遞給另一個

另外我想寫一個乾淨的代碼,我想創建類連接並使用它連接一次在第一頁並斷開連接在最後一頁(當我完成時)。

豆:

public class BeanTools { 

    private List<SelectItem> mesElements; 
    private String maValeur; 
    public PreparedStatement st; 
    Connectionx con = new Connectionx(); **//IF I do that on N page I will have N connection!!** 
    Connection cx = (Connection) con.Connx(); 

     public BeanTools() { 
    }  

    public String getMaValeur() { 
     return maValeur; 
    } 

    public void setMaValeur(String maValeur) { 
     this.maValeur = maValeur; 
    } 

    public void setMesElements(List<SelectItem> mesElements) { 
     this.mesElements = mesElements; 
    } 




public Iterable<String> remplireItem() throws ClassNotFoundException, SQLException 
    { 

String sql; 
sql ="select issuestatus.pname from issuestatus;"; 
System.out.println("sql"); 
st=(PreparedStatement) cx.prepareStatement(sql); 
ResultSet rs1 = st.executeQuery();System.out.println("execute"); 

String nbb; 
ArrayList<String> list = new ArrayList<String>(); 
while(rs1.next()) 
     { 
      nbb = rs1.getString("pname"); 
      System.out.println(nbb); 
      list.add(nbb); 
      System.out.println("list"+list.toString()); 
     } 


return list; 
    } 


public List<SelectItem> getMesElements() throws SQLException, ClassNotFoundException { 

if (mesElements == null) { 
mesElements = new ArrayList<SelectItem>(); 
for (String val : remplireItem()) { 
mesElements.add(new SelectItem(val)); 
} 
} 
return mesElements; 
} 
} 

首先JSF頁面:

<h:form id="form">  
Issue status 2 :<h:selectOneMenu style="width:200px" value="#{BeanTools.maValeur}" > 
    <f:selectItems value="#{BeanTools.mesElements}"/> 
       </h:selectOneMenu> 

<h:commandButton action="submit" value="submit"/> 
</h:form> 

二JSF頁面:

<body>  
     <h:outputText value="#{BeanTools.maValeur}" /> 

</body> 

面孔配置:

<managed-bean> 
<managed-bean-name>BeanTools</managed-bean-name> 
<managed-bean-class>KPIs.BeanTools</managed-bean-class> 
<managed-bean-scope>session</managed-bean-scope> 
</managed-bean> 

<navigation-rule> 
<from-view-id>/welcomeJSF.jsp</from-view-id> 
<navigation-case> 
<from-outcome>submit</from-outcome> 
<to-view-id>/Result.jsp</to-view-id> 
</navigation-case> 
</navigation-rule> 

類連接:

public class Connectionx { 

public Connection cx=null; 
public Statement st; 

public Connectionx() {} 

public Connection Connx() 
{ 

    System.out.println("enregister le driver"); 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
     } 
     catch(Exception ex) 
       { 
       System.err.println("Erreur lors du chargement du driver"+ex.getMessage()); 


       } 


     try { 
      /** Connection */ 
      cx = (Connection) DriverManager.getConnection   ("jdbc:mysql://localhost/jiradb","",""); 

       } 
       catch(SQLException ex) 
       { 
      System. err. println("Error ")    } 
    return cx; 
} 

整個堆棧跟蹤:

org.apache.jasper.JasperException: An exception occurred processing JSP page /Result.jsp at line 19 

16:  <body> 
17:   <h1>Hello World!</h1> 
18:  
19:   <h:outputText value="#{BeanTools.maValeur}" /> 
20:   
21:  </body> 
22: </html> 


Stacktrace: 
    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553) 
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:442) 
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391) 
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684) 
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471) 
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402) 
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329) 
    at com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:546) 
    at com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:363) 
    at com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:154) 
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
    at java.lang.Thread.run(Thread.java:662) 
Caused by: javax.servlet.ServletException: javax.servlet.jsp.JspException: java.lang.IllegalStateException: Component [email protected] not expected type. Expected: javax.faces.component.UIOutput. Perhaps you're missing a tag? 
    at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:907) 
    at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840) 
    at org.apache.jsp.Result_jsp._jspService(Result_jsp.java:84) 
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419) 
    ... 38 more 
Caused by: java.lang.IllegalStateException: Component [email protected] not expected type. Expected: javax.faces.component.UIOutput. Perhaps you're missing a tag? 
    at com.sun.faces.taglib.html_basic.OutputTextTag.setProperties(OutputTextTag.java:126) 
    at javax.faces.webapp.UIComponentClassicTagBase.findComponent(UIComponentClassicTagBase.java:690) 
    at javax.faces.webapp.UIComponentClassicTagBase.doStartTag(UIComponentClassicTagBase.java:1311) 
    at com.sun.faces.taglib.html_basic.OutputTextTag.doStartTag(OutputTextTag.java:163) 
    at org.apache.jsp.Result_jsp._jspx_meth_h_005foutputText_005f0(Result_jsp.java:103) 
    at org.apache.jsp.Result_jsp._jspService(Result_jsp.java:73) 
    ... 41 more 
+0

儘管代碼沒有公開最佳實踐,但它應該可以工作。你面臨的問題是什麼?當你按下按鈕時會發生什麼?或者是真正的問題*實際上*(與您在標題中提到的內容相反)「請檢查併爲我重寫此代碼」? – BalusC 2011-06-07 14:44:56

+0

您已編輯該問題以添加異常消息和「Connection」類。但是如果沒有整個堆棧跟蹤,這是沒用的。帶有根本原因的堆棧跟蹤是異常最重要的部分。它包含答案。請注意代碼格式化規則,您的'Connection'類代碼格式不正確。 – BalusC 2011-06-07 14:51:18

回答

3

最後,還有異常的根本原因:

所致:java.lang.IllegalStateException:組件的javax .faces.component.UIViewRoot @ 144c984不是預期的類型。預期:javax.faces.component.UIOutput。也許你錯過了一個標籤?

您忘記了<f:view>Result.jsp。所有的JSF組件都必須進入<f:view>。編輯您的Result.jsp,使其看起來像下面的基本模板:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> 
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> 
<!DOCTYPE html> 
<f:view> 
    <html lang="en"> 
     <head> 
      <title>JSP page</title> 
     </head> 
     <body> 
      <h:outputText value="JSF components here." /> 
     </body> 
    </html> 
</f:view> 
+0

非常感謝,我很抱歉不清楚 – rym 2011-06-07 15:16:31

+0

不客氣。它解決了問題嗎?由於您是新手,請不要忘記標記問題解決後接受的最有幫助的答案。另見http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 – BalusC 2011-06-07 15:21:20

+0

是的,我的問題解決了:) – rym 2011-06-07 15:43:41

相關問題