aspose file tools*
The moose likes JSF and the fly likes Creating List/Array of Managed Beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Creating List/Array of Managed Beans" Watch "Creating List/Array of Managed Beans" New topic
Author

Creating List/Array of Managed Beans

Kamron Abtahi
Greenhorn

Joined: Jun 17, 2010
Posts: 17
Hello Everybody,

This is my first post on this forum so let me thank everyone in advance for their help with my situation.

I am currently trying to display results I am receiving from a web service search. I have developed a webservice client which was developed in java and i want to use the JSF pages to display them.

So essentially the process goes as the following: I begin by running a userVerify.jsp. This JSP has three fields for input (which the proper values have been pre-populated via the face-config.xml for testing purposes) and a button named "verify". These fields are the username, password, and proper url for access to the webservice. So once it is deployed and since the fields have been pre-populated, the user can just click the "verify" button which will input that information into a webservice client function.

After these security parameters have been populated and accepted, it will move onto the next page which redisplays these three fields (username, password, and proper endpoint url) just for piece of mind and will have six new search parameter input fields to allow the user to search through the web serivce. These fields are simple such as (name, address, city, state, country, etc). After the fields are filled out (again pre-populated data through face-config.xml is present at intitiation for testing purposes but can be changed) the user will hit the "search" button and the search credentials are populated into the webservice client.

A response is then received in a multidimensional String[][] array. The vertical columns represent the various records that were found in the search (different entities) and the horizontal rows represent the various attributes about those different entities (name, address, city, state, country .etc). I am able to take the array using a for loop and populate the managed bean resultParams which will be used for displaying the answer within the JSP. I had issues with that originally, but figured out that the managedbean resultParams had to be a managed property of the lookupParams managedbean (which is where the search is being performed and the results received) to keep the information binded to the managed bean between pages.

Now i have one last step before the module is complete and this is where i will need help and advisement from my new colleagues on this forum. So if i perform a search, and get back 28 results or different entities, i am unable to display all of them. Through my current syntax, the for loop will go through the process of binding the attributes of the 28 different entitites to the resultParams and after the process is complete and the string "search" is returned in the function (this string is what controls the navigation case to go from the searchLookup.jsp to the searchResult.jsp) only the last cycled entity is binded to the variables and displayed in the page (searchResult.jsp).

From reading through the various forums on the internet, i feel the key behind it has to do with the list of map concept, more specifically the list concept. A list of these resultParams managed beans should do the trick in capturing all the different entities so i can display them in the searchResult.jsp. My problem is i am unable to figure out the syntax i need for the face-config.xml. I tried various combinations and am having issues. Also i am curious how can i have the <value> of the list as a generic bean rather then a specific value. Can someone please help me configure my face-config.xml so that i am capturing a list of resultParams managed beans and also explain how to display it within the jsf. I believe the syntax to display the list within the jsf has to do with the <h:dataTable value="#{managedbean.attribute} var="attribute"> tag with a <h:column><hutputText value="#{attribute}</h:column> inside of it. If you could confirm this method as the way, that would be great. This would allow me to dynamically display as many entities as were found through the search.

If there are any questions to help better clarify the situation, please do tell me. I have kept it a bit vague but can provide more information if it is needed to figure out the solution.

Thanks again

Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I too am new to JSF and just recently experienced what you describe. It sounds like one of things you are asking about is how to display a list of value objects in a JSF page as a table. This is how I did it.

I created a managed bean that is the Controller part of the MVC design pattern and set the object in the session. This way the JSP (view part of the MVC) can always access it. Below is what you need to put in your face-config.xml for defining your controller. Doing so will tell JSF to create a single instance of your Controller for each session. Create your Controller in Java.

<managed-bean>
<managed-bean-name>flightController</managed-bean-name>
<managed-bean-class>FlightController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>


Add List member variables to your Controller that hold the value objects that you want to display in your table. Then in your JSP, do something like below. Notice that flightController is the Controller. It has an instance variable (List) called originalFlights. In the JSP, we refer to each flight in originalFlights as flight. In the columns we refer to each individual attribute of flight.

<h:dataTable value="#{flightController.originalFlights}" var="flight" border="1" >
<f:facet name="header"><hutputText value="Available Flights" /></f:facet>
<h:column>
<f:facet name="header"><hutputText value="Flight" /></f:facet>
<hutputText value="#{flight.flightNum}"></hutputText>
</h:column>

<h:column>
<f:facet name="header"><hutputText value="Origin" /></f:facet>
<hutputText value="#{flight.origin}"></hutputText>
</h:column>

<h:column>
<f:facet name="header"><hutputText value="Destination" /></f:facet>
<hutputText value="#{flight.destination}"></hutputText>
</h:column>

<h:column>
<f:facet name="header"><hutputText value="Departing Date" /></f:facet>
<hutputText value="#{flight.departDate}" >
<f:convertDateTime pattern="MM/dd/yyyy hh:mm a" />
</hutputText>
</h:column>
</h:dataTable>

Kamron Abtahi
Greenhorn

Joined: Jun 17, 2010
Posts: 17
Thanks for the quick reply Tom. I am about to try what you have suggested and just wanted to ask a few questions for clarification.

So essentially the value of objects i want to display in my table is the resultParams managed bean attributes (name, address, state, etc). So i will create a controler, such as totalResults managed bean with session scope, and create a list of these resultParams managed beans within it. Once i populate the List of resultParams through a method, i can output it using the syntax you provided below?

Thank you.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Yes. But I question why you call resultParams a managed bean. The resultParams is a list of POJOs. For example, List<ResultParam or List<User>.

I prefer the latter because the class User is more descriptive and can be reused throughout your app. As a value object, the class User should have no knowledge of presentation layer or data layer, etc. Ignore this last statement if you think I'm complicating the discussion.
Kamron Abtahi
Greenhorn

Joined: Jun 17, 2010
Posts: 17
Well the reason is I had the resultParams as a managed bean. This managed bean is binding the results (name, address, city, etc) to the resultParams attributes (resultParams.name) so that i can display them. Should i be just making a list of the actual result objects coming out. The actual results are released in a Result object called finalResult. finalResult then has a .row() function that allows me to build the multidimensional String[][] array. This array i used to bind to managed bean resultParams and display it. Is this not the correct architecture for the way i am looking to do it?

I am just a few minutes away from taking an attempt at your method. I have created a new managed bean, TotalResults which contains a List. This list, then has an addResult method that uses the lists .add function to add the resultParams functions. I will now complete the output piece in html and test.

Thanks.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I didn't understand everything you said. Perhaps showing some code would help.
Kamron Abtahi
Greenhorn

Joined: Jun 17, 2010
Posts: 17
Well what code should i provide for you the .lookup() function which contains all the sets?

Essentially, there is a function which does the majority of the work with the webservice. The .loopup() is this function and contains function that access the WebServiceClient directly. This includes setting the search parameters and getting the results back into a Results object. This result object is then passed into a muldimensional String[][]. This string is then iterated through to "set" but really bind, the received String attributes to the attributes of the resultParams managed bean (name, address, city, etc). After it is binded, i display these binds within the html.

What i tried to create, which was another managed bean with a list of resultParams is causing me issues. I keep having issues with the List being null and i can no instantiate it such as List test = new List(); Keeps giving me issues, the parameterized warning message you mentioned previously is also coming up but not causing a problem yet.

Thanks
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I don't think I'll be able to help you on the web service part.

As for the List is an interface so you cannot create an instance of one. Try Also to get rid of the warnings, you say where ResultParam is the class of the instances that you wish to store in the ArrayList. (ArrayList implements List)
Kamron Abtahi
Greenhorn

Joined: Jun 17, 2010
Posts: 17
Thanks Tom, that guidance helped. I was able to create the proper List initialized for ResultParam objects. So this is where i stand now. I am able to add all the ResultParams to the List and create a list of them but for some reason it is still no showing up on the JSP page. Let me paste my code from the JSP. So keep in mind I have been able to use a object name TotalResults (a managed bean) which a List called allResultParams which has 28 records attached to it who themselves have 7 attributes a piece. Appreciate all the help and the quick responses btw. Below is my code for producing the data table

<h:dataTable value="#{totalResults.allResultParams }" var="answer" border="1">

<h:column>
<f:facet name="header"><hutputText value="Name"/></f:facet>
<hutputText value="#{answer.name}"></hutputText>
</h:column>

<h:column>
<f:facet name="header"><hutputText value="Address"/></f:facet>
<hutputText value="#{answer.address }"></hutputText>
</h:column>

<h:column>
<f:facet name="header"><hutputText value="City"/></f:facet>
<hutputText value="#{answer.city }"></hutputText>
</h:column>

</h:dataTable>
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Please use the code tags for your code. In addition to the JSP code, please display the faces-config.xml and Java code.

You say the data does not display. What does display?
Kamron Abtahi
Greenhorn

Joined: Jun 17, 2010
Posts: 17
Sorry about that, hope this makes the code display properly.

[code] <h:dataTable value="#{totalResults.allResultParams }" var="answer" border="1">

<h:column>
<f:facet name="header"><hutputText value="Name"/></f:facet>
<hutputText value="#{answer.name}"></hutputText>
</h:column>

<h:column>
<f:facet name="header"><hutputText value="Address"/></f:facet>
<hutputText value="#{answer.address }"></hutputText>
</h:column>

<h:column>
<f:facet name="header"><hutputText value="City"/></f:facet>
<hutputText value="#{answer.city }"></hutputText>
</h:column>

</h:dataTable> [/code]

I am about to head out for dinner, but i will be sure to add the other pieces of code tomorrow. What does display is just the headers, but no tables and no data attributes. Thanks for all your help today Tom, i will continue with your tomorrow morning.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Your code tags didn't work.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

Actually, what helps more is if you use the "code" button on the JavaRanch messsage editor to frame formatted code/xml.

I'm going to be nitpicky here, however. A backing bean is NOT a Controller. It's a Model. Most of the controller code in JSF is pre-written in the form of the FacesServlet, taglibs, validators and converters, etc. About the only controller logic in backing beans are the listeners, and I've written entire major applications without using listeners in any of the backing beans.

Some people might argue that Action processors are "controllers", but that's not really true. In MVC, the controller is what co-ordinates the Model with the View. Action processors invoke business logic and interact with the dispatcher. These are functions that make the MVC triad integrate into the larger framework of the application, but they're not strictly speaking part of the MVC operation itself.

It's also worth making the distinction that backing beans are VIEW models. Quite a few people have attempted to use Domain Model objects directly as backing beans. That doesn't work very well. A backing bean is the facade between the UI and the actual program functionality. To a degree, it can also be used as a business model object, but there are limits.


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

Joined: Jun 17, 2010
Posts: 17
Alright I see where your going. My controller is the webservice client that i have. It performs all of the processing of the data through functions then the data that is returned is set to managed beans. Wouldnt this make the managed bean my view as you said? Then the Model is all the functions behind the webservice client which interacts with the database, pulls from the webservice, and generates the SOAP messages being passed back and forth. I believe this is it unless my understanding of MVC isnt correct.

My bit of of code with the dataTable looks like it should produce me the right code correct? Any traction on that?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

I'd have to read your earlier explanation more carefully. But a webservice isn't MVC at all. And in fact, isn't even JSF, although a webservice can update a JSF backing bean (view model). The MVC paradigm was specifically intended that the controllers manage 2-way co-ordination between View and Model, so an update to the Model means that JSF would post back the model changes to the (JSF) view.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Still waiting on you to display your code...
Kamron Abtahi
Greenhorn

Joined: Jun 17, 2010
Posts: 17
This is face-config.xml



This is the LookParams class that has the majority of the data collection and setting

}

After this is when i try to iterate through totalResults to get the 28 results with 3 attributes to display on the jsf.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
In future posts, please post all of the code - not just the code that you think is relevant.

Try looking in your server log file or console. Did the System.outs get printed?

The following is in part a guess because I haven't see all your code:

Since you've made your TotalResults a managed bean then the JSF will create a single instance at the application level. Did you create an instance variable of TotalResults in LookupParams? I think this is where you store your data. So you have the JSP looking for the data in the TotalResults of your application object but you stored your data in the TotalResults of the LookupParams (which is in your application object).
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
After you get this working (one problem at a time), you might want to think about not using the application object for everything. In the current scenario, you can only support a single user. If multiple users try to access the application simultaneously, user A may see user B's data. Consider using the session or request objects for user data.
Kamron Abtahi
Greenhorn

Joined: Jun 17, 2010
Posts: 17
That is not an error i am receiving. Either way, if i make the change, i dont believe it will effect the display of my JSF. I have the program running accordingly, and it prints out the results in the console. I also have it populated that List of the new managed bean, TotalResults. My sole issue is the syntax for the jsf tag dataTable is not putting out the results. That is where my issue lies.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
It's great that everything is logging correctly. I guess I didn't explain well what I think is the problem . You have two instances of ResultParams list. One is created by the JSF framework. This list is empty. The is the list that your JSP is displaying. It doesn't display anything because there is no data in the list.

The other instance of the list is referenced by a member variable of your LookupParams class. This is the list that you are populating with data. The easiest way to test my theory is change your JSP to the following:

And make sure you have the appropriate getter for LookupParams.totalResults.
Kamron Abtahi
Greenhorn

Joined: Jun 17, 2010
Posts: 17
This fixed my issue. Thank you for your help gentleman.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating List/Array of Managed Beans