CommandLink doesn't work. Tried everything - jsf

it's my first question on the site :) But first, sorry for my bad english, i'm learning :)
Plz, i need your help. I'm blocked with an application in JSF.
I have this
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<body>
<ui:composition template="./defaultTemplate.xhtml">
<ui:define name="content">
<h1 style="margin-bottom: 0px">#{msg.default_2}</h1>
<br/>
<ul style="padding-left: 0px">
<ui:repeat value="#{categoryMB.categories}" var="categorie">
<h:outputLabel value=" -==- " style="color: #FF8620; font-size: 10px; padding-left: 0px"></h:outputLabel>
<h:form>
<h:commandLink value="#{categorie.categoryname}" action="#{categoryMB.getItemsByCat(categorie.categoryid.id)}"/>
</h:form>
</ui:repeat>
</ul>
<ui:repeat value="#{categoryMB.listItems}" var="item">
<div class="itemCategory">
<h:graphicImage class="item-image" url="#{item.urlimage}"/>
<h:outputLabel value="#{item.price} €" class="prix"></h:outputLabel>
<br/>
<h2><h:outputLabel value="#{item.name}"></h:outputLabel></h2>
<br/>
<h:form>
<h:commandLink value="#{msg.default_14}" action="#{itemMB.linkItem(item.id)}"
></h:commandLink>
</h:form>
</div>
</ui:repeat>
</ui:define>
</ui:composition>
</body> </html>
Everything is good, except the second commandLink !
I can't execute the action. I always return on the same page...
I tried everything i could and i read all subjects about that on the site but i can't find a solution. Please, i'm asking you, help me. I'm going to be crazy.
My bean for categoryMB :
package managedBean;
import entityBean.Item;
import entityBean.Translatecategory;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.view.ViewScoped;
import sessionBean.ItemFacadeLocal;
import sessionBean.TranslatecategoryFacadeLocal;
/**
*
* #author Aurélien
*/
#ManagedBean
#ViewScoped
public class CategoryMB {
#EJB
private ItemFacadeLocal itemFacade;
#EJB
private TranslatecategoryFacadeLocal translatecategoryFacade;
#ManagedProperty("#{internationalizationMB}")
private InternationalizationMB language;
private List<Item> listItems;
/**
* Creates a new instance of CategoryMB
*/
public CategoryMB() {
}
public List<Translatecategory> getCategories () {
return translatecategoryFacade.findByLanguage(language.getLocale().getLanguage());
}
public void getItemsByCat (int idCat) {
setListItems(itemFacade.findByCat(idCat));
}
public InternationalizationMB getLanguage() {
return language;
}
public void setLanguage(InternationalizationMB language) {
this.language = language;
}
public List<Item> getListItems() {
return listItems;
}
public void setListItems(List<Item> listItems) {
this.listItems = listItems;
}
}
And my bean for itemMB :
package managedBean;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
#ManagedBean
#SessionScoped
public class ItemMB implements Serializable {
private int idItem;
/**
* Creates a new instance of ItemMB
*/
public ItemMB() {
}
public int getIdItem() {
return idItem;
}
public void setIdItem(int idItem) {
this.idItem = idItem;
}
public String linkItem(int id)
{
setIdItem(id);
return "item";
}
}

You're mixing CDI #SessionScoped with JSF 2.x #SessionScoped. This is noted in your imports:
import javax.enterprise.context.SessionScoped;
#ManagedBean
#SessionScoped
public class ItemMB implements Serializable {
//...
}
This makes your managed bean to have the default scope, which in JSF 2 is #RequestScoped, so your managed bean will be re created on every request.
Fix your import to:
import javax.faces.bean.SessionScoped;
If you happen to use JSF 2.2.x, start working everything with CDI 1.1. Use #Named for your managed beans and use javax.faces.view.ViewScoped for #ViewScoped.
More info:
What's new in JSF 2.2. CDI compatible #ViewScoped

Related

value not saving to session bean

I have created a session bean but it seems my <h:selectOneMenu> is not storing the value in the bean. Can someone tell me what I am doing wrong?
In the xhtml navigates to another xhtml that displays the results. I do not have any values in faces-config.xml other than the navigation rules since I think managed bean / session bean annotations can replace that
xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<!-- Using a custom header -->
<ui:include src="/resources/ADTHeader.xhtml" />
<h:body>
<h1>Test</h1>
<h:form>
<h:selectOneMenu id="mypick"
value="#{gridMaster_backing.pickedGrid}"
converter="#{categoryConverter}"
title="ADTF" >
<f:selectItems value="#{gridMaster_backing.gridList}" var="prog" itemValue="#{prog.gridid}" itemLabel="#{prog.gridid} - #{prog.program} - #{prog.project} - #{prog.ci}" />
</h:selectOneMenu>
<br /><br />
<h:button value="View Grid" outcome="result" />
</h:form>
</h:body>
</html>
Session bean:
package edu.adtf.web;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.ejb.EJB;
import com.ray.adtf.ejb.*;
import com.ray.adtf.jpa.Gridmaster;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
#ManagedBean
#SessionScoped
public class gridMaster_backing {
#EJB
private GridMasterBean ejb;
private Collection<Gridmaster> mgrid;
private List gridList = new ArrayList();
// pickedGrid holds value picked from the gridid drop down list
private Long pickedGrid;
public Long getPickedGrid() {
//System.out.println("getPicked Grid");
return pickedGrid;
//return (long) 100;
}
public void setPickedGrid(Long pickedGrid) {
this.pickedGrid = pickedGrid;
}
// returns data to show grid form
public Collection<Gridmaster> getGridmaster(Long vgridid){
mgrid = ejb.getAllGrids(vgridid);
return mgrid;
}
public void setGridList(List gridList) {
this.gridList = gridList;
}
// list for grid list drop down
public List getGridList() {
List gridList2 = ejb.getDisplayGridList();
return gridList2;
}
}
converter:
package edu.adtf.web;
import javax.faces.bean.ManagedBean;
import edu.adtf.jpa.Gridmaster;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
#ManagedBean(name = "categoryConverterBean")
#FacesConverter(value = "categoryConverter")
public class CategoryConverter implements Converter{
#PersistenceContext
private transient EntityManager em;
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
//System.out.println(em.find(Gridmaster.class, new Long(value)));
return em.find(Gridmaster.class, new Long(value));
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Gridmaster gridmaster;
gridmaster = (Gridmaster) value;
//System.out.println(String.valueOf(gridmaster.getGridid()));
return String.valueOf(gridmaster.getGridid());
}
}
The problem is in the converter you're using. The field of your managed bean from where you're getting/setting the data is of type Long and the default value for <h:selectOneMenu> is of type String, so you only need to use a Long converter. There's a built-in converter for this case: javax.faces.Long. Make sure you're defining that the itemValue for the <h:selectItems> is type Long as well.
The code should look like this:
<h:selectOneMenu id="mypick"
value="#{gridMaster_backing.pickedGrid}"
converter="javax.faces.Long"
title="ADTF">
<!-- #{prog.gridid} must return Long -->
<f:selectItems
value="#{gridMaster_backing.gridList}"
var="prog"
itemValue="#{prog.gridid}"
itemLabel="#{prog.gridid} - #{prog.program} - #{prog.project} - #{prog.ci}" />
</h:selectOneMenu>

#PostConstruct of #ViewScoped is invoked on every request [duplicate]

This question already has an answer here:
#ViewScoped calls #PostConstruct on every postback request
(1 answer)
Closed 6 years ago.
I've got problem with opening dialog in JSF 2.2.7 and Primefaces 5. I've got button which opens a dialog and the problem is everytime when I click the button #PostConstruct method is executed. Why?
I want to invoke #PostConstruct only 1 time, but I don't want to change to scope to Session (with #SessionScope annotation it works perfectly).
It's my view:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form id="f1">
<p:dialog widgetVar="trainingDialog2" id="d1">
<h:outputText value="#{userViewBean.errorMessage}" />
</p:dialog>
<br />
<p:dataTable id="dt1" value="#{userViewBean.infoList}" var="item">
<p:column>
<p:commandButton id="btn" update=":f1:d1"
oncomplete="PF('trainingDialog2').show()"
styleClass="ui-icon ui-icon-calendar">
<f:setPropertyActionListener value="#{item.id}"
target="#{userViewBean.errorMessage}" />
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
It's my bean:
package pl.jrola.java.www.vigym.viewcontroller.beans.userview;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
#ManagedBean(name = "userViewBean")
#ViewScoped
public class UserViewBean implements Serializable {
private static final long serialVersionUID = 6994205182090669165L;
private String errorMessage;
private List<UserProfileInfoBean> infoList;
public List<UserProfileInfoBean> getInfoList() {
return infoList;
}
public void setInfoList(List<UserProfileInfoBean> infoList) {
this.infoList = infoList;
}
public UserViewBean() {
}
#PostConstruct
public void postConstruct() {
this.infoList = new ArrayList<UserProfileInfoBean>();
for (long i = 0; i < 3; i++) {
this.infoList.add(new UserProfileInfoBean(i));
}
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
You're mixing the annotations for JSF beans and CDI beans, effectively making the bean #RequestScoped, because it's the default for a #ManagedBean.
If you use JSF beans use:
javax.faces.bean.ManagedBean;
javax.faces.bean.ViewScoped;
If you wanna go for CDI beans use:
javax.inject.Named;
javax.faces.view.ViewScoped;
If your server supports CDI you should go for CDI beans.
Read more about the default scopes here:
What is the default Managed Bean Scope in a JSF 2 application?

Dynamically add and remove inputText field using primefaces

I have using primefaces in my project. I need to create and delete field dynamically on my project. Here I am going to attached my code please find it
my xhtml page is
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:panelGrid border="0" columns="3" cellpadding="4" columnClasses="control-label">
<h:panelGrid columns="3" cellpadding="4" >
<h:outputText value="Individual Email"/>
<div>
<h:dataTable value="#{utilBean.attachments}" var="attachmentBean" binding="#{utilBean.data}" id="attachments">
<h:column>
<h:inputText id="attachment" value="#{attachmentBean.emailAddress}" binding="#{utilBean.attachment}"/>
</h:column>
<h:column>
<h:commandButton id="delete" value="Delete" immediate="true" actionListener="#{utilBean.deleteAddress}"/>
</h:column>
</h:dataTable>
<h:commandButton id="add" value="Add Email Address" immediate="true" actionListener="#{utilBean.addAddress}" />
</div>
<br/>
</h:panelGrid>
</h:panelGrid>
</ui:composition>
My Bean Is:
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIData;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
#ManagedBean
#SessionScoped
public class UtilBean{
private UIData data=null;
private UIInput attachment = null;
private List<AttachmentBean> attachments = new ArrayList<AttachmentBean>();
public void addAddress(ActionEvent event){
attachments.add(new AttachmentBean());
this.updateAddresses();
FacesContext.getCurrentInstance().renderResponse();
System.out.println("adress size:" + attachments.size());
}
public void deleteAddress(ActionEvent event){
int index = data.getRowIndex();
this.updateAddresses();
this.getAttachments().remove(index);
FacesContext.getCurrentInstance().renderResponse();
}
public void updateAddresses(){
System.out.println("adress sizedfdfdfdf:" + attachments.size());
#SuppressWarnings("unchecked")
List<AttachmentBean> list = (ArrayList<AttachmentBean>)data.getValue();
for(int i =0;i<data.getRowCount();i++){
data.setRowIndex(i);
list.get(i).setEmailAddress((String)getAttachment().getSubmittedValue());
}
data.setRowIndex(0);
}
public UIData getData() {
return data;
}
public void setData(UIData data) {
this.data = data;
}
public UIInput getAttachment() {
return attachment;
}
public void setAttachment(UIInput attachment) {
this.attachment = attachment;
}
public List<AttachmentBean> getAttachments() {
return attachments;
}
public void setAttachments(List<AttachmentBean> attachments) {
this.attachments = attachments;
}
}
And the Attachment Class is
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
#ManagedBean
#SessionScoped
public class AttachmentBean implements Serializable {
private static final long serialVersionUID = 1L;
private String emailAddress;
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public AttachmentBean() {
super();
}
}
I am not able to add textBox by clicking the add button. Please help. Thanks in advance.
You need to put the whole in a <h:form>. See also commandButton/commandLink/ajax action/listener method not invoked or input value not updated.
Another major mistake is here:
<h:dataTable ... binding="#{utilBean.data}">
...
<h:inputText ... binding="#{utilBean.attachment}"/>
Remove those binding attributes. UI components are request scoped and yet you're binding them to a session scoped bean. This would fail hard if you open the same page in multiple browser windows in the same session. See also How does the 'binding' attribute work in JSF? When and how should it be used?.
That said, the #ManagedBean #SessionScoped on AttachmentBean is completely unnecessary. Get rid of those annotations. The #SessionScoped on UtilBean is also rather strange. The #ViewScoped is much better at its place here. See also How to choose the right bean scope?

JSF 2.1: h:dataTable does not refresh data

I've set-up a system which allows you to view categories of IT-equipment and their subcategories.
Now I want to be able to add a subcategory to a parentcategory via JPA.
The problem is that my entity is persisted correctly, but it does not show up in the dataTable. I need to start a new session to make it visible.
This is my xhtml-page:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Kategorie Tester</title>
<h:outputStylesheet library="css" name="tables.css"/>
<f:metadata>
<f:event type="preRenderView" listener="#{kategorieController.beforeRenderLoadKat}"/>
</f:metadata>
</h:head>
<h:body>
<h:form>
<h:dataTable value="#{kategorieController.reload()}"
var="kat"
styleClass="katview">
<h:column>
<f:facet name="header">
Kategorie
</f:facet>
<h:outputText value="#{kat.titel}"/>
</h:column>
<h:column>
<f:facet name="header">
Unterkategorie
</f:facet>
<h:selectManyCheckbox layout="pageDirection">
<f:selectItems value="#{kat.unterkategorieList}"
var="sub"
itemLabel="#{sub.titel}"
itemValue="#{sub.idunterkategorie}"/>
</h:selectManyCheckbox>
</h:column>
</h:dataTable>
<h:panelGroup>
<h:inputText value="#{kategorieController.titel}"/>
<h:selectOneMenu value="#{kategorieController.kategorie.idkategorie}">
<f:selectItems id="kategorie" value="#{kategorieController.allKats}"
var="kati"
itemLabel="#{kati.titel}"
itemValue="#{kati.idkategorie}"/>
</h:selectOneMenu>
<h:commandButton value="Add" action="#{kategorieController.addSub()}"/>
</h:panelGroup>
</h:form>
</h:body>
</html>
And these are my controllers in order to persist and relaod the data:
KategorieController:
package controller;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ComponentSystemEvent;
import javax.inject.Inject;
import model.Kategorie;
import model.Unterkategorie;
#ManagedBean
#SessionScoped
public class KategorieController implements Serializable {
#Inject
private KategorieService katService;
private List<Kategorie> allKats;
// Hinzufügen
private Unterkategorie subkategorie;
private Kategorie kategorie;
private String titel;
public void beforeRenderLoadKat(final ComponentSystemEvent event) {
if (allKats != null) {
allKats.clear();
}
allKats = katService.getAll();
subkategorie = new Unterkategorie();
kategorie = new Kategorie();
}
public void addSub() {
subkategorie.setKategorieId(kategorie);
subkategorie.setTitel(titel);
katService.addSub(this.subkategorie);
}
// GETTERS AND SETTERS
}
KategorieService:
package controller;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import model.Kategorie;
import model.Unterkategorie;
#Stateless
#TransactionAttribute(TransactionAttributeType.REQUIRED)
public class KategorieService implements Serializable {
#Inject
private KategorieFacade katFacade;
public List<Kategorie> getAll() {
return katFacade.getAll();
}
public List<Kategorie> addSub(final Unterkategorie sub) {
return katFacade.addSub(sub);
}
}
KategorieFacade:
package controller;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import model.Kategorie;
import model.Unterkategorie;
public class KategorieFacade {
#PersistenceContext
private EntityManager em;
public List<Kategorie> getAll() {
TypedQuery<Kategorie> query = em.createNamedQuery("Kategorie.findAll", Kategorie.class);
return query.getResultList();
}
public List<Kategorie> addSub(final Unterkategorie sub) {
em.persist(sub);
em.flush();
return getAll();
}
}
Any help is appreciated!
Thank you in advance!
Why don't you try to refresh the collection in the addSub() method:
public void addSub() {
subkategorie.setKategorieId(kategorie);
subkategorie.setTitel(titel);
allKats.clear();
allKats.addAll(katService.addSub(this.subkategorie));
}
And completely unrelated to the original question: you're interchanging the concepts Service and Facade with each other.
But I would suggest that instead of putting the getAll() into addSub(), make it void and do another call from the client to the service after doing the insertion.

JSF ui:repeat included by ui:include wrapped in h:panelGroup with conditional rendering... A mouthfull

Original question is below, but as I have come up with a more minimal example to demonstrate this problem, and figured it should go at the top.
Anyway, it appears that ui:repeat tags are processed before checking to see if parent elements are actually rendered. To recreate this, here is the facelet (minimalTest.xhtml):
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Test JSF <ui:repeat> inside <h:panelGroup rendered="false"></title>
</h:head>
<h:body>
<h:form>
<h1>Testing</h1>
<h:panelGroup rendered="false">
<span>#{minimalTestBean.alsoThrowsException}</span>
<ul>
<ui:repeat value="#{minimalTestBean.throwsException}" var="item">
<li>#{item}</li>
</ui:repeat>
</ul>
</h:panelGroup>
</h:form>
</h:body>
</html>
With using this bean (MinimalTestBean.java):
package com.lucastheisen.beans;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class MinimalTestBean implements Serializable {
private static final long serialVersionUID = 9045030165653014015L;
public String getAlsoThrowsException() {
throw new RuntimeException( "rendered is false so this shouldnt get called either" );
}
public List<String> getThrowsException() {
throw new RuntimeException( "rendered is false so this shouldnt get called" );
}
}
From this example you can see that the h:panelGroup that contains the ui:repeat is statically set to rendered=false which I would assume would mean that none of the EL expressions inside of that h:panelGroup would get executed. The EL expressions just call getters which throw a RuntimeException. However, the ui:repeat is actually calling the getter for its list thus causing the exception even though it should not be getting rendered in the first place. If you comment out the ui:repeat element, no exceptions get thrown (even though the other EL expression remains in the h:panelGroup) as I would expect.
Reading other questions here on stackoverflow leads me to believe that is likely related to the oft-referred-to chicken/egg issue, but I am not sure exactly why, nor what to do about it. I imagine setting the PARTIAL_STATE_SAVING to false might help, but would like to avoid the memory implications.
---- ORIGINAL QUESTION ----
Basically, I have a page that conditionally renders sections using <h:panelGroup rendered="#{modeXXX}"> wrapped around <ui:include src="pageXXX.xhtml" /> (per this answer). The problem is that if one of the pageXXX.xhtml has a <ui:repeat> inside of it, it seems to get processed even when the containing <h:panelGroup> has rendered=false. This is a problem because some of my sections rely on having been initialized by other sections that should be visited before them. Why is the included pageXXX.xhtml getting processed?
This is a painful bug and incredibly hard to boil down to a small example, but here is the most minimal case I could build that demonstrates the issue. First a base page:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Test JSF <ui:include></title>
</h:head>
<h:body>
<h:form>
<h1>#{testBean.title}</h1>
<h:panelGroup rendered="#{testBean.modeOne}">
<ui:include src="modeOne.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{testBean.modeTwo}">
<ui:include src="modeTwo.xhtml" />
</h:panelGroup>
</h:form>
</h:body>
</html>
As you can see this page will conditionally include either the modeOne page or the modeTwo page based upon the value in the testBean bean. Then you have modeOne (the default):
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<ui:composition>
<span>Okay, I&apos;m ready. Take me to </span>
<h:commandLink action="#{testBean.setModeTwo}">mode two.</h:commandLink>
</ui:composition>
</html>
Which in my real world app would be a page that sets up things needed by modeTwo. Once set up, an action on this page will direct you to modeTwo:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<ui:composition>
<div>Here is your list:</div>
<ui:repeat value="#{testBeanToo.list}" var="item">
<div>#{item}</div>
</ui:repeat>
</ui:composition>
</html>
The modeTwo page basically presents a details for the modeOne page in a ui:repeat as the actual information is in a collection. The main managed bean (TestBean):
package test.lucastheisen.beans;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class TestBean implements Serializable {
private static final long serialVersionUID = 6542086191355916513L;
private Mode mode;
#ManagedProperty( value="#{testBeanToo}" )
private TestBeanToo testBeanToo;
public TestBean() {
System.out.println( "constructing TestBean" );
setModeOne();
}
public String getTitle() {
System.out.println( "\ttb.getTitle()" );
return mode.getTitle();
}
public boolean isModeOne() {
return mode == Mode.One;
}
public boolean isModeTwo() {
return mode == Mode.Two;
}
public void setModeOne() {
this.mode = Mode.One;
}
public void setModeTwo() {
testBeanToo.getReadyCauseHereICome();
this.mode = Mode.Two;
}
public void setTestBeanToo( TestBeanToo testBeanToo ) {
this.testBeanToo = testBeanToo;
}
private enum Mode {
One("Mode One"),
Two("Mode Two");
private String title;
private Mode( String title ) {
this.title = title;
}
public String getTitle() {
return title;
}
}
}
Is the bean for all the main data, and the TestBeanToo bean would be for the details:
package test.lucastheisen.beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class TestBeanToo implements Serializable {
private static final long serialVersionUID = 6542086191355916513L;
private ObjectWithList objectWithList = null;
public TestBeanToo() {
System.out.println( "constructing TestBeanToo" );
}
public String getTitle() {
System.out.println( "\ttb2.getTitle()" );
return "Test Too";
}
public List<String> getList() {
System.out.println( "\ttb2.getList()" );
return objectWithList.getList();
}
public void getReadyCauseHereICome() {
System.out.println( "\ttb2.getList()" );
objectWithList = new ObjectWithList();
}
public class ObjectWithList {
private List<String> list;
public ObjectWithList() {
list = new ArrayList<String>();
list.add( "List item 1" );
list.add( "List item 2" );
}
public List<String> getList() {
return list;
}
}
}
<ui:repeat> does not check the rendered attribute of itself (it has actually none) and its parents when the view is to be rendered. Consider using Tomahawk's <t:dataList> instead.

Resources