Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bind a multple InputText values into HashMap<String,ArrayList<Integer>> in JSF

 
Suresh Kumar Rajendran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Basically I want to read table records, edit and update dynamically. Reading part is fine, only update part is having problem. Once I retrieve the records from table I'm assigning those values to dynamic InputText fields. Table has 5 records and 6 columns. I don't wants to hardcode the InputText fields with array like this below. If you see the below columns all are hard coded with numbers.

Can you please suggest me any Logic for this condition in JSF2 framework. It's Basically N rows x N Columns.


Rows:

R1

C1 C2 C3
<h:inputText value="#{controller.Tl[0]}" /> <h:inputText value="#{controller.Tl[1]}" /> <h:inputText value="#{controller.Tl[2]}" /> <h:inputText value="#{controller.Tl[3]}" /> <h:inputText value="#{controller.Tl[4]}" />

R2
C1 C2 C3
<h:inputText value="#{controller.Tl[0]}" /> <h:inputText value="#{controller.Tl[1]}" /> <h:inputText value="#{controller.Tl[2]}" /> <h:inputText value="#{controller.Tl[3]}" /> <h:inputText value="#{controller.Tl[4]}" />






 
Tim Holloway
Saloon Keeper
Pie
Posts: 18276
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. First of all, backing beans are not Controllers, they are Models. In JSF, almost invariably the Controllers are pre-written code that is part of the JSF system itself, not user-implemented code.

Aside from being pedantic, however, when you have a collection of objects, the easiest way to work with them is to use one of the collection-display JSF constructs. For tabular displays (m x n), I recommend the h:dataTable tag. For cases where a tabular layout isn't what is desired (such as when you have a collectionm of items to layout horizontally instead of vertically, you can use the ui:repeat tag. In either case, the iteration is done automatically using the underlying Model object.

Note that in cases where you are intending to incorporate input controls into one or more cells of a table, you should not attempt to use a raw collection as the table mode. Construct a JSF DataModel object and use it to wrap the underlying collection. The DataModel provides essential cursor functionality.
 
Suresh Kumar Rajendran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for reply.I was using bean only not controller.

I have tried <repeat> and <foreach> both tags none of them worked. My requirement is to update entire records (all column field) of the table, so i retrieved complete records assign to Inputtext for editing the value.

As i mentioned its an n rows x n columns. My code has mentioned below, i'm sure it's incorrect logic. Please let me know if you see any logic for this specific requirement.

Ist forech represents row count.

2nd foreach represents columns value for all the record.

Say I have table having 3 columns and 2 records on it .


Bean Code:

public String retrieve() {



parentlist=new ArrayList<Integer>();
parentlist.add(1); //Row 1
parentlist.add(2); //Row 2

list = new ArrayList<Integer>();
list.add(0,1); //Column 1 value of Row1
list.add(1,2); //Column 2 value of Row1
list.add(2,3); //Column 3 value of Row1
list.add(3,4); //Column 1 value of Row2
list.add(4,5); //Column 2 value of Row2
list.add(5,6);//Column 3 value of Row2
}

//This is will return column value based index
public List<Integer> getList() {

return list;
}

//Submit
public String processRequest() {

try {
System.out.println("***********User Saved data*****************" + list);//list

} catch (Exception anException) {
}

return "";
}



JSF code

<c:forEach items="#{backedbean.parentlist}"
var="prntstationCodeString" varStatus="prentrowCounter"
id="parentloop">

<h:outputLabel style="text-valign:middle;"
value="#{counter}"
escape="false" />

<c:forEach items="#{backedbean.list}"
var="CodeString" varStatus="rowCounter" id="loop">

<c:if test="#{rowCounter.index lt 2}"> //Since total column is 3 so it loop through up to 2

<h:inputText
value="#{bean.list[counter]}" /> // getList() will be called.

</c:if>

<c:set var="counter" value="${counter + 1}" />

</c:forEach>
</c:forEach>
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18276
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: to make sample code and XML more readable, use our "Code" button to wrap formatting tags.

JSTL does not work well mixed with JSF. JSF has better equivalents. In any case, I strongly discourage "programming" on the View Definition (xhtml). The View Definition should be a display template, not a place where coding is done.

I recommend that you get a good book on JSF and study how dataTables work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic