How to pass a value (selected from selectOneMenu)from page JSF to another
I want to fill an selectOneMenu with fields from my DataBase and then use the chosen value in another request SQL. For know I have my selectOneMenu filled from my data but I dont Know how to retrieve the chosen value and show it into another page. The error is: org.apache.jasper.JasperException: An exception occurred processing JSP page /Result.jsp Stacktrace: at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:442)
Also I want to write a clean code , I want to create class connection and use it to connect one time in the first page and deconnect at the last page(when I finish).
Bean:
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;
}
}
First JSF page:
<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>
Second JSF page:
<body>
<h:outputText value="#{BeanTools.maValeur}" />
</body>
The Faces config:
<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>
Class Connection:
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;
}
The whole stacktrace:
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.ApplicationFil开发者_JAVA技巧terChain.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
Finally, there's the root cause of the exception:
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?
You forgot the <f:view>
in Result.jsp
. All JSF components has to go inside a <f:view>
. Edit your Result.jsp
so that it look like the following basic template:
<%@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>
精彩评论