Dynamic number of primefaces datatables with dynamic columns - Wrong number of columns - jsf

I have to show multiple dynamic number of primefaces datatables with dynamic columns on single page. Everything works fine if number of columns in each dynamic datatable is same.
Wrong behaviour occurs when there are different number of columns in different tables. All the tables show as many columns as in the first table in the list.
Tested on:
Primefaces: 3.4 and 4.0
JSF 2.0
Here is the code demonstrating the problem:
EDIT: The following code is assigning different number of columns to different tables. The first table should have 8 columns, second should have 7 columns, and so on. But all the tables take up 8 columns; i.e. they take the number of columns from first table.
Expected Output: 5 datatables. First table having 8 columns, Second table 7 columns, Third table 6 columns, and so on. Expected Output Screenshot -- This is actually the output on pf 3.3
Actual Output: 5 tables, all having 8 columns. Data shown is correct. But empty extra columns are also being shown, which should not be shown. Actual Output Screenshot
test2.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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">
<h:head>
</h:head>
<h:body>
<h:form>
<ui:repeat value="#{tableBean.tables}" var="table">
<p:dataTable id="cars" var="car" value="#{table.carsSmall}"
style="font-size: 12px;'width: 70%;">
<p:columns value="#{table.columns}" var="column"
columnIndexVar="colIndex">
<f:facet name="header">
#{column.header}
</f:facet>
<h:outputText value="#{car[column.property]}"></h:outputText>
</p:columns>
</p:dataTable>
<br />
</ui:repeat>
</h:form>
</h:body>
</f:view>
</html>
TableBean.java:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean(name = "tableBean")
#ViewScoped
public class TableBean implements Serializable
{
private static final long serialVersionUID = 1L;
private final static List<String> VALID_COLUMN_KEYS = Arrays.asList("model", "manufacturer", "year", "color");
private final static String[] colors;
private final static String[] manufacturers;
private String columnTemplate = "model manufacturer year";
static
{
colors = new String[10];
colors[0] = "Black";
colors[1] = "White";
colors[2] = "Green";
colors[3] = "Red";
colors[4] = "Blue";
colors[5] = "Orange";
colors[6] = "Silver";
colors[7] = "Yellow";
colors[8] = "Brown";
colors[9] = "Maroon";
manufacturers = new String[10];
manufacturers[0] = "Mercedes";
manufacturers[1] = "BMW";
manufacturers[2] = "Volvo";
manufacturers[3] = "Audi";
manufacturers[4] = "Renault";
manufacturers[5] = "Opel";
manufacturers[6] = "Volkswagen";
manufacturers[7] = "Chrysler";
manufacturers[8] = "Ferrari";
manufacturers[9] = "Ford";
}
private List<Car> carsSmall;
private List<ColumnModel> columns = new ArrayList<ColumnModel>();;
private List<TableBean> tables;
public List<TableBean> getTables()
{
if (tables == null)
{
tables = new ArrayList<TableBean>();
for (int i = 0; i < 5; i++)
{
TableBean t = new TableBean();
for (int j = 5; j > i; j--)
{
t.columnTemplate += " year";
t.createDynamicColumns();
}
tables.add(t);
}
}
return tables;
}
public TableBean()
{
carsSmall = new ArrayList<Car>();
populateRandomCars(carsSmall, 9);
createDynamicColumns();
}
private void populateRandomCars(List<Car> list, int size)
{
for (int i = 0; i < size; i++)
list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));
}
public List<Car> getCarsSmall()
{
return carsSmall;
}
private int getRandomYear()
{
return (int) (Math.random() * 50 + 1960);
}
private String getRandomColor()
{
return colors[(int) (Math.random() * 10)];
}
private String getRandomManufacturer()
{
return manufacturers[(int) (Math.random() * 10)];
}
private String getRandomModel()
{
return UUID.randomUUID().toString().substring(0, 8);
}
public List<ColumnModel> getColumns()
{
return columns;
}
public String[] getManufacturers()
{
return manufacturers;
}
public String[] getColors()
{
return colors;
}
static public class ColumnModel implements Serializable
{
private static final long serialVersionUID = 1L;
private String header;
private String property;
public ColumnModel(String header, String property)
{
this.header = header;
this.property = property;
}
public String getHeader()
{
return header;
}
public String getProperty()
{
return property;
}
#Override
public String toString()
{
return "[header=" + header + ", property=" + property + "]";
}
}
public void createDynamicColumns()
{
String[] columnKeys = columnTemplate.split(" ");
columns.clear();
for (String columnKey : columnKeys)
{
String key = columnKey.trim();
if (VALID_COLUMN_KEYS.contains(key))
{
columns.add(new ColumnModel(columnKey.toUpperCase(), columnKey));
}
}
}
}
Car.java:
import java.io.Serializable;
public class Car implements Serializable
{
private static final long serialVersionUID = 1L;
private String model;
private int year;
private String manufacturer;
private String color;
public Car(String model, int year, String manufacturer, String color)
{
super();
this.model = model;
this.year = year;
this.manufacturer = manufacturer;
this.color = color;
}
public String getModel()
{
return model;
}
public void setModel(String model)
{
this.model = model;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public String getManufacturer()
{
return manufacturer;
}
public void setManufacturer(String manufacturer)
{
this.manufacturer = manufacturer;
}
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
}
}

I think you should use Primefaces 3.3 because it is showing as expected as in my case.
For now this will work but i will try to get this fixed on Primefaces 4.0 too.
Please find attached image for the same. Image

Related

¿How can I create a pagination of records? (JSF)

I'm doing a hotel room reservation page with Java Server Faces. I have a base template that contains a hotel search, with an index template that is where I show the results. I'm loading all the hotels on the front page, like cover. And I show only 9 hotels so that they do not overflow the hotels of the DIV:
Ok, I have only 10 hotels registered in the database in total .... I want to make a pagination for my page where I display only 9 hotels per page .... the result of having this, as I have 10 hotels in the database, it would be that on the next page I should only see 1 single hotel.
This is my index 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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:metadata>
<f:viewAction action="#{buscador.cargarPortada()}"/>
</f:metadata>
<body>
<ui:composition template="./templates/base.xhtml">
<ui:define name="content">
<h:form id="form2">
<h:dataTable value="#{buscador.display}" var="res">
<h:column>
<h:outputLabel value="#{res.a}" escape="false"/>
</h:column>
<h:column>
<h:outputLabel value="#{res.b}" escape="false"/>
</h:column>
<h:column>
<h:outputLabel value="#{res.c}" escape="false"/>
</h:column>
</h:dataTable>
</h:form>
</ui:define>
</ui:composition>
</body>
I am using a dataTable to display the results. I have two classes: a class that handles rows of the dataTable, and another class that constructs the display boxes of each hotel
With this class I construct each box in the table:
package Clases;
public class ResultElement {
private String nombre, imagen, localidad;
private String display;
public ResultElement(String nombre, String imagen, String localidad){
this.nombre=nombre;
this.imagen=imagen;
this.localidad=localidad;
display = "<div id='HotP'><b><p style=\"text-align:center;font-
style:italic;color:red\">" + nombre + "</p></b>"
+"<img src=\"resources/images/" + imagen +
"\"style=\"width:160px;height:70px;border:1px solid gray;\"/><br>"
+ "<span style=\"color: darkblue;text-align: center;\">"+
localidad +"</span></div>";
}
/**
* #return the nombre
*/
public String getNombre() {
return nombre;
}
/**
* #param nombre the nombre to set
*/
public void setNombre(String nombre) {
this.nombre = nombre;
}
/**
* #return the imagen
*/
public String getImagen() {
return imagen;
}
/**
* #param imagen the imagen to set
*/
public void setImagen(String imagen) {
this.imagen = imagen;
}
/**
* #return the localidad
*/
public String getLocalidad() {
return localidad;
}
/**
* #param localidad the localidad to set
*/
public void setLocalidad(String localidad) {
this.localidad = localidad;
}
/**
* #return the display
*/
public String getDisplay() {
return display;
}
/**
* #param display the display to set
*/
public void setDisplay(String display) {
this.display = display;
}
}
With this class I manage the rows of the table. I show 3 results per row:
package Clases;
public class DisplayResult {
private String A, B, C;
public DisplayResult(String a, String b, String c){
A = a;
B = b;
C = c;
}
/**
* #return the A
*/
public String getA() {
return A;
}
/**
* #param A the A to set
*/
public void setA(String A) {
this.A = A;
}
/**
* #return the B
*/
public String getB() {
return B;
}
/**
* #param B the B to set
*/
public void setB(String B) {
this.B = B;
}
/**
* #return the C
*/
public String getC() {
return C;
}
/**
* #param C the C to set
*/
public void setC(String C) {
this.C = C;
}
}
This class is where I will load the cover of the page and perform searches:
package Beans;
import Clases.DisplayResult;
import Clases.ResultElement;
import javax.inject.Named;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.sql.ResultSet;
import Database.GestorDB;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* #author SEMINARIO
*/
#Named(value = "portada")
#ManagedBean
#ViewScoped
public class Buscador {
private String fechaE, fechaS, localidad;
private int personas;
private static ArrayList<DisplayResult> displayList;
private ArrayList<String> aux = new ArrayList<>();
public Buscador() {
displayList = new ArrayList<>();
}
public String getFechaE() {
return fechaE;
}
public void setFechaE(String fechaE) {
this.fechaE = fechaE;
}
public String getFechaS() {
return fechaS;
}
public void setFechaS(String fechaS) {
this.fechaS = fechaS;
}
public String getLocalidad() {
return localidad;
}
public void setLocalidad(String localidad) {
this.localidad = localidad;
}
public int getPersonas() {
return personas;
}
public void setPersonas(int personas) {
this.personas = personas;
}
public ArrayList<DisplayResult> getDisplay() {
return displayList;
}
public void setDisplay(ArrayList<DisplayResult> listaResultado) {
Buscador.displayList = listaResultado;
}
public void agregarResultado(DisplayResult resultado){
Buscador.displayList.add(resultado);
}
public void limpiarResultados(){
Buscador.displayList.clear();
aux.clear();
}
public void calibrar(){
do{
if(aux.size()%3 != 0)
aux.add("");
}while(aux.size()%3 != 0);
}
public void cargarPortada(){
try{
ResultSet rs = GestorDB.getConsulta("SELECT HOTELES.NOMBRE AS A, HOTELES.IMGHOTEL, LOCALIDADES.NOMBRE AS B FROM HOTELES INNER JOIN LOCALIDADES"
+ " ON LOCALIDADES.IDLOCALIDADES = HOTELES.LOCALIDADES_IDLOCALIDADES FETCH FIRST 9 ROWS ONLY");
while(rs.next()){
ResultElement res = new ResultElement(rs.getString("A"), rs.getString("IMGHOTEL"),rs.getString("B"));
aux.add(res.getDisplay());
}
calibrar();
for(int i = 0; i < aux.size(); i+=3){
DisplayResult obj = new DisplayResult(aux.get(i),aux.get(i+1),aux.get(i+2));
displayList.add(obj);
}
} catch (SQLException ex) {
Logger.getLogger(Buscador.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I thought of controlling the pagination of my page with my arrayList but the truth is that I do not know how to place it in the dataTable ...
Is there an easy and practical way to do it?
Native JSF does not have any support/have components for pagination.
You could go for a Component library like Primefaces, which has a pagination components like DataGrid .

How to change the default titles in p:galleria for all images to specific titles for each image

I implemented the example in PrimeFaces for the p:galleria. I can display my images and such with no problem. But I want each image to have it's own title. Here's my code:
<p:galleria value="#{mywork.images}" var="image" panelWidth="500" panelHeight="313"
showCaption="true" style="left:350px;">
<p:graphicImage name="/galleria/dotacion/#{image}" alt="Image Description for #{image}"
title="#{mywork.titles}" style="width:100%;"/>
</p:galleria>
and here is my bean:
package org.portfolio.util;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
#ManagedBean
public class Mywork {
private List<String> images;
private String titles = "";
private String titles2 = "";
#PostConstruct
public void init() {
images = new ArrayList<String>();
for (int i = 1; i <= 7; i++) {
images.add("pierrel"+i+".jpg");
if (i == 1) {
titles = "imagen de prueba if1";
getTitles();
}
if (i == 2){
titles = "imagen de prueba if2";
getTitles();
}
}
}
public List<String> getImages() {
return images;
}
public String getTitles() {
return titles;
}
public String getTitles2() {
return titles2;
}
}
What I am doing wrong or missing?
This is just returning me "imagen de prueba if2" for every image.
You can use an Object instead of using a list of string.
In you ManagedBean
#ManagedBean
public class Mywork {
private List<ImageObject> images;
#PostConstruct
public void init() {
images = new ArrayList<String>();
for (int i = 1; i <= 7; i++) {
images.add(new ImageObject("fileName" + i + ".jpg", "myDescription", "myTitle"));
}
}
public class ImageObject{
private String fileName;
private String description;
private String title;
public ImageObject(String fileName, String description, String title){
this.fileName = fileName;
this.description = description;
this.title = title;
}
//Getters setters
}
}
in your xhtml
<p:galleria value="#{mywork.images}" var="image" panelWidth="500" panelHeight="313" showCaption="true" style="left:350px;">
<p:graphicImage name="/galleria/dotacion/#{image.fileName}" alt="#{image.description}" title="#{image.title}" style="width:100%;"/>
</p:galleria>

i can not access to my model's attributs in the dynaForm primefaces

my problem is that i can not access to my model's attributs in the dynaForm.
(i'm using spring jsf primfaces).
picture of my xhtml where i have the problem
thank you for help.
here is my code
public class Colonne {
private String nomColonne;
private String typeColonne;
private int index;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public Colonne() {
super();
}
public Colonne(String nomColonne, String typeColonne, int i) {
super();
this.nomColonne = nomColonne;
this.typeColonne = typeColonne;
this.index=i;
}
public String getNomColonne() {
return nomColonne;
}
public void setNomColonne(String nomColonne) {
this.nomColonne = nomColonne;
}
public String getTypeColonne() {
return typeColonne;
}
public void setTypeColonne(String typeColonne) {
this.typeColonne = typeColonne;
}
}
here is my managedBean
public class MonBean implements Serializable{
private static final long serialVersionUID = -5773011533863117274L;
private GestionTableImpl gestionTable;
private Table table;
private DynaFormModel model;
public static long getSerialversionuid() {
return serialVersionUID;
}
public DynaFormModel initialize() {
System.out.println("1");
model = new DynaFormModel();
Colonne col = new Colonne("a","b",0 );
DynaFormRow row = model.createRegularRow();
row.addControl(col, "columnName");
row.addControl(col, "columnType");
row.addControl(col, "add");
col = new Colonne("c","d",1 );
row = model.createRegularRow();
row.addControl(col, "columnName");
row.addControl(col, "columnType");
row.addControl(col, "add");
col = new Colonne("e","f", 2);
row = model.createRegularRow();
row.addControl(col, "columnName");
row.addControl(col, "columnType");
row.addControl(col, "add");
return model;
}
public DynaFormModel getModel() {
return model;
}
public void setModel(DynaFormModel model) {
this.model = model;
}
public Table getTable() {
return table;
}
public void setTable(Table table) {
this.table = table;
}
}
You are assigning #{monBean.initialize()} to dynaForm value attribute, use #{monbean.model} instead.
Also, you have to change initialize method of MonBean class to return void, and annotate this method with #PostConstruct.
This way it won't fail when executing and your IDE wn't complain about it.

How to do pagination in JSF 2

I am very new to JSF and was trying to implement a pagination using <ui:repeat>. I am using the #ViewScoped but currently my state is not saved. Every time the backing bean resets the page information and the next page is not displayed. Attaching the source code for reference.
WorkItemList.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">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
template="/WEB-INF/template/default.xhtml">
<ui:define name="content">
<h:form id="form">
<ui:repeat value="#{repeatPaginator.model}" var="listItem">
<div>
<h:outputText value="#{listItem}"/>
</div>
</ui:repeat>
<h:commandButton value="< prev" action="#{repeatPaginator.prev}"/>
<h:outputText value="#{repeatPaginator.pageIndex} / #{repeatPaginator.pages}"/>
<h:commandButton value="next >" action="#{repeatPaginator.next}"/>
<h:inputHidden value="#{repeatPaginator.pageIndex}"/>
</h:form>
</ui:define>
</ui:composition>
Backing Bean - RepeatPaginator.java
package test;
#ManagedBean
#ViewScoped
public class RepeatPaginator implements Serializable{
private static final long serialVersionUID = 1L;
private static final int DEFAULT_RECORDS_NUMBER = 2;
private static final int DEFAULT_PAGE_INDEX = 1;
private TestDelegate delegate;
private int records;
private int recordsTotal;
private int pageIndex;
private int pages;
private List<String> model;
public RepeatPaginator() {
delegate = new TestDelegate();
this.records = DEFAULT_RECORDS_NUMBER;
this.pageIndex = DEFAULT_PAGE_INDEX;
// Get Model
this.model = delegate.fetchCurrentList(getFirst(), getFirst()+records);
this.recordsTotal = delegate.getListSize();
if (records > 0) {
pages = records <= 0 ? 1 : recordsTotal / records;
if (recordsTotal % records > 0) {
pages++;
}
if (pages == 0) {
pages = 1;
}
} else {
records = 1;
pages = 1;
}
updateModel();
}
public void updateModel() {
int fromIndex = getFirst();
int toIndex = getFirst() + records;
if(toIndex > this.recordsTotal) {
toIndex = this.recordsTotal;
}
setModel(delegate.fetchCurrentList(fromIndex, toIndex));
}
public void next() {
if(this.pageIndex < pages) {
this.pageIndex++;
}
updateModel();
}
public void prev() {
if(this.pageIndex > 1) {
this.pageIndex--;
}
updateModel();
}
public int getRecords() {
return records;
}
public int getRecordsTotal() {
return recordsTotal;
}
public int getPageIndex() {
return pageIndex;
}
public int getPages() {
return pages;
}
public int getFirst() {
return (pageIndex * records) - records;
}
public List<String> getModel() {
if(model==null)
updateModel();
return model;
}
public void setModel(List<String> model) {
this.model = model;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
}
Delegate - TestDelegate.java
#ViewScoped
public class TestDelegate {
private List<String> list = new ArrayList<String>();
public TestDelegate()
{
this.list.add("Item 1");
this.list.add("Item 2");
this.list.add("Item 3");
this.list.add("Item 4");
this.list.add("Item 5");
this.list.add("Item 6");
this.list.add("Item 7");
this.list.add("Item 8");
this.list.add("Item 9");
this.list.add("Item 10");
this.list.add("Item 11");
}
public List<String> fetchCurrentList(int from, int to)
{
return list.subList(from, to);
}
public int getListSize()
{
return this.list.size();
}
}
The #ViewScoped bean is not "magically present all the time". At the end of the request, it's values will be serialized and the bean is destructed.
This causes a Re-Construct of the bean at the beginning of the next request (click on next). Your Container will take care to deserialize the stored values again, in order to return the actual State of the bean. BUT: This happens after Bean-Construction, meaning when you try to use the values within the Constructor of the bean, nothing is restored.
Move your logic into a method that you annotate with #PostConstruct - then all values will be loaded and you can fetch the correct result. The container will call the method annotated with #PostConstruct once it restored the prior state of the bean.
This becomes important when you start to use ManagedProperties or CDI-Injections.
public RepeatPaginator() {
//no serialized data available here, Good location to initialize some stuff
//that is independent.
delegate = new TestDelegate();
this.records = DEFAULT_RECORDS_NUMBER;
this.pageIndex = DEFAULT_PAGE_INDEX;
}
#PostConstruct
public void init() {
//Here the ViewScoped values are restored. We can start to use them.
// Get Model
this.model = delegate.fetchCurrentList(getFirst(), getFirst()+records);
this.recordsTotal = delegate.getListSize();
if (records > 0) {
pages = records <= 0 ? 1 : recordsTotal / records;
if (recordsTotal % records > 0) {
pages++;
}
if (pages == 0) {
pages = 1;
}
} else {
records = 1;
pages = 1;
}
updateModel();
}

Dynamic Textfield In JSF 2.0

hey I am using the following code to create the number of text fields as the user wants
<h:form>
<p>Number Of News <h:inputText value="#{news.noOfFields}" /></p>
<ui:repeat value="#{news.values}" var="item">
<hr/>
News #{item.no}
<h:inputText value="#{item.news}" /><br/>
</ui:repeat>
<hr/>
<h:commandButton styleClass="btn btn-blue" action="#{news.submit}" value="Save" />
</h:form>
The managed bean news has a class News as
#ManagedBean
#SessionScoped
public class News
{
private String noOfFields;
private List<NewsVO> values;
public News()
{
this.values = new ArrayList<NewsVO>();
}
public String submit() {
for(NewsVO newsVO : this.values)
{
System.out.println(newsVO.getNews());
System.out.println(newsVO.getNo());
}
return null;
// save values in database
}
public String getNoOfFields() {
return noOfFields;
}
public List<NewsVO> getValues() {
return values;
}
public void setValues(List<NewsVO> values) {
this.values = values;
}
public void setNoOfFields(String noOfFields) {
this.values = new ArrayList<NewsVO>();
try {
for(int i=0;i<Integer.valueOf(noOfFields);i++)
{
NewsVO newsVO = new NewsVO();
newsVO.setNo(i+1);
this.values.add(newsVO);
}
this.noOfFields = noOfFields;
}
catch(NumberFormatException ex) {
/*values = new String[1];*/
noOfFields = "1";
}
}
}
The NewsVO is just a javaBean class as follows
public class NewsVO
{
public int no;
public String news;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getNews() {
return news;
}
public void setNews(String news) {
this.news = news;
}
}
The problem is the values inside the input Text doesn't get reflected on pressing the save button. It gives me null, even though, I have written something inside all the textfields.
<h:inputText value="#{item.news}" />
Everytime you push the submit button, all setters in the bean are called (including setNoOfFields()). In this setter you are resetting your list, that's why you loose your values. Since you only need to modify your list if there is a size change, here is a simple way doing it :
#ManagedBean
#SessionScoped
public class News
{
private int noOfFields;
private List<NewsVO> values;
public News()
{
this.values = new ArrayList<NewsVO>();
}
public String submit()
{
for(NewsVO newsVO : this.values)
{
System.out.println(newsVO.getNews());
System.out.println(newsVO.getNo());
}
return null;
// save values in database
}
public int getNoOfFields()
{
return noOfFields;
}
public List<NewsVO> getValues()
{
return values;
}
public void setValues(List<NewsVO> values)
{
this.values = values;
}
public void setNoOfFields(int noOfFields)
{
if(noOfFields < this.noOfFields)
{
for(int i = this.noOfFields - 1;i >= noOfFields;i--)
{
getValues().remove(i);
}
}
else if(noOfFields > this.noOfFields)
{
for(int i = this.noOfFields;i < noOfFields;i++)
{
NewsVO newsVO = new NewsVO();
newsVO.setNo(i+1);
getValues().add(newsVO);
}
}
}
}
Note : I've also changed your noOfFields getter/setter for simple int, JSF will do the conversion for you.

Resources