Created
February 28, 2019 18:29
-
-
Save phisad/e9709e791838ddef2c02b3f746afc971 to your computer and use it in GitHub Desktop.
Primefaces 6.2 tabs with c:forEach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.phisad.mybudget.views.budgetplan; | |
import javax.faces.application.FacesMessage; | |
import javax.faces.context.FacesContext; | |
public class Tab { | |
private String name; | |
public Tab(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public String action() { | |
FacesMessage msg = new FacesMessage("name clicked " + name); | |
FacesContext.getCurrentInstance().addMessage(null, msg); | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ui:composition template="/layout/template.xhtml" | |
xmlns="http://www.w3.org/1999/xhtml" | |
xmlns:f="http://xmlns.jcp.org/jsf/core" | |
xmlns:h="http://xmlns.jcp.org/jsf/html" | |
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" | |
xmlns:p="http://primefaces.org/ui" | |
xmlns:c="http://java.sun.com/jsp/jstl/core"> | |
<ui:define name="content"> | |
<div class="ui-g"> | |
<div class="ui-g-2"> | |
<p:commandButton value="Add new tab" | |
styleClass="ui-priority-primary" process="@this" | |
update="f:messages f:tabs" action="#{tabsView.onAddTab()}" /> | |
</div> | |
<div class="ui-g-10"></div> | |
</div> | |
<p:tabView id="tabs"> | |
<c:forEach items="#{tabsView.tabs}" var="tab"> | |
<p:tab title="#{tab.name}"> | |
<h:commandButton value="#{tab.name}" action="#{tab.action}"> | |
<p:ajax update="f:messages" /> | |
</h:commandButton> | |
</p:tab> | |
</c:forEach> | |
</p:tabView> | |
</ui:define> | |
</ui:composition> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.UUID; | |
import javax.annotation.PostConstruct; | |
import javax.faces.bean.ManagedBean; | |
import javax.faces.bean.ViewScoped; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.context.support.SpringBeanAutowiringSupport; | |
@ManagedBean | |
@ViewScoped | |
public class TabsView extends SpringBeanAutowiringSupport { | |
private List<Tab> tabs = new ArrayList<Tab>(); | |
public void onAddTab() { | |
tabs.add(new Tab(UUID.randomUUID().toString())); | |
} | |
public List<Tab> getTabs() { | |
return tabs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment