如何将值(从selectOneMenu中选择)从页面JSF传递到另一个

时间:2011-06-07 14:39:19

标签: jsf

我想用我的DataBase中的字段填充selectOneMenu 然后在另一个请求SQL中使用所选的值。 要知道我从我的数据中填充了selectOneMenu,但我不知道如何检索所选值并将其显示在另一页中。错误是: org.apache.jasper.JasperException:处理JSP页面/Result.jsp时发生异常 堆栈跟踪:     at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)     在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:442)

另外我想编写一个干净的代码,我想创建类连接并使用它在第一页连接一次并在最后一页(我完成时)断开连接。

豆:

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>

Faces配置:

<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 javax.faces.component.UIViewRoot@144c984 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 javax.faces.component.UIViewRoot@144c984 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

1 个答案:

答案 0 :(得分: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>