Primefaces p:rowExpansion shows random data - jsf

I implemented the <p:rowExpansion> from PF in my HTML, as seen below.
<p:column style="width:16px">
<p:rowToggler />
</p:column>
<p:column headerText="Content 1" sortBy="#{portview.port_comment}">
<h:outputText value="#{contentbean.content1}" />
</p:column>
<p:rowExpansion>
<p:panelGrid columns="2" columnClasses="label,value" style="width:50%">
<h:outputText value="Label:" />
<h:outputText value="#{portview.port_comment}" />
</p:panelGrid>
</p:rowExpansion>
Yet, the data shown in the expansion does not fit the row it was issued from and in the PF website I can't find any clues on how to solve this.
Q: What should I do here?
UPDATE:
I use version 8.0 of Primefaces. The outcome looks like this...
The data in the expansion shows just any value from the view.
Here's my bean behind the html...
#Named(value = "vb")
#ViewScoped
public class ViewBean implements Serializable {
#Inject
private ViewHandler vh;
#PostConstruct
public void init() {
}
public List<PortView> getPVList() {
return vh.getPortViewList();
}
And the ViewHandler looks like this...
#Stateless
public class ViewHandler {
public List<PortView> getPortViewList() {
List<PortView> pvlist = em.createQuery("SELECT v FROM PortView v", PortView.class).getResultList();
return pvlist;
}

Related

p:inputTextarea returns empty string

I have a p:inputTextarea and I need the value of it while processing the form. It turned out that every time I submit the form I get all the values except the one from the textarea. #{xyzUI.description} is a String object with regular getters and setters.
<ui:composition>
<h:form id="form1">
<p:panel rendered="...">
<p:panel id="formPanel">
<p:panelGrid columns="2" cellpadding="5">
<!-- other form elements -->
<p:outputLabel>Description:</p:outputLabel>
<p:inputTextarea value="#{xyzUI.description}" style="width: 350px;" counter="display" counterTemplate="{0} characters remaining" maxlength="2000" autoResize="true" rows="4" />
<h:panelGroup />
<h:outputText id="display" />
</p:panelGrid>
<p:commandButton rendered="#{not xyzUI.noChange}" action="#{xyzUI.submitForm}" update="formPanel" ajax="true" value="Apply" >
<p:ajax update="formPanel"></p:ajax>
</p:commandButton>
</p:panel>
</p:panel>
</h:form>
<ui:composition>
In my backing bean the value is always "". I don't know what's wrong.
public void submitForm()
{
...
tmp.setDescription(description); // String is always "" while debugging
myList.add(tmp);
RequestContext.getCurrentInstance().update("content");
}
I ran your code locally and discovered the issue. In the command button, remove the p:ajax call.
PrimeFaces command buttons are ajax enabled by default.
So change this:
<p:commandButton rendered="#{not xyzUI.noChange}" action="#{xyzUI.submitForm}" update="formPanel" ajax="true" value="Apply" >
<p:ajax update="formPanel"></p:ajax>
</p:commandButton>
To this:
<p:commandButton rendered="#{not xyzUI.noChange}" action="#{xyzUI.submitForm}" update="formPanel" value="Apply" />
My backing bean for reference
#ManagedBean
#ViewScoped
public class xyzUI implements Serializable{
private static final long serialVersionUID = 6259024062406526022L;
private String description;
private boolean noChange = false;
public xyzUI(){
}
public void submitForm(){
System.out.println(description);
}
public boolean isNoChange() {
return noChange;
}
public void setNoChange(boolean noChange) {
this.noChange = noChange;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

JSF Primefaces datatable doesn't show the right values

I have a problem with primefaces datatable. I'm getting the data from my database and it's saved in a list. That's works great! But the datatable just displays one column instead of four and this column is copied in every row.
<h:form id="userslistform">
<p:dataTable id="userlist" var="users" value="#{userBean.usersList}"
rowKey="#{userBean.username}">
<p:column headerText="ID">
<h:outputText value="#{userBean.id}" />
</p:column>
<p:column headerText="Username">
<h:outputText value="#{userBean.username}" />
</p:column>
<p:column headerText="Firstname">
<h:outputText value="#{userBean.firstname}" />
</p:column>
<p:column headerText="Surname">
<h:outputText value="#{userBean.surname}" />
</p:column>
</p:dataTable>
</h:form>
Bean:
#Named
#SessionScoped
#ManagedBean(name = "userBean")
public class UserBean implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String firstname;
private String surname;
private Long id;
private String email;
private Long treeId;
private Boolean isadmin;
private List<User> usersList;
private User selectedUser;
private UserDataQuery query = new UserDataQuery();
//getter and setters of every attribute
...
#PostConstruct
public void init() {
usersList = query.getAllUsers();
}
}
DAO:
public class UserDataQuery {
private EntityManagerFactory enf;
private EntityManager em;
public UserDataQuery() {
enf = Persistence.createEntityManagerFactory("xxxx");
em = enf.createEntityManager();
em.getTransaction().begin();
}
public List<User> getAllUsers() {
List<User> usersList = em.createNamedQuery("User.findAll", User.class)
.getResultList();
return usersList;
}
...
}
You need to use value from var inside your table, to display the data
<h:form id="userslistform">
<p:dataTable id="userlist" var="users" value="#{userBean.usersList}"
rowKey="#{userBean.username}">
<p:column headerText="ID">
<h:outputText value="#{users.id}" />
</p:column>
<p:column headerText="Username">
<h:outputText value="#{users.username}" />
</p:column>
<p:column headerText="Firstname">
<h:outputText value="#{users.firstname}" />
</p:column>
<p:column headerText="Surname">
<h:outputText value="#{users.surname}" />
</p:column>
</p:dataTable>
</h:form>
var value represents a reference to current row, at the time the table is rendered. With #{userBean.<attribute>} you were always displaying values that were fixed. And, now you don't need those attributes in the bean itself, just the list (unless, of course, you need them for another purpose).

How to set and get viewparameter in jsf?

Im trying to send data from one jsf page to another, but without success.
firstPage.xhtml
<h:form>
<p:dataTable id="dataList" value="#{firstBean.dataList}" var="data">
<p:column>
<f:facet name="header">Column 1</f:facet>
#{data.column1.id}
</p:column>
<p:column>
<f:facet name="header" >Column 2</f:facet>
#{data.colum2.name}
</p:column>
<p:column>
<f:facet name="header" >Detail Row</f:facet>
<!-- I need this button send me to another page where i will detail information about an certain id -->
<h:commandButton value="Detalhar" action="#{firstBean.someMethod}">
<f:param name="dataId" value="#{data.column1.id}"/>
</h:commandButton>
</p:column>
</p:dataTable>
</h:form>
First Bean
#ManagedBean
#ViewScoped
public class FirstBean implements Serializable{
private static final long serialVersionUID = 1L;
//list with some data
private List<Data> dataList; //getter setter
#EJB
MyDataManager manager;
#PostConstruct
public void init() {
dataList = manager.getDataList();
}
public void someMethod() throws IOException {
//do something
FacesContext.getCurrentInstance().getExternalContext().redirect("secondPage.xhtml");
}
}
secondPage.xhtml
<h:body>
<f:metadata>
<f:viewParam name="dataId" value="#{secondBean.dataDetailId}" />
<f:viewAction action="#{secondBean.init}" />
</f:metadata>
<p:dataTable value="#{secondBean.dataDetailList}" var="dataDetail">
<p:column>
<f:facet name="header">Data Detail 1</f:facet>
#{dataDetail.data1}
</p:column>
<p:column>
<f:facet name="header">Data Detail 2</f:facet>
#{dataDetail.data2}
</p:column>
<p:column>
<f:facet name="header">Data Detail 3</f:facet>
#{dataDetail.data3}
</p:column>
</p:dataTable>
</h:body>
Second Bean
#ManagedBean
#ViewScoped
public class CarteiraDetalheBean implements Serializable{
private static final long serialVersionUID = 1L;
private Integer dataId; //getter setter
private List<DataDetail> dataDetailList; //getter setter
#EJB
MyDataManager manager;
#PostConstruct
public void init() {
dataDetailList = manager.getDataDetail(dataId);
}
}
When i use:
<f:viewAction action="#{secondBean.init}" />
I gotthe message: The metadata component needs to be nested within a f:metadata tag. Suggestion: enclose the necessary components within
and when i use:
<f:event type="preRenderView" listener="#{secondBean.init()}" />
i got a null pointer exception in line:
dataDetailList = manager.getDataDetail(dataId);
What im doing wrong?

How to pass parameters between two pages (from p:commandLink with redirect=true and view scoped beans)?

I am trying to pass a parameter (a user's id from a datatable) from one page to the next where I am using it (showing the user's details). The problem is that I can not get it. What am I missing? What am I doing wrong? I am using PrimeFaces 5 and JSF 2.2 (also Hibernate 4). Is there any way that I can have the parameter #PostConstruct so that I can load the user's details immediately?
The first page with the datable
<h:form id="manageUserForm" prependId="false">
<p:growl id="messages" showDetail="true" life="20000"/>
<p:dataTable id="usersTable" var="user" value="#{manageUsers.users}">
<f:facet name="header">
#{pvtmsg.registeredUsers}
</f:facet>
<p:column headerText="ID">
<h:outputText value="#{user.id}" />
</p:column>
<p:column headerText="#{pvtmsg.username}">
<h:outputText value="#{user.username}" />
</p:column>
<p:column headerText="#{pvtmsg.firstname}">
<h:outputText value="#{user.firstname}" />
</p:column>
<p:column headerText="#{pvtmsg.lastname}">
<h:outputText value="#{user.lastname}" />
</p:column>
<p:column headerText="#{pvtmsg.email}">
<h:outputText value="#{user.email}" />
</p:column>
<p:column headerText="#{pvtmsg.status}">
<h:outputText value="#{user.enabled ? pvtmsg.enabled : pvtmsg.disabled}" />
</p:column>
<p:column style="width:32px;text-align: center">
<p:commandLink action="editUser.xhtml?faces-redirect=true">
<f:setPropertyActionListener value="#{user.id}" target="#{manageUsers.selectedUser}" />
<f:param name="userId" value="#{manageUsers.selectedUser}" />
</p:commandLink>
</p:column>
</p:dataTable>
</h:form>
Its backing bean
package beans;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import models.User;
import utils.PropertyHelper;
#Named(value = "manageUsers")
#ViewScoped
public class ManageUsers implements Serializable {
private static final long serialVersionUID = 954672295622776147L;
private List<User> users = null;
private String selectedUser = null;
public ManageUsers() {
try {
PropertyHelper helper = new PropertyHelper();
users = helper.getUsers();
} catch (Exception ex) {
Logger.getLogger(ManageUsers.class.getName()).log(Level.SEVERE, null, ex);
}
}
public List<User> getUsers() {
return users;
}
public String getSelectedUser() {
return selectedUser;
}
public void setSelectedUser(String selectedUser) {
this.selectedUser = selectedUser;
}
}
The second page
<f:metadata>
<f:viewParam name="userId" value="#{editUserBean.userId}" />
</f:metadata>
<h:form id="editUserForm" prependId="false">
<p:growl id="messages" showDetail="true" life="20000"/>
<!--nothing yet-->
</h:form>
Its backing bean
package beans;
import java.io.Serializable;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
#Named(value = "editUserBean")
#ViewScoped
public class EditUserBean implements Serializable {
private static final long serialVersionUID = 543216875622776147L;
private String userId;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
Your commandLink doesn't make sense here. Typically when you want to navigate with a commandLink, you should do a redirect in your backing bean.
If you want to navigate using outcome, then something like this should work for you:
<h:link outcome="editUser.xhtml">
<f:param name="userId" value="#{user.id}" />
</h:link>
So I found my solution here http://www.oracle.com/technetwork/articles/java/jsf22-1377252.html and, after some time, the last piece that I needed here here http://www.coderanch.com/t/625319/JSF/java/working-viewParam
Your f:viewParams won't be available during the #PostConstruct method call. You should assign a f:viewAction, since you are using JSF 2.2, to handle business processing on GET parameters.
<f:metadata>
<f:viewParam name="userId" value="#{editUserBean.userId}" />
<f:viewAction action="#{editUserBean.processUserId}"/>
</f:metadata>
See also:
Read this if using JSF older than 2.2
JSF bean: call #PostConstruct function after ViewParam is set

Display a summation of a data table column on the footer of the column

For example,
<p:dataTable var="price" value="#{testManagedBean.price}" rowIndexVar="rowIndex">
<p:column headerText="Index">
<h:outputText value="#{rowIndex+1}"/>
</p:column>
<p:column headerText="Price" style="text-align: right;">
<h:outputText value="#{price}"/>
<f:facet name="footer">
<c:set var="total" value="${total+price}"/>
<h:outputText value="#{total}"/>
</f:facet>
</p:column>
</p:dataTable>
The managed bean:
#ManagedBean
#ViewScoped
public final class TestManagedBean implements Serializable
{
private List<BigDecimal>price;
private static final long serialVersionUID = 1L;
#PostConstruct
public void init()
{
price=new ArrayList<>();
price.add(new BigDecimal(50));
price.add(new BigDecimal(100));
price.add(new BigDecimal(150));
price.add(new BigDecimal(200));
price.add(new BigDecimal(250));
price.add(new BigDecimal(300));
}
public List<BigDecimal> getPrice() {
return price;
}
}
The total of list of items is to be displayed on the footer.
Is this possible to do this summation using JSTL or otherwise without having a method that does this summation in the backing bean itself?
Currently the operation with JSTL <c:set> yields 0 on the footer of the associated column.
Something like this works for me:
<c:set var="total" value="0"/>
<c:forEach items="#{testManagedBean.price}" var="t">
<c:set var="total" value="#{total + t}"/>
</c:forEach>
<h:outputText value="#{total}"/>
I will not reject the idea it might be optimizable :-)

Resources