Supongamos que en tu vista tienes algo así:
<h:dataTable id="myDataTable" value="#{bean.myItemList}" var="item">
<h:column>
<f:facet name="header">
#{'UnHeader'}
</f:facet>
<h:commandLink actionListener="#{bean.myActionListener">
#{item.field}
</h:commandLink>
</h:column>
</h:dataTable>
*Suponiendo que en tu bean tienes un getMyItemList con una lista de objetos MyClass(una propiedad llamada “field”)
Y tu código en tu bean algo así:
private List myItemList;
public List getMyItemList(){
return this.myItemList;
}
public void myActionListener(ActionEvent e){
UIData data = (UIData) e.getcomponent().findComponent("myDataTable");
MyClass item = (MyClass) data.getRowData();
int rowIndex = data.getRowIndex();
}
When I started with JSF I needed to know the size of certain list, and the natural thinking was… well Im gonna try #{bean.myAwesomeList.size} of course it is not going to work, so… after this I met the JSTL Functions Library and tadaaaaa!! problem solved, I used the fn:length which gives you the size of any Collections or the length of a String, and it goes something like this:
Step 1: Include in your html the xmlns of this library
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
...here goes all your namespaces...
xmlns:fn="http://java.sun.com/jsp/jstl/functions" />
Step 2: Show the magic!
<h:outputText
value="My collection size: #{fn:length(bean.myList)} Dang thats huge!" />
Posibles causas:
1.- Este error salio al utilizar un valueChangeListener, el cual entraba en un metodo que manipulaba el evento para saltarse unas fases del ciclo de vida, en la vista se usaba un selectBooleanCheckbox cuyo value era nulo, es decir una variable que no había sido inicializada, causando este error (message can not be null), la solución asignarle falso a dicha variable antes de mostrar la vista.
2.- Y de acuerdo con este link por un selectOneMenu…
NTM
Ahora en el trabajo me surgió la necesidad de darle formato a una fecha en JavaServerFaces, lo correcto es usar los Converters standars, para este caso es el tag de f:convertDateTime nos ayudará, un ejemplo de como quedaría es:
<h:outputText value="#{miFecha}">
<f:convertDateTime pattern="dd-MMM-yyyy" />
</h:outputText>
Source: Tutorial J2EE
, API
La siguiente línea de código es para obtener el tipo de navegador que esta usando el cliente al momento de realizar una petición a nuestra pagina de JavaServer Faces, obteniendo el header enviado en el request, que contiene el tipo de browser que esta utilizando el usuario.
String browser = ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getHeader(“User-Agent”);
Esta línea de código sirve para obtener la ip del cliente o ip del usuario que se encuentra realizando una petición al servidor, en el cual tienes la pagina de Java Server Faces (JSF). Yo la escribí en el bean que utilizo como soporte de la pagina, algunos le llaman BackingBean, yo solo le llamo mi Bean =).
String remoteAddr = ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRemoteAddr();