Help 2 start with JSF needed.
843842Mar 16 2004 — edited Mar 17 2004Hello All.
I'm starting a new short-time project and spent a day trying to jump into JSF.
Things I'm trying to do are pretty simple:
1 jsp (faces) that has a combo getting data from a bean (a la struts form).
html:
<CODE>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<h:form id="addForm" >
<h:selectOneMenu value="#{TipoIncidenciaBean.selectedTipoIncidencia}">
<f:selectItems value="#{TipoIncidenciaBean.tipoIncidenciaList}"/>
</h:selectOneMenu>
</h:form>
</CODE>
bean:
<CODE>
package com.sabia.fichatron.faces.model;
import com.sabia.fichatron.db.dao.TipoIncidenciaDAO;
import com.sabia.fichatron.db.DataAccessException;
import com.sabia.fichatron.db.bean.TipoIncidencia;
import javax.faces.model.SelectItem;
import java.util.Collection;
import java.util.ArrayList;
public class TipoIncidenciaModel
{
private Object selectedTipoIncidencia;
public Object getSelectedTipoIncidencia()
{
return selectedTipoIncidencia;
}
public void setSelectedTipoIncidencia(Object selectedTipoIncidencia)
{
this.selectedTipoIncidencia = selectedTipoIncidencia;
}
public Collection getTipoIncidenciaList()
{
ArrayList items;
try
{
ArrayList incidencias = TipoIncidenciaDAO.getTipoIncidenciaList();
items = new ArrayList(incidencias.size());
for (int i = 0, n = incidencias.size(); i < n; i++)
{
TipoIncidencia incidencia = (TipoIncidencia)incidencias.get(i);
SelectItem item = new SelectItem(new Integer(incidencia.getIdTipoIncidencia()), incidencia.getDescripcion());
items.add(item);
}
}
catch (DataAccessException e)
{
e.printStackTrace();
items = new ArrayList(0);
}
return items;
}
public void getTipoIncidenciaList(Collection list) {}
}
</CODE>
web.xml
<CODE>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/jsp/test.jsp</welcome-file>
</welcome-file-list>
</web-app>
</CODE>
faces-config:
<CODE>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<managed-bean>
<managed-bean-name>TipoIncidenciaBean</managed-bean-name>
<managed-bean-class>com.sabia.fichatron.faces.model.TipoIncidenciaModel</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
</faces-config>
</CODE>
I got a ClassCastException:
<CODE>
java.lang.ClassCastException
com.sun.faces.taglib.html_basic.FormTag.setProperties(FormTag.java:160)
javax.faces.webapp.UIComponentTag.findComponent(UIComponentTag.java:695)
javax.faces.webapp.UIComponentTag.doStartTag(UIComponentTag.java:425)
com.sun.faces.taglib.html_basic.FormTag.doStartTag(FormTag.java:339)
org.apache.jsp.jsp.test_jsp._jspx_meth_h_form_0(test_jsp.java:93)
org.apache.jsp.jsp.test_jsp._jspService(test_jsp.java:66)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:142)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
</CODE>
The faces jsp are called OK through Faces servlet. I can even put a breakpoint to test.jsp to check it.
D.