Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSF and the fly likes Help <h:selectOneMenu> and Arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Help <h:selectOneMenu> and Arraylist" Watch "Help <h:selectOneMenu> and Arraylist" New topic
Author

Help <h:selectOneMenu> and Arraylist

Jesus Schneider
Greenhorn

Joined: Oct 17, 2012
Posts: 28
Hi!
I'm new with JSF. I'm working with Netbeans 7.2 and JSF 2.1
I have this XHTML:



And these Beans (both application-managed beans):

CONSOLAS BEAN:




LINEAS BEAN:



As you can see "Consola" uses "lineas".
The question is: How do I get to link the dataTable "value" in the XHTML with the corresponding backing bean "Consola.lineasmostradas(XX).CantidadArticulo" ??? I have tried lots of code with no result!
Please help me!!
Thanks in advance!!

Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

If you are new to JSF then take some graet free course or follow tutorials on JSF on the Internet.
You should see examples how to use <h:selectOneMenu> before using it.

I know that value attribute of <h:selectOneMenu> should have the the same type or supertype as type used in f:selectItems!
If elemento.getCantDispoArrayList() stores Integer instances then <h:selectOneMenu value= is Integer(or Object).
Instead of #{XXXXXXXXXXXXXXXXXX} you should have either Integer or EL #{bean.Integerproperty}

You shuld know that book 'JavaServer Faces 2.0: The Complete Reference' by Ed Burns Chris Schalk with Neil Griffin because teaches how to set up custom security
which is never secure and how to mix up JSTL with JSF which is recipe for pain!
I don't recommend this book because I myself covered it

Take a look at this tutorial on JSF http://www.coreservlets.com/JSF-Tutorial/jsf2/index.html


True person is moral, false is right!
Jesus Schneider
Greenhorn

Joined: Oct 17, 2012
Posts: 28
Hello.
First of all thanks for your answer!
I have read many tutorials and books, and I have searched over the net before asking here.
I think I haven't express myself properly in my question. I know that the value attribute of <h:selectOneMenu> should have the same type as type used in f:selectItems.
The "XXXXXXXXXXXXXXXXXX" in the code is a just a mark. It's "unknow code for me", but it's an integer.

The XXXXXXXX mark should look something like that: Consola.lineasmostradas.CantidadArticulo

The point here is the syntax. I'm trying to link the "value" atribute of the <h:selectOneMenu> to the backing bean "Consola" which uses another bean called "linea".
Something like that:

XHTML------uses-----> CONSOLA-----uses------>LINEA

How do I express that line of code?
I hope I have expressed myself.
Thanks!
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

How you can retrieve from database managed bean in this line
ArrayList<Linea> resultado=conexion.datosarraylist("select * from articulos where art_tip='1' ");
I don't understand is Linea entity because it is not written.
Anyway I have read that it is better to separate model(Entity) from logic(beans).
If Linea is not entity then it cannot be persisted into database at least I believe in that.
If Linea is enitity then to reference it from page is really simple
#{ManagedBeanName.propertythathastypeofentity.propertyofentity}

If you want to take from list certain element say 0 and take a property of 0 element, then try something like this
#{consola.lineasmostradas[0].CantidadArticulo}

I hope this will work if I realized what you really need!
Jesus Schneider
Greenhorn

Joined: Oct 17, 2012
Posts: 28
Hi!
I usually use hybernate but I'm using JDBC for this test.
My problem is that I want to link a selectOneMenu with a backing bean property that is an ArrayList<ANOTHER BEAN>.
If the bean property to be linked to were just an integer it would be simple, for example:


Then the code in the XHTML would like something like:



As you can see ALL selectOneMenu are linked to the EXAMPLE property of the backing bean.
But what I want to link each selectOneMenu with the "CantidadArticulo" in the corresponding index of "ArrayList<Linea> lineasmostradas". Something like:



I hope to express myself.
Thanks!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

This is improper:


EL is not primarily intended as a programming language, it is an Expression Language.

The proper form is:


That will invoke a public "getCantDispoArrayList()" method on the backing bean (elemento). The getCantDispoArrayList() method should be returning either an array or a List (ordered collection) of SelectItem objects whose value properties are the internal binary values and whose label properties are the corresponding texts that you wish to display in the GUI dropdown list. There must be a mechanism that can convert the binary values to/from text (String) form, since they will render the "VALUE=" attributes on the generated HTML SELECT OPTION elements, and HTML is a text-only medium.


Customer surveys are for companies who didn't pay proper attention to begin with.
Jesus Schneider
Greenhorn

Joined: Oct 17, 2012
Posts: 28
Hi!
You are totally right! Thanks.
But that's not the point of this post...
May someone give me some help with the syntax issue I'm asking for?
Thanks!!
Jesus Schneider
Greenhorn

Joined: Oct 17, 2012
Posts: 28
Any help with the syntax issue?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

It doesn't work that way. There is no syntax for that sort of thing, because JSF has other - and simpler - ways to do stuff.
Jesus Schneider
Greenhorn

Joined: Oct 17, 2012
Posts: 28
Hello!
May you tell me what other ways exist to do what I want?
I want to represent a list of LINES that come from the database and each of them has a button to add some quantity of the item to the shopping list.
Some orientation, please!
Thanks in advance.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

OK. That sounds similar to a standard Shopping Cart.

You need these objects in the UI Model (Backing Bean) for a Shopping Cart.

1. A backing bean to hold everything.

2. A DataModel object to serve as the sub-model for the dataTable control (sub-View).

3. An ordered collection (List or array) to hold the row model objects. The DataModel wraps this collection. It will be used to build your Shopping Cart line items.

3. A RowModel class to hold the dataTable's line item.

4. A collection of Domain Model objects used to build the RowModel instances. This is your "catalog" in the database. In your example, it's what you'd get back from the SQL SELECT.

The actual RowModel class is a class you design yourself as needed to hold the various column items, including the data that the user enters in the form.


The DataModel (item #2) is critical. JSF2 allows you to bind a dataTable "directly" to a general collection (item #3), but if you do that, then an anonymous #2 will be built anyway, and you won't be able to easily access critical information that's stored in the DataModel object, such as which row you clicked on if you have commandButtons or commandLinks on each row.


I'm a little confused about exactly how you want things to work, but I'm going to describe how you'd do it if you wanted a table whose rows each contained a numeric input control (inputText) and a dropdown item selection list (selectOneMenu). In other words:


Since the dropdown options for each of those list items are the same, we can use a common SelectItem collection instead of a separate SelectItem collection for each row. The skeleton of the corresponding VTL for that is like this:


Where "myDataModel" is a ListDataModel that wraps a List of MyRowModel objects. For simplicity, you can start by creating a fixed-sized list of about 10 MyRowModel objects and when you create your order from this list, only pull objects whose "itemKey" is not the empty string "". You can expand this to more dynamic models once you have the basics working.

So now we have a form containing a 10-row table, where the rows are all 2-column objects, consisting of initially a quantity of 0 and an empty itemKey (dropdown list with "-- Select --" displayed). We present this form to the user (client), who enters numbers and selects items from the dropdown lists. Then the user clicks the SUBMIT button (commandButton control) to submit the form.

Once the form has been submitted, JSF will validate all of the form's input data. If it's all valid, the backing bean (myBean) will be automatically updated using the form data. Specifically, the MyRowData list wrapped by the myDataModel object will have its row elements updated automatically by JSF. You can then process this model in the action method - for example, build a shopping cart database object using the rows in the MyRowData list that have non-zero "numItems" properties.

Disclaimer: I coded the above from memory. My memory isn't very good, so when in doubt, consult official JSF documentation. Also, I just edited this response because I'd misplaced the example class code.
Jesus Schneider
Greenhorn

Joined: Oct 17, 2012
Posts: 28
Thank you for your answer!
It helped me in some ways, really, but your basic design changes mine and it's not the same.
I want to create a datable with the data of all items AND a selectonemenu with the max number of items you can buy AND a commandLink that will add that line to the shopping cart.
I have found the required sintax to make it on my way, but after reading your answers and some documents I will change a little the design.
This is the actual syntax I used:

<h:dataTable value="#{consolas.getlistaconsolas()}" var="elemento">
<h:selectOneMenu value="#{consolas.lineasmostradas.get(consolas.buscaIndiceLinea(elemento.getCodigoArticulo())).cantidadArticulo}" >
<f:selectItems value="#{elemento.getCantDispoArrayList()}" />
</h:selectOneMenu>
</h:dataTable>

I will take more seriosly the VMC and I will modify the code...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

This is still improper:

As mentioned earlier, EL is not supposed to be a programming language. It is an Expression Language.

In particular, stuff like "#{elemento.getCantDispoArrayList()}" are perilous because the "#" marker indicates a possible read/write action and the expression supplied only works for reading. It also will make experienced JSF programmers laugh at you, because the expression "#{elemento.cantDispoArrayList}" is what you should be using.

And this one is just an absolute nightmare:


Even if it were a good idea, debugging EL is an excruciatingly painful process, which is one of the reasons what I always recommend putting the complex logic as Java code in the backing bean, not in the EL. The other reason, as I have said, is that otherwise you're violating the Separation of Concerns between Model and View that's one of the foundations of the MVC paradigm.

 
jQuery in Action, 2nd edition
 
subject: Help <h:selectOneMenu> and Arraylist
 
Similar Threads
h:datatable, how multiply row cell A with row cell B and set value in row cell C
How to use database row values to generate additional columns in datatable
lucene highlighting + jsf
Passing parameters with h: link
JSF Datatable if