PrimeFaces DataTables not working - jsf

I'm new to PrimeFaces tried out an example in PrimeFaces datatable
public class Datatable {
private String fname;
private String lname;
private int age;
public Datatable(String fname, String lname, int age) {
// TODO Auto-generated constructor stub
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Here in the class I have declared what are the fields in the data table
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name="solodat")
#RequestScoped
public class Solodata implements Serializable{
private static final long serialVersionUID = 1L;
public Solodata() {}
private List<Datatable>addeta;
public List<Datatable> getAddeta() {
return addeta;
}
public void setAddeta(List<Datatable> addeta) {
this.addeta = addeta;
}
#PostConstruct
public void init() {
List<Datatable> addeta=new ArrayList<Datatable>();
addeta.add( new Datatable("man","eater",14));
addeta.add( new Datatable("solo","world",28));
addeta.add( new Datatable("antan","evanious",20));
addeta.add( new Datatable("hi","daa",29));
addeta.add( new Datatable("thallu","vandi",30));
addeta.add( new Datatable("prime","faces",1000));
addeta.add( new Datatable("crime","shit",1412));
addeta.add( new Datatable("shit","head",18));
}
}
Here in list I have get that values:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://www.java.com/jsf/html"
xmlns:f="http://www.java.com/jsf/core"
xmlns:p="http://www.primefaces.org/ui">
<h:head>
<title>DATA TABLES DEMO</title>
</h:head>
<h:body>
<h:form>
<h1>output values</h1>
<p:dataTable var="sol" value="#{solodat.addeta}" >
<p:column headerText="LASTNAME">
<h:outputText value="#{sol.lname}"/>
</p:column>
<p:column headerText="age">
<h:outputText value="#{sol.age}"/>
</p:column>
<p:column headerText="first">
<h:outputText value="#{sol.fname}"/>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
This is the xhtml page to get the bean values by data table but JSF is showing an empty page. Any help would be appreciated.

Try this and let us know (using CDI, removing the useless constructor, and initializing addeta properly)
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
#Named("solodat")
#RequestScoped
public class Solodata {
private List<Datatable> addeta;
public List<Datatable> getAddeta() {
return addeta;
}
public void setAddeta(List<Datatable> addeta) {
this.addeta = addeta;
}
#PostConstruct
public void init() {
addeta=new ArrayList<Datatable>();
addeta.add( new Datatable("man","eater",14));
addeta.add( new Datatable("solo","world",28));
addeta.add( new Datatable("antan","evanious",20));
addeta.add( new Datatable("hi","daa",29));
addeta.add( new Datatable("thallu","vandi",30));
addeta.add( new Datatable("prime","faces",1000));
addeta.add( new Datatable("crime","shit",1412));
addeta.add( new Datatable("shit","head",18));
}
}
And replace the facelet with this one (I changed the first lines)
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>DATA TABLES DEMO</title>
</h:head>
<h:body>
<h:form>
<h1>output values</h1>
<p:dataTable var="sol" value="#{solodat.addeta}" >
<p:column headerText="LASTNAME">
<h:outputText value="#{sol.lname}"/>
</p:column>
<p:column headerText="age">
<h:outputText value="#{sol.age}"/>
</p:column>
<p:column headerText="first">
<h:outputText value="#{sol.fname}"/>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
Now make sure you have placed Primefaces library at the right place, and that the app is being deployed on a running server without errors.
Edit: The reason you don't have data , is due to Datatable's constructor, which is incomplete.
Replace
public Datatable(String fname, String lname, int age) {
// TODO Auto-generated constructor stub
}
by
public Datatable(String fname, String lname, int age) {
this.fname = fname;
this.lname = lname;
this.age = age;
}

Small mistake: You are initializing a new local variable in init().
List<Datatable> addeta = new ArrayList<Datatable>();
Change it to:
this.addeta = new ArrayList<Datatable>();

You should use <!DOCTYPE html> instead of
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
and also change your are initializing as noone answer.
See also : Wrong doctype when one is specified in composite view

Related

Setting bean's attribute

I'm building a simple e-shop with JSF. There is a page with a list of all the products (product-list.xhtml) and then a detail page for each product (product.xhtml). product-list.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"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
</h:head>
<h:body>
<f:view>
<h:outputText value="Number of items in the cart: #{shoppingCart.numberOfSelectedProducts}"/>
<h:link value="Cart" outcome="cart.xhtml"/>
<h:dataTable value="#{productController.products}" var="p">
<h:column>
#{p.name}
<h:commandLink action="product.xhtml" value="Detail">
<f:actionListener target="#{product.id}" value="#{p.id}"/>
</h:commandLink>
</h:column>
<h:column>
</h:column>
</h:dataTable>
</f:view>
</h:body>
</html>
then the product.xhtml looks following:
<?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"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<head></head>
<!--f:metadata>
<f:event type="preRenderView" listener="#{shoppingCart.loadSelectedProduct(param.id)}"/>
</f:metadata -->
<body>
<f:view>
<h:graphicImage value="resources/images/img.jpg"/>
<h:outputLabel value="The value of shoppingCart.selectedProduct is: #{shoppingCart.selectedProduct.id}"/>
</f:view>
</body>
</html>
When I run the App in the debugger, the value set correctly to the selectedProduct attribute, but then it's not accessible in the page - the output of product.xhtml looks following (if the chosen product has ID 4):
The value of shoppingCart.selectedProduct is:
Products on the main page are loaded from RequestScoped bean's (productController) property. The ShoppingCart is then a SessionScoped bean with injected productController instance.
package main.java;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;
#Named
#SessionScoped
public class ShoppingCart implements Serializable {
private List<Product> selectedProducts;
private int numberOfSelectedProducts;
private Product selectedProduct;
public Product getSelectedProduct() {
return selectedProduct;
}
public void setSelectedProduct(Product selectedProduct) {
this.selectedProduct = selectedProduct;
}
public List<Product> getSelectedProducts() {
return selectedProducts;
}
public void setSelectedProducts(List<Product> selectedProducts) {
this.selectedProducts = selectedProducts;
}
public int getNumberOfSelectedProducts() {
return numberOfSelectedProducts;
}
public void setNumberOfSelectedProducts(int numberOfSelectedProducts) {
this.numberOfSelectedProducts = numberOfSelectedProducts;
}
}
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
#ViewScoped
#Named
public class ProductController implements Serializable {
private List<Product> products;
#Inject
private ShoppingCart shoppingCart;
public void setProducts(List<Product> products) {
this.products = products;
}
public ShoppingCart getShoppingCart() {
return shoppingCart;
}
public void setShoppingCart(ShoppingCart shoppingCart) {
this.shoppingCart = shoppingCart;
}
public List<Product> getProducts() {
return products;
}
//v teto metode se pote budou nacitat produkty z DB
#PostConstruct
public void loadItems(){
products = new ArrayList<>();
products.add(new Product(1, "Nazev1", 11, "Popis1"));
products.add(new Product(2, "Nazev2", 22, "Popis2"));
products.add(new Product(3, "Nazev3", 33, "Popis3"));
products.add(new Product(4, "Nazev4", 44, "Popis4"));
products.add(new Product(5, "Nazev5", 55, "Popis5"));
products.add(new Product(6, "Nazev6", 66, "Popis6"));
products.add(new Product(7, "Nazev7", 77, "Popis7"));
}
public Product findById(final int id) {
List<Product> product = products.stream().filter(p -> p.getId() == id).limit(1).collect(Collectors.toList());
return product.get(0);
}
}
package main.java;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;
#Named
#ViewScoped
public class Product implements Serializable {
private int id;
private String name;
private double price;
private String description;
public Product(int id, String name, double price, String description) {
this.id = id;
this.name = name;
this.price = price;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#PostConstruct
public void init(){
System.out.println("Product created");
}
}
If you add a #PostConstruct method to your #SessionScoped bean like this:
#PostConstruct
public void init() {
System.out.println("ShoppingCart.init()");
}
You will notice that your bean is instantiated several times per request - at least one time for each #{shoppengCart...} expression. This is because using the deprecated annotation #javax.faces.bean.SessionScoped within CDI environment behaves like #NoneScoped.
You should instead use javax.enterprise.context.SessionScoped for your shopping cart.
For display of a selected product I would suggest to add a #ViewScoped bean (javax.faces.view.ViewScoped, not javax.faces.bean.ViewScoped) or even #RequestScope if you don't do AJAX stuff. (javax.enterprise.context.RequestScoped, not javax.faces.bean.RequestScoped).
Which IDE do you use - didn't it notify you that the sope used by you is deprecated?
For further reading see: How to choose the right bean scope?
In addition to the scope problem, you should probably use f:viewParam instead of f:event to inject the URL query parameter into the bean:
Instead of:
<f:metadata>
<f:event type="preRenderView" listener="#{shoppingCart.loadSelectedProduct(param.id)}"/>
</f:metadata>
do this:
<f:metadata>
<f:viewParam name="id" value="#{productDisplayBean.selectedProductId}"/>
</f:metadata>

How to pass selected row data from datatable between two beans [duplicate]

This question already has answers here:
Creating master-detail pages for entities, how to link them and which bean scope to choose
(2 answers)
Closed 6 years ago.
I've got a datatable with values in which I get from my DB.
In my class myBean, I've got an Variable of type User to store the selected row. It just can be selected one row. Now I want to call this Variable from an other bean which is called printUser to get the selected User.
But it always prints null.
View
<p:dataTable id="userDT" var="user" value="#{myBean.getUserList()}" selection="#{myBean.selectedUser}"
rowKey="#{user.id}" >
<p:column selectionMode="single" style="width:16px;text-align:center"/>
<p:column width="200" headerText="ID">
<h:outputText value="#{user.id}" />
</p:column>
<p:column width="200" headerText="Firstname">
<h:outputText value="#{user.firstname}" />
</p:column>
<p:column width="250" headerText="Lastname">
<h:outputText value="#{user.lastname}" />
</p:column>
</p:dataTable>
myBean
#Named(value = "myBean")
#ManagedBean
#SessionScoped
public class myBean implements Serializable {
private static final long serialVersionUID = 1L;
private User selectedUser = new User();
public myBean() {
}
public List<User> getUserList() {
...
}
public Patient getSelectedUser() {
return selectedUser;
}
public void setSelectedUser(User selectedUser) {
this.selectedUser= selectedUser;
}
}
User.java
public class User {
private Integer id;
private String firstname;
private String lastname;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
printUser
#Named(value = "printUser")
#ManagedBean
#RequestScoped
public class printUser {
public printUser() {
}
public void getSelectedUserData(){
myBean bean = new myBean();
User user = new User();
user = bean.getSelectedUser();
System.err.println("UserID: " + user.getID());
}
}
Hope you undertand my Problem.
Thanks alot
Excuse my English
This is an example to use the entity of the selected row in the same page and in another page. The example use Lombok Getter,Setter and Data annotations for shortness:
The entity for the DataTable:
#Entity
#Data
#TableGenerator( name = "GEN_TestEntity1", table = "ID_Generator", pkColumnName = "GEN_KEY", pkColumnValue = "GEN_TestEntity1", valueColumnName = "GEN_VALUE" )
#NamedQuery( name = TestEntity1.QUERY_ALL_TESTENTITY1, query = "SELECT te1 FROM TestEntity1 te1" )
public class TestEntity1 implements Serializable
{
public static final String QUERY_ALL_TESTENTITY1 = "query_All_TestEntity1";
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue( strategy = GenerationType.TABLE, generator = "GEN_TestEntity1" )
private int id;
#Column
private String name;
}
The ManagedBean as a controller:
#ManagedBean
#SessionScoped
public class EntityBean
{
#EJB
private EntitySER entitySER;
#Getter
#Setter
private TestEntity1 selectedEntity;
public List<TestEntity1> getAllTestEntity1()
{
return entitySER.getAllTestEntity1();
}
public void onRowSelect( SelectEvent event_ )
{
}
}
Another ManagedBean which uses the first one (if you really want it):
#ManagedBean
#RequestScoped
public class AnotherBean
{
#ManagedProperty( value="#{entityBean}" )
private EntityBean entityBean;
...
}
The stateless session bean:
#Stateless
#LocalBean
public class EntitySER
{
#PersistenceContext
private EntityManager em;
public List<TestEntity1> getAllTestEntity1()
{
Query q = em.createNamedQuery( TestEntity1.QUERY_ALL_TESTENTITY1 );
return q.getResultList();
}
}
The index page (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"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Page 1</title>
</h:head>
<h:body>
<h:form id="form">
<p:dataTable id="table_TestEntity1" value="#{entityBean.allTestEntity1}" var="entity" selection="#{entityBean.selectedEntity}"
rowKey="#{entity.id}" selectionMode="single">
<p:ajax event="rowSelect" listener="#{entityBean.onRowSelect}" update=":form:entID :form:entName"/>
<p:column>
#{entity.id}
</p:column>
<p:column>
#{entity.name}
</p:column>
</p:dataTable>
<p>
<h:outputLabel id="entID" value="#{entityBean.selectedEntity.id}"/>:
<h:outputLabel id="entName" value="#{entityBean.selectedEntity.name}"/>
</p>
<p>
<h:commandButton value="Page 2" action="/faces/another.xhtml"/>
</p>
</h:form>
</h:body>
</html>
The another Page (another.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>Another Page</title>
</h:head>
<h:body>
<p>
The selected entity: #{entityBean.selectedEntity.id}:#{entityBean.selectedEntity.name}
</p>
<h:form>
<h:commandButton value="Back" action="/faces/index.xhtml"/>
</h:form>
</h:body>
</html>

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.

How to use complex EL like A.B(x).C to set a value?

Mojara 2.1.21
I'm using primefaces editor component p:editor. The value attribute in editor is a complex EL-Statement.
<h:form>
<p:datatable value="#{bean.getItems}" var="item">
<p:column>
<p:editor value="bean.A(item).value" />
</p:column>
</p:datatable>
</h:form>
class Bean {
public Entity A (Item i) { return ...}
}
class Entity {
public String getValue();
public void setValue(String);
}
The getter Entity.getValue() is called, but the setter Entity.serValue(String) is not called, if form is submitted.
I suppose it has nothing to do with editor but a common feature of EL. How can I instruct the editor to call a setter if some changes will be made in editor by a user ?
UPDATE
The variant <p:editor value="#{multiEditorBacking.eval(editor).text}" id="textArea" /> has trouble if setter will be called. But <p:editor value="#{editor.text}" id="textArea" /> is ok. The following examples can be used for testing.
<!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"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:o="http://omnifaces.org/ui" xmlns:pe="http://primefaces.org/ui/extensions">
<ui:composition>
<h:head></h:head>
<h:body>
<h:form id="formId">
<p:dataTable value="#{multiEditorBacking.editors}" var="editor" rowIndexVar="index" >
<p:column>
<p:commandButton value="Refresh" actionListener="#{multiEditorBacking.onRefresh(index)}" update="textArea"
process="#this" />
<p:editor value="#{multiEditorBacking.eval(editor).text}" id="textArea" />
<!-- <p:editor value="#{editor.text}" id="textArea" /> -->
</p:column>
</p:dataTable>
<p:commandButton value="Save" />
</h:form>
</h:body>
</ui:composition>
</html>
MultiEditorBean.java
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
#SessionScoped
#Named
public class MultiEditorBacking implements Serializable{
private List<MultiPojo> editors;
private HashMap <Integer, MultiPojo> hash = new HashMap<Integer, MultiPojo>();
#PostConstruct
public void init(){
editors = new ArrayList<MultiPojo>();
MultiPojo m = new MultiPojo();
m.setText("hey1");
editors.add(m);
hash.put(1, m);
m=new MultiPojo();
m.setText("adf2");
editors.add(m);
hash.put(2, m);
m=new MultiPojo();
m.setText("cjd3");
editors.add(m);
hash.put(3, m);
}
public MultiPojo eval (MultiPojo m){
return m;
}
public void onRefresh (int index){
}
public List<MultiPojo> getEditors() {
return editors;
}
public void setEditors(List<MultiPojo> editors) {
this.editors = editors;
}
public HashMap <Integer, MultiPojo> getHash() {
return hash;
}
public void setHash(HashMap <Integer, MultiPojo> hash) {
this.hash = hash;
}
}
MultiPojo.java
public class MultiPojo {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
This works for me, in Mojarra 2.2.5, using EL 2.2. Are you sure you've got that EL version enabled which allows method parameter passing? You need a Servlet 3.x container available (such as Tomcat 7) or you'll need to add the library yourself. However, it seems you've got it as #{multiEditorBacking.eval(editor).text} value for your editors is being properly evaluated.
By the way, your <ui:composition> surrounding <h:head /> and <h:body /> is unecessary. Another thing I don't like from your code is the use of #SessionScoped for pure view matters. Go with #ViewScoped unless you're explicitly dealing with session related stuff.
#SessionScoped
#Named
public class MultiEditorBacking implements Serializable {
private List<MultiPojo> editors;
private HashMap<Integer, MultiPojo> hash = new HashMap<Integer, MultiPojo>();
public MultiEditorBacking() {
editors = new ArrayList<MultiPojo>();
MultiPojo m = new MultiPojo();
m.setText("hey1");
editors.add(m);
hash.put(1, m);
m = new MultiPojo();
m.setText("adf2");
editors.add(m);
hash.put(2, m);
m = new MultiPojo();
m.setText("cjd3");
editors.add(m);
hash.put(3, m);
}
public MultiPojo eval(MultiPojo m) {
return m;
}
public void onRefresh(int index) {
System.out.println("Editor " + index + " refreshed");
}
public List<MultiPojo> getEditors() {
return editors;
}
public void setEditors(List<MultiPojo> editors) {
this.editors = editors;
}
public HashMap<Integer, MultiPojo> getHash() {
return hash;
}
public void save() {
System.out.println("Editors: " + editors);
}
public void setHash(HashMap<Integer, MultiPojo> hash) {
this.hash = hash;
}
public class MultiPojo {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
#Override
public String toString() {
return "MultiPojo [text=" + text + "]";
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
<h:form id="formId">
<p:dataTable value="#{multiEditorBacking.editors}" var="editor"
rowIndexVar="index">
<p:column>
<p:commandButton value="Refresh"
actionListener="#{multiEditorBacking.onRefresh(index)}"
update="textArea" process="#this" />
<p:editor value="#{multiEditorBacking.eval(editor).text}"
id="textArea" />
</p:column>
</p:dataTable>
<p:commandButton value="Save" action="#{multiEditorBacking.save}" />
</h:form>
</h:body>
</html>
See also:
Using EL 2.2 with Tomcat 6.0.24
The solution was to use brackets in EL to get setter called.
<p:editor value="#{(multiEditorBacking.eval(editor)).text}"
id="textArea" />

Cannot get sortBy work in RichFaces 4

I am following this example: http://mkblog.exadel.com/2008/11/richfaces-built-in-sorting/
it its said that arrows should appear beside the header and the user can click it to sort.
But I cannot get the arrow appear. You help appreciated.
The code I use:
newwonder.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:rich="http://richfaces.org/rich">
<h:head>
<title>Requirement Workflow</title>
</h:head>
<h:body>
<rich:dataTable value="#{newWondersBean.sevenNewWonders}" var="wonder">
<rich:column sortBy="#{wonder.name}">
<f:facet name="header">Name</f:facet>
<h:outputText value="#{wonder.name}" />
</rich:column>
<rich:column sortBy="#{wonder.location}">
<f:facet name="header">Location</f:facet>
<h:outputText value="#{wonder.location}" />
</rich:column>
<rich:column>
<f:facet name="header">Image</f:facet>
<h:graphicImage url="#{wonder.imageUrl}" />
</rich:column>
</rich:dataTable>
</h:body>
</html>
NewWondersBean.java
package wonder;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class NewWondersBean {
private ArrayList <Wonder> sevenNewWonders = new ArrayList <Wonder>();
#PostConstruct
public void init () {
sevenNewWonders = new ArrayList <Wonder>();
sevenNewWonders.add(new Wonder("Chichen Itza", "Mexico", "http://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Chichen-Itza-Castillo-Seen-From-East.JPG/90px-Chichen-Itza-Castillo-Seen-From-East.JPG"));
sevenNewWonders.add(new Wonder("Christ the Redeemer", "Brazil", "http://upload.wikimedia.org/wikipedia/commons/thumb/5/50/CorcovadofotoRJ.jpg/90px-CorcovadofotoRJ.jpg"));
sevenNewWonders.add(new Wonder("Colosseum", "Italy", "http://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Colosseum_in_Rome%2C_Italy_-_April_2007.jpg/90px-Colosseum_in_Rome%2C_Italy_-_April_2007.jpg"));
sevenNewWonders.add(new Wonder("Great Wall of China", "China", "http://upload.wikimedia.org/wikipedia/commons/thumb/1/16/GreatWallNearBeijingWinter.jpg/90px-GreatWallNearBeijingWinter.jpg"));
sevenNewWonders.add(new Wonder("Machu Picchu", "Peru", "http://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Before_Machu_Picchu.jpg/90px-Before_Machu_Picchu.jpg"));
sevenNewWonders.add(new Wonder("Petra", "Jordan", "http://upload.wikimedia.org/wikipedia/commons/thumb/0/06/PetraMonastery.JPG/90px-PetraMonastery.JPG"));
sevenNewWonders.add(new Wonder("Taj Mahal", "India", "http://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Taj_Mahal_in_March_2004.jpg/90px-Taj_Mahal_in_March_2004.jpg"));
}
public ArrayList <Wonder> getSevenNewWonders() {
return sevenNewWonders;
}
}
Wonder.java
package wonder;
public class Wonder {
public Wonder(String string, String string2, String string3) {
// TODO Auto-generated constructor stub
this.name = string;
this.location = string2;
this.imageUrl = string3;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setLocation(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getImageUrl() {
return imageUrl;
}
private String name;
private String location;
private String imageUrl;
}
As I understand it sorting has changed with Richfaces 4.
You will need to have a Map <String, SortOrder> and you will need to add an attribute to your rich:column where sortOrders is the above Map and vendor is the key for a given column.
sortOrder="#{bean.sortsOrders['vendor']}">
After that sorting is pretty much auto-magic, but the icon's are also not in Richfaces 4 so you will manually have to add those based on the value of the SortOrder enum for each column.
I think it is a pain and a step backwards from 3.x but that is what is needed to get sorting working.
You can always check out the RichFaces Showcase site for more info
http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=extendedDataTable&sample=edt-sorting&skin=blueSky
EDIT: (Jan 2013)
With the upcoming RichFaces 4.3 Automatic Sorting is being added back in, and will include the arrows and what not. The link above should still have a good option for that.
EDIT: (Sep 2013)
RF is now in version 4.3.4 and the automatic sorting is still not back. It only works on the rich:extendedDataTable but not the rich:dataTable

Resources