0

My question is a bit complex, I have a managed bean, possibly A session scoped one, Now In this bean I retrieve information stored in a database about some elements(their width, their height, their X,Y coordinates..etc), But these elements'number are dynamically stored, meaning they don't have a certain number, And the other thing is they also might have another element nested inside.

Like storing a div's position and coordinates, and the p elements inside it with thier information as well, then now i want to use managed beans to create elements accordingly.

1- What should I use ? (now I am using JSF 2 Facelets and Javascript/jQuery).

2- What would be the right way to do it using the tools that you recommend.

I am looking for something that would act like this in the JSF page

Can this be done using jsf and javascript/jquery ??

for(every i in elements.div)
{
     create a div element
    then
     for(every i in div.p )
     create a p element in that div
     width of p element = p.width
    .
    .
    .
    load all element info 
}
engma
  • 1,849
  • 2
  • 26
  • 55

2 Answers2

1

You can use component binding. On your JSF page you refer your backing bean, which provides prepared JSF tree components.

<p>Here comes a dynamic content</p>
<h:panelGroup binding="#{myBackingBean.generatedElements}"/>

In your backing bean you create and nest elements as you wish

...
public class MyBackingBean {
...

private HtmlPanelGroup generatedElements = new HtmlPanelGroup();

public void init() {
     for (Element e : myElements) {
         // Take any element type you want
          HtmlOutputText subElement = new HtmlOutputText();
          subElement.setValue(e.getText());
          generatedElements.getChildren().add(subElement);
     }
}

You can also use subElement.setValueExpression method to build components with dynamic attributes defined by JSF expressions. For example for internationalization.

DRCB
  • 2,111
  • 13
  • 21
  • so i create the elements in the managed bean ? is there another way to create it in the facelet ? – engma Jul 06 '12 at 10:55
  • @Developer106, if your structure is not too complicated you can try to go through it using something like `` and render elements based on your data. For example ``. Another option is to render your HTML source in managed bean into string and inject it without escaping: `` – DRCB Jul 06 '12 at 12:33
  • so there is no for looping and if statements available ? – engma Jul 06 '12 at 17:10
  • @Developer106, for conditional statements you can use JSTL tag library ``, `` etc. However it is always recommended to use `rendered` attribute of the elements to show/hide them and keep JSF document tree stable. – DRCB Jul 09 '12 at 06:55
0

You may want to take a look at https://vaadin.com/ which is admittedly a different approach, but maybe does what you actually want and more.

Edit
Since you can't change the framework (which is very understandable), take a look at How to create dynamic JSF form fields. Basically all necessary steps are listed there. You could take a similar approach to create <div> and <p> tags instead of form elements.

Community
  • 1
  • 1
bassim
  • 886
  • 1
  • 21
  • 33