ace:datatable inside composite component does not work in JSF - jsf

I try to use tag <ace:datatable> inside composite component Mainly it works well. I want to getting row data from the table to use it in bean but when I click on the row, I get error message
Internal server error
I need your help (BalusC :)) to fix this bug.
This is my component:
<?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://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:ace="http://www.icefaces.org/icefaces/components"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:icecore="http://www.icefaces.org/icefaces/core"
xmlns:ice="http://www.icesoft.com/icefaces/component" >
<head>
<title>IGNORED</title>
</head>
<body>
<ui:composition>
<cc:interface componentType="partnercard">
<!-- properties -->
...
</cc:interface>
<cc:implementation>
<div id="#{cc.clientId}">
<ace:dataTable id="partnerTable"
value="#{cc.partnersList}"
binding="#{cc.partnersTable}"
var="partner"
paginator="true"
paginatorPosition="bottom"
rows="10"
style="font-size: 11px;margin-top: 10px;"
selectionMode="single"
stateMap="#{cc.stateMap}" >
<ace:ajax event="select" render="#this" execute="#this" />
<ace:column id="id"
headerText="ID"
sortBy="#{partner.id}"
filterBy="#{partner.id}"
filterMatchMode="contains"
rendered="false">
<h:outputText id="idCell" value="#{partner.id}"/>
</ace:column>
<ace:column id="name"
headerText="Name"
sortBy="#{partner.name}"
filterBy="#{partner.name}"
filterMatchMode="contains">
<h:outputText id="nameCell" value="#{partner.name}"/>
</ace:column>
</ace:dataTable>
</div>
</cc:implementation>
</ui:composition>
</body>
</html>
Component backing bean
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.component.FacesComponent;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIInput;
import javax.faces.component.html.*;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import javax.faces.component.UIData;
import org.icefaces.ace.model.table.RowStateMap;
#FacesComponent("partnercard")
public class partnercard extends UINamingContainer implements Serializable {
private RowStateMap stateMap = new RowStateMap();
private List<PartnerData> partnersList = new ArrayList<PartnerData>();
private UIData partnersTable; // поиск партнера - таблица со списком партнеров
#Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
public void encodeBegin(FacesContext context) throws IOException {
super.encodeBegin(context);
}
public UIData getPartnersTable() {
return partnersTable;
}
public void setPartnersTable(UIData partnersTable) {
this.partnersTable = partnersTable;
}
public RowStateMap getStateMap() {
return stateMap;
}
public void setStateMap(RowStateMap stateMap) {
this.stateMap = stateMap;
}
public List<PartnerData> getPartnersList() {
return partnersList;
}
public void setPartnersList(List<PartnerData> partnersList) {
this.partnersList = partnersList;
}
}
This is PartnerData.java
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
public class PartnerData implements Serializable {
private long id = 0;
private String name = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}

Related

selectOneRadio lost checked value inside a dataList with pagination

I have this example code,
My xhtml 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:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<p:dataList value="#{dataListView.lista}" var="l" type="unordered"
itemType="none" paginator="true" rows="1" styleClass="paginated">
<f:facet name="header">
Paginator
</f:facet>
<p:selectOneRadio id="resposta" value="#{dataListView.resposta}">
<f:selectItem itemLabel="Certo" itemValue="Certo" />
<f:selectItem itemLabel="Errado" itemValue="Errado" />
</p:selectOneRadio>
</p:dataList>
</h:body>
</html>
My Bean
package br.com.so.teste;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import br.com.so.modelo.Questao;
#Named
#ViewScoped
public class DataListView implements Serializable {
private List<String> lista;
private String resposta;
#PostConstruct
public void init() {
lista = new ArrayList<>();
lista.add("1");
lista.add("2");
lista.add("3");
lista.add("4");
lista.add("5");
}
public List<String> getLista() {
return lista;
}
public void setLista(List<String> lista) {
this.lista = lista;
}
public String getResposta() {
return resposta;
}
public void setResposta(String resposta) {
this.resposta = resposta;
}
}
The error appear when i change the page view, in pagination have (1,2,3...) if i check the radio button in page '1' and i go to page '2' and i come back to page '1' the radio button is not checked.
Why does it happen?
How do i fix it?
I need to keep the value checked in the radioButtons.
I resolved using this.
I create 2 SelectoneRadio, one geting the checked value, if exists..
And another only puting the value.
I use Ajax to pass the ID.
If the map contains the id, i use ajax only to update, because the radio set the value directly in the map.
<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:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form id="formulario">
<p:dataList value="#{dataListView.lista}" var="l" type="unordered"
itemType="none" paginator="true" rows="1" styleClass="paginated">
<f:facet name="header">
Paginator
</f:facet>
<p:selectOneRadio value="#{dataListView.teste}"
rendered="#{dataListView.resposta[l] == null}">
<p:ajax listener="#{dataListView.processaResposta(l)}" event="click"
update="formulario"></p:ajax>
<f:selectItem itemLabel="Certo" itemValue="Certo" />
<f:selectItem itemLabel="Errado" itemValue="Errado" />
</p:selectOneRadio>
<p:selectOneRadio value="#{dataListView.resposta[l]}"
rendered="#{dataListView.resposta[l] != null}">
<p:ajax event="click" update="formulario"></p:ajax>
<f:selectItem itemLabel="Certo" itemValue="Certo" />
<f:selectItem itemLabel="Errado" itemValue="Errado" />
</p:selectOneRadio>
</p:dataList>
</h:form>
</h:body>
</html>
And in bean...
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class DataListView implements Serializable {
private List<String> lista;
private Map<String, String> resposta;
private String teste;
#PostConstruct
public void init() {
lista = new ArrayList<>();
lista.add("1");
lista.add("2");
lista.add("3");
lista.add("4");
lista.add("5");
resposta = new HashMap<>();
}
public List<String> getLista() {
return lista;
}
public void setLista(List<String> lista) {
this.lista = lista;
}
public Map<String, String> getResposta() {
return resposta;
}
public void setResposta(Map<String, String> resposta) {
this.resposta = resposta;
}
public void processaResposta(String action) {
resposta.put(action, teste);
teste = null;
}
public String getTeste() {
return teste;
}
public void setTeste(String teste) {
this.teste = teste;
}
}
After put a answer and ID, you need to make the value 'teste' null.

p:dashboard added panels aren't draggable

I have been trying to follow this showcase example. What I want to do is dynamically add components to a dashboard by clicking buttons on a p:splitButton, the adding of components to the dashboard works, but the panels aren't draggable for some reason. Why?
The XHTML
<!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Example</title>
</h:head>
<h:body>
<h:form id="splitButtonForm">
<p:splitButton value="Action" icon="ui-icon-circle-triangle-s">
<p:menuitem value="New text entry" icon="ui-icon-newwin"
actionListener="#{dashboardView.addTextWidget}"
update="dashboardForm:dashboardPanel" />
</p:splitButton>
</h:form>
<h:form id="dashboardForm">
<p:dashboard id="dashboardPanel" model="#{dashboardView.model}">
<p:ajax event="reorder" listener="#{dashboardView.handleReorder}"/>
</p:dashboard>
</h:form>
</h:body>
</html>
And here's the bean
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.panel.Panel;
import org.primefaces.event.DashboardReorderEvent;
import org.primefaces.model.DashboardColumn;
import org.primefaces.model.DashboardModel;
import org.primefaces.model.DefaultDashboardColumn;
import org.primefaces.model.DefaultDashboardModel;
#SuppressWarnings("serial")
#ManagedBean
#ViewScoped
public class DashboardView implements Serializable
{
private DashboardModel model;
public DashboardModel getModel()
{
return model;
}
#PostConstruct
public void init()
{
model = new DefaultDashboardModel();
DashboardColumn column1 = new DefaultDashboardColumn();
DashboardColumn column2 = new DefaultDashboardColumn();
model.addColumn(column1);
model.addColumn(column2);
}
public void addWidget(String widgetId)
{
DashboardColumn column1 = model.getColumn(0);
column1.addWidget(widgetId);
}
public void addTextWidget(ActionEvent event)
{
UIComponent dashboardPanel = FacesContext.getCurrentInstance().getViewRoot().findComponent("dashboardForm:dashboardPanel");
Panel panel = new Panel();
InputText textWidget = new InputText();
int childCount = dashboardPanel.getChildCount();
String widgetId = "widget" + String.valueOf(childCount);
panel.setId(widgetId);
panel.getChildren().add(textWidget);
addWidget(widgetId);
dashboardPanel.getChildren().add(panel);
}
public void handleReorder(DashboardReorderEvent event)
{
}
}
OK then to make the panels draggable the panels header needs to be set, so in the addTextWidget method I had to do something like
panel.setHeader(widgetId);

ManagedBean with ArrayList, how to add elements?

I'm trying to do SIMPLE webapp which show partyguest list and allow me to add new guest. I want to store guests in ArrayList. I don't know where and how to invoke party.addGuest() method.
index.xhtml
<?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://xmlns.jcp.org/jsf/html">
<h:head>
<title>Big Party</title>
</h:head>
<h:body>
<h2>Add new guest to Big Party: </h2>
<h:form>
<h:inputText id="guestName" value="#{guest.name}"/>
<h:commandButton value="Add guest" action="guests" />
</h:form>
<h:link value="GuestList" outcome="guests" />
</h:body>
</html>
guests.xhtml
<?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://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Super Party</title>
</h:head>
<h:body>
<h2>New guest:</h2>
<h:outputLabel value="#{guest.name}" />
<h2>Guests:</h2>
<ul>
<ui:repeat value="#{party.guests}" var="curr">
<li>#{curr}</li>
</ui:repeat>
</ul>
<h2>Guests count:</h2>
<h:outputLabel value="#{party.cnt}"/>
</h:body>
</html>
Party.java
package managedBeans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
#ManagedBean(name = "party")
#ApplicationScoped
public class Party implements Serializable {
private List<String> guests;
private int cnt;
public Party() {
}
#PostConstruct
public void init() {
guests = new ArrayList<>();
guests.add("Guest A");
guests.add("Guest B");
}
public List<String> getGuests() {
return guests;
}
public void addGuest(String guest) {
guests.add(guest);
}
public int getCnt() {
cnt = guests.size();
return cnt;
}
}
Guest.java
package managedBeans;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name = "guest")
#RequestScoped
public class Guest implements Serializable{
private String name;
public Guest() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
If you are using JSF 2, you can have something like this in your index.xhtml (but it can be easily converted to earlier JSF version):
...
<h:form>
<h:inputText id="guestName" value="#{party.newGuest}"/>
<h:commandButton value="Add guest" action="#{party.addGuest()}" />
</h:form>
...
And, in Party.java:
private String newGuest;
....
public String getNewGuest() {
return this.newGuest;
}
public void setNewGuest(String guest) {
this.newGuest = guest;
}
....
public void addGuest() {
guests.add(newGuest);
newGuest = null;
}
No need for Guest.java in this use case. Could be done in a nicer way, though.

Datatable 7 items on each row

Let's say I have a list of all the days in a month and I want to print it as a calendar. I want a week on each line and then a row break
in my example below, I will get a day on each row, like this:
Which is the best way to get 7 days on each row like this in JSF?
Example code:
View:
<h:dataTable value="#{myController.dayList}" var="day">
<h:column>
<h:outputText value="#{day}"/>
</h:column>
</h:dataTable>
Backbean:
#ManagedBean(name = "myController")
#SessionScoped
public class MyController {
private List <int> dayList;
public MyController()
{
dayList = getAllDaysInMonth();
}
public List <int> getAllDaysInMonth()
{
.....
}
public List <int> getDayList()
{
return dayList;
}
public void setDayList(List <int> dayList)
{
this.dayList = dayList;
}
}
if you would like to use Primefaces in your Project, you could do it in this way.
<!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"
xmlns:p="http://primefaces.org/ui">
<body>
<p:dataGrid value="#{myController.dayList}" var="day" columns="7">
<p:panel style="text-align: center; background-color: skyblue; width: 100px; height: 100px;">
Day ${day}
</p:panel>
</p:dataGrid>
</body>
</html>
The Controller class
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
#Named(value = "myController")
#SessionScoped
public class MyController implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
List<Integer> dayList = new ArrayList<>();
public MyController() {
}
#PostConstruct
public void init() {
for(int i = 1; i <= 31; i++) {
dayList.add(new Integer(i));
}
}
public List<Integer> getDayList() {
return dayList;
}
public void setDayList(List<Integer> dayList) {
this.dayList = dayList;
}
}
Looks not very nice, but does what you need.
Patrick

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.

Resources