Help

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

I was facing a problem and challenging it for some days until i solved it, i thought it's a good idea to create a page and documented it

as you know you can generate CRUD page rapidly with seam gen but what if users want to enter more than one record in one page for the same entity then you don't know how many instances do you need and have to create them dynamically

you have two steps

  1. Add UIComponents dynamically
  2. Load components which you created and create instance from component value then persist them via EntityHome

it's a good idea to create managerBean

Creating ManagerBean

@Name("managerBean")
public class ManagerBean {


	public UIComponent componentContainer;

	public UIComponent getComponentContainer() {
		return componentContainer;
	}

	public void setComponentContainer(UIComponent componentContainer) {
		this.componentContainer = componentContainer;
	}

	public String addComponent() {
            //......
	}

	public String saveAll() {
		//........
        }
}

After creating ManagerBean

Adding Simple UIComponent Dynamically

public String addComponent() {
		FacesContext fc= FacesContext.getCurrentInstance();
		Application app=  fc.getApplication();
		HtmlInputText inputText = 
                (HtmlInputText) app.createComponent(HtmlInputText.COMPONENT_TYPE);
		componentContainer.getChildren().add(inputText);
		return null;
}

Load all components and persist them

public String saveAll(){
	List<UIComponent> comps =  componentContainer.getChildren();
	for (Iterator iterator = comps.iterator(); iterator.hasNext();) {
	    HtmlInputText uiComponent = (HtmlInputText) iterator.next();
	    EnitytModel em = new EnitytModel();
	    em.setName(uiComponent.getValue().toString());
	    EntityModelHome emHome = 
           (EntityModelHome) Component.getInstance(EntityModelHome.class, true);
	    emHome.setInstance(img);
	    emHome.persist();
	}
		return null;
}

Now let's create UI

UI Layer

it's really simple

<s:decorate template="layout/edit.xhtml">
	<ui:define name="label">
	    <h:commandButton action="#{managerBean.addComponent()}" 
							value="Add More" />
	</ui:define>
	<h:panelGrid binding="#{managerBean.componentContainer}"></h:panelGrid>
</s:decorate>
<s:decorate template="layout/edit.xhtml">	
	<h:commandButton action="#{managerBean.saveAll()}" value="Save All" />
</s:decorate>

PS : Remeber there are difference between Component and Component instance, you can not create seam component dynamically but you can do it for component instance