Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Dynamically generate JSF components.

Posted on 2004-08-31
Medium Priority
Last Modified: 2011-07-26
There doesnt seem to be a JSF category, so I guess heres the best place to ask.

Ill try to use an example to illustrate my problem. Say you want to manage a company contact list and you have many companies. Basically you have many companies and each company has a list of contacts. Each company screen has a dynamic contact list. The display is like.

Add Contact

Contact 1:<input box>Bugs Bunny</input box>
Contact 2:<input box>Daffy Duck</input box>

If the user clicks on Add Contact, I want to create a new contact info line with associated input. All the JSF examples I have seen thus far "hard code" these input boxes. How do I do this dynamically? Im assuming there will be something like an array of inputText's in the backing bean. But how can I dynamically show this?

I would imagine I would need to put some sort of place holder in the JSF page and then somehow ask it to render 1 to many of these controls that I am generating in the backing bean.

Im new to JSF/JSP, so forgive me if my question is confusing.
Question by:chrysler7
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 32

Accepted Solution

ldbkutty earned 600 total points
ID: 11947404
If i understand correctly, JS is enough for dynamically adding rows. Something like:

I am sorry if this is not what you want.

Author Comment

ID: 11947632
This is true from a UI perspective, and perhaps JSF has a way of rendering controls as the javascript you have shown.

However, I dont want to manually implement this javascript(if at all possible). I also know that relying on server side code to generate this will more than likely require a post back.

The reason I dont want to use this exact approachs is because I also want the ability to add logic to oeprate against these contact "objects" in the code behind. I may want to make sure that a contact name is unique across all companies, or ensure that a contact name is less than 5 characters long. I know this is all technically possible using javascript but I want to be able to access these objects from the backing bean. I also dont want to expose any business logic via javascript.
LVL 35

Assisted Solution

TimYates earned 900 total points
ID: 11949925
> How do I do this dynamically?

as ldbkutty says, you have to do this via javascript :-/

> I also know that relying on server side code to generate this will more than likely require a post back.

Yeah, and you will have to store all the values that have already been entered/changed, and set them again each time a row is added or removed

> I know this is all technically possible using javascript but I want to be able to access these objects from the backing bean.

That's fine, you can just do all of the row adding/deleting in javascript, then when the form is finally submitted to the server, you can dynamically check all of the values sent to the server for duplicates, etc...

You can give each row a unique name -- "name1.1" for example, then loop through all of these names on the server side to work out how many rows were submitted, and check them all off against the known list of values...
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 35

Expert Comment

ID: 11949939
You can see in that example ldbkutty posted that each row is called "txtRow" plus the line number...

Author Comment

ID: 11954992
A javascript solution will be sort of my last resort... Is it impossible to achieve this effect using JSF? I figure there *has to* be a way to do this, but I just havent found an example.
LVL 35

Expert Comment

ID: 11955441
> Is it impossible to achieve this effect using JSF?

Not without resubmitting the form...

There are 2 places this can happen...

On the server (needs a submittal), or on the client (needs javascript)

Author Comment

ID: 11965728
Yes, im looking to use a JSF approach and realize that it will result in a post back. How to do this?
LVL 35

Expert Comment

ID: 11965757
you will have to submit all of the current's values, etc, and a couple of hidden properties saying that you want to add or delete a row back to the server, which will then spit outthe modified html (dpending on the flags)...

Author Comment

ID: 11976811
I was hoping for a cleaner .NET like approach but it doesnt look like there is one... Thanks though guys!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Files go missing when using DFS (Distributed File System) Replication and how to recover them and fix it.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question