I have tried a bunch of suggestions after googling but none has so far worked for me. I am trying to display a simple datatable with editable inputText values in each row. table is being populated from database via a usual List of objects. Here is my xhtml page and managed bean
Richfaces 4.2.1, JSF 2 mojarra bundled with JBoss 7.1, JDK 1.6
<rich:dataTable value="#{wlScoreBean.scoreList}" binding="#{wlScoreBean.scoreTable}" var="item" id="table" style="width:100%">
<rich:column>
<f:facet name="header">PARAM NAME</f:facet>
<h:outputText value="#{item.paramName}" />
</rich:column>
<rich:column>
<f:facet name="header">1 Score</f:facet>
<h:inputText value="#{item.name1}" />
</rich:column>
<rich:column>
<f:facet name="header">2 Score</f:facet>
<h:inputText value="#{item.name2}" />
</rich:column>
</rich:dataTable>
<br/>
<h:panelGrid columns="3" id="buttonRow">
<a4j:commandButton value="Save" render="table" execute="#this" action="#{wlScoreBean.update()}">
</a4j:commandButton>
</h:panelGrid>
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.event.ValueChangeEvent;
import org.richfaces.component.UIDataTable;
#ManagedBean(name = "wlScoreBean")
#ViewScoped
public class WS implements Serializable
{
private static final long serialVersionUID = 1L;
private HtmlDataTable scoreTable;
private List<WorkloadScore> scoreList;
public void watchScore(ValueChangeEvent e)
{
System.out.println("old value="+e.getOldValue() + ", New Value="+e.getNewValue());
}
public List<WorkloadScore> getScoreList() {
return scoreList;
}
public void setScoreList(List<WorkloadScore> scoreList) {
this.scoreList = scoreList;
}
#EJB
private WorkloadScoreManagerService wlScoreManager;
public HtmlDataTable getScoreTable() {
return scoreTable;
}
public void setScoreTable(HtmlDataTable scoreTable) {
this.scoreTable = scoreTable;
}
public WorkloadScoreBean()
{
}
#PostConstruct
private void getAllScores()
{
this.scoreList = wlScoreManager.getAllScores();
}
public void update()
{
for (WorkloadScore wls : getScoreList())
{
System.out.println(wls.getParamName()+"="+wls.getPortabilityScore()+","+wls.getVirtualizationScore());
}
//wlScoreManager.update(workloadScore);
}
}
Here's all the things I tried. All of them result in only the OLD VALUES being printed to console in the update() method.
Changed from rich:dataTable to plain old JSF h:dataTable, same result
Bound the dataTable to a Managed Bean property, checked in update() method, old values are being printed here too. I just did a getVlaue() on the UIDataTable object and cast it to List.
The List is supposed to have been updated with changed values from the form, but I do not see it happening. I did make sure to put the MBean in ViewScope.
do you have a h:form outside the datatable? and did you try to replace inputtext with inplaceinput fields?
See answer here https://community.jboss.org/thread/200684?tstart=0
I changed execute=#form and it worked! no need for ValueChangeEvent or messing with dataTable binding. The List values are updated in place and the update method prints the correct value
Related
I am trying to display a random number on a panel through PrimeFaces panel. I have the following xhtml code:
<h:form>
<p:growl id="msgs" showDetail="true" />
<p:panel id="basic" header="Random Number" style="margin-bottom:20px">
<h:panelGrid columns="2" cellpadding="10">
<h:outputText value="#{randomnum.number}" />
</h:panelGrid>
</p:panel>
</h:form>
This panel calls randomnum.number which is like this
import java.io.Serializable;
import java.util.Random;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean
#RequestScoped
public class randomnum implements Serializable {
private int number;
public randomnum() {
}
public int getNumber() {
return number;
}
#PostConstruct
public void init() {
Random r = new Random();
int Low = 10;
int High = 100;
number = r.nextInt(High-Low) + Low;
System.out.println("Random Number :"+number);
}
}
But when I run my xhtml code, I see the panel but I do not see anything inside it. Also, the System.out.println() output is not displayed on console. How do I resolve the issue? My basic aim is that when I run the xhtml code then a random number must be shown on the panel.
The solution is to use a Class named correct according to the Java Code Conventions: http://www.oracle.com/technetwork/java/codeconventions-135099.html
tl;dr:
Rename your Class "randomnum" to "Randomnum".
This way JSF will find the Bean, instantiate it, call #PostConstruct and display the value.
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?
I want to assign a List of resources to a project and then i use selectManyCheckbox :
the problem is although I select 2 resources. it return an empty list of resources(Utilisateur) but the project is successfully added :
and then this is my xhtml file:
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
and this is the method save in the ProjetComponent:
private Projet projet;
private Utilisateur utilisateurs;
#Autowired
private ProjetDAO projetDAO;
#Autowired
private UtilisateurDAO utilisateurDAO;
#Autowired
private ProjetService projetService;
#Transactional
public String saveProjetUtilisateurs1(Projet p, List<Utilisateur> utilisateur) {
projet = projetService.saveProjetUtilisateurs(p, utilisateur);
return "/jsf/projet/viewProjet.xhtml";
}
and this the method save in the ProjetService class called by the component ProjetCompnent:
#Transactional
public Projet saveProjetUtilisateurs(Projet projet,List<Utilisateur> ut)
{
projet.setAvancement(projet.getAvancement());
projet.setConfidentialite(projet.getConfidentialite());
projet.setDatedebut(projet.getDatedebut());
projet.setDatefineffective(projet.getDatefineffective());
projet.setDatefinprevu(projet.getDatefinprevu());
projet.setDescription(projet.getDescription());
projet.setDurreprojet(projet.getDurreprojet());
projet.setNomprojet(projet.getNomprojet());
projet.setObjectifprojet(projet.getObjectifprojet());
projet.setStatut(projet.getStatut());
projet.setUtilisateurs(ut);
projet = projetDAO.store(projet);
projetDAO.flush();
return projet;
}
and this is the Projet Entity:
#Entity
public class Projet implements Serializable {
#Id
private Integer idprojet;
#ManyToMany(mappedBy = "projets", fetch = FetchType.LAZY)
java.util.List<com.gestion.projet.domain.Utilisateur> utilisateurs;
}
If you want the Checkbox based multiple selection on Primeface Datatable, no need to use the explicit h:selectManyCheckbox.
You could do that by specifying selectionMode="multiple" attribute on first/last p:column.
See the Primefaces Showcase.
I want to implement a filtering facility in a JSF web application as follows: The users can add as many filters as they want. They can also delete them. So I am having a dataTable of filters. Each row consists of one h:selectOneMenu which has an ajax “change” event in order to make a second h:selectOneMenu visible in the same row. The options of the second h:selectOneMenu are calculated dynamically according to the selected option of the first.
The problem is that the value of second h:selectOneMenu is never set to the back-end object even if I added an ajax event. However the value of the first h:selectOneMenu is set.
I have the following fragment of code in an .xhtml page:
<h:form id="filterForm">
<h:dataTable id="filterTable" value="#{filterManager.filters}" var="filter">
<h:column>
<h:outputLabel value="#{msgs.filterBy}:" for="availableFilters" />
<h:selectOneMenu id="availableFilters" value="#{filter.filter}">
<f:selectItems value="#{filterManager.getProperties(typeSelector.typeSelected)}" />
<f:ajax event="change" render=":filterForm" />
</h:selectOneMenu>
</h:column>
<h:column>
<h:panelGroup id="filterValuesPanel" >
<h:outputLabel value="#{msgs.value}:" for="filterValues" rendered="#{!filter.filterEmpty}" />
<h:selectOneMenu value="#{filter.value}" id="filterValues" rendered="#{!filter.filterEmpty}" >
<f:selectItems value="#{filterManager.getPossibleAnswers(filter)}" />
<f:ajax event="change" render=":filterForm" />
</h:selectOneMenu>
</h:panelGroup>
</h:column>
<h:column>
<h:commandButton value="#{msgs.delete}" title="#{msgs.deleteFilter}">
<f:ajax event="click" listener="#{filterManager.removeFilter(filter)}" render=":filterForm" />
</h:commandButton>
</h:column>
</h:dataTable>
<h:commandButton value="#{msgs.addNewFilter}">
<f:ajax event="click" listener="#{filterManager.addNewFilter}" render=":filterForm" />
</h:commandButton>
</h:form>
I have a bean called “FilterManager” which has a ViewScoped. Important parts are shown below:
#ManagedBean
#ViewScoped
public class FilterManager implements Serializable {
private List<Filter> filters; // it has a getter
private int currentFilterId;
public void addNewFilter(AjaxBehaviorEvent event) {
this.currentFilterId++;
this.filters.add(Filter.getEmptyFilter(this.currentFilterId));
}
public void removeFilter(Filter filter) {
this.filters.remove(filter);
}
...
}
The Filter class is a normal class (not a bean) and is shown below:
public class Filter implements Serializable {
private int id;
private String filter;
private String value;
public String getFilter() {
return filter;
}
public void setFilter(String theFilter) {
if (theFilter != null && !theFilter.isEmpty())
this.filter = theFilter;
}
public String getValue() {
return value;
}
public void setValue(String theValue) {
this.value = theValue;
}
public boolean isFilterEmpty() {
return this.filter == null || this.filter.isEmpty();
}
...
}
Notice that TypeSelector is a SessionScoped bean which has a typeSelected property along with getter and setter.
The problem is: filter.filter is set correctly whereas filter.value is never set. I can't find the problem so I need your help please. Apologies for all this code but I needed to provide you with all the necessary details.
Thanks in advance!
Okay guys that was my fault. I had a bug in FilterManager.getPossibleAnswers(Filter filter) method. Basically, at the end of the method, I was setting filter.value to the first element of List unconditionally. Eg instead of writing
if (filter.getValue() == null || filter.getValue().isEmpty()) {
SelectItem first = answers.get(0);
filter.setValue((String) first.getValue());
}
I just wrote:
SelectItem first = answers.get(0);
filter.setValue((String) first.getValue());
Although filter.value was updating as normal, the value was changing back to default (first element in list) during re-rendering of dataTable component.
When I use f:validator inside a field of a dataTable, doesn't work, if I insert a breakpoint in my validator class, it never executes.
My dataTable:
<p:dataTable value="#{contrato.plantillaPrograma}" var="pl" scrollHeight="300" rendered="#{contrato.abrirPrograma}" autoUpdate="true">
<p:column headerText="#{txtMsg['crearContrato.fecha']}" style="width:125px"
sortBy="#{pl.fecha}">
<h:inputText value="#{pl.fecha}">
<f:converter converterId="dateConverter" />
</h:inputText>
</p:column>
<p:column headerText="#{txtMsg['crearContrato.programa']}" style="width:125px"
sortBy="#{pl.programa}">
<h:inputText value="#{pl.programa}" />
</p:column>
<p:column headerText="#{txtMsg['crearContrato.fechaFactura']}" style="width:125px"
sortBy="#{pl.fechaFactura}">
<p:inputText value="#{pl.fechaFactura}" validator="sheetEnFirme">
<f:converter converterId="dateConverter"/>
</p:inputText>
</p:column>
</p:dataTable>
My class:
package es.axpo.jsf.validator;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
#FacesValidator(value="sheetEnFirme")
public class SheetEnFirmeValidator implements Validator{
public void validate(FacesContext fc, UIComponent comp, Object obj)
throws ValidatorException {
throw new ValidatorException(new FacesMessage("Error"));
}
}
Make sure that the #{contrato} bean is at least #ViewScoped. Make sure that you aren't preparing plantillaPrograma in its getter method, but instead in bean's (post)constructor. Make sure that the dateConverter hasn't thrown a ConverterException which you should have noticed by a faces message in <h:message(s)> or in the server's log.
By the way, why are you not just using the builtin <f:convertDateTime> for dates?
It's solved.
My problem was on the command button, I had property process="#this", when removed, validation works fine