Adding Components And There Value Bindings Programmatically

Dear Friends ,

In One of assignment I have requirement of creating JSF Components

and There Value Bindings Programmatically

on valuechange event of a SelectOneListBox I need to create some componenet and add them to a panelGrid...up to this it's okay I have already achieved this goal but my next big challenge is creating value bindings programmatically for those components...

suppose I have created a textbox component in which user has to insert some text and that text I want to store in a database ..

how to do that...?

[566 byte] By [pra_n_dea] at [2007-11-26 15:34:42]
# 1

Here are some examples:

JSF 1.1HtmlOutputText text = new HtmlOutputText();

ValueBinding value = FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myBean.text}");

text.setValueBinding("value", value);

JSF 1.2HtmlOutputText text = new HtmlOutputText();

ValueExpression value = FacesContext.getCurrentInstance().getApplication().getExpressionFactory()

.createValueExpression(FacesContext.getCurrentInstance().getELContext(), "#{myBean.text}", String.class);

text.setValueExpression("value", value);

Which renders the same as<h:outputText value="#{myBean.text}" />

BalusCa at 2007-7-8 21:52:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

tht's simplest way ..I know but suppose

my code is like this....

public void buildComponenets(int selectedId) {

ArrayList arrList = new ArrayList();

arrList = objABC.GetDocumentParameter(selectedId);

Iterator iterate = arrList.iterator();

HtmlInputTexthtmlText;

HtmlSelectBooleanCheckbox htmlSelect;

HtmlOutputLabel htmlLabel;

HtmlMessage htmlMess;

int i = 1;

ABC objABC= new ABC();

while(iterate.hasNext())

{

objABC = (ABC)iterate.next();

String compType = objABC.getControlName();

if(compType.equals("TextBox"))

{

htmlText = new HtmlInputText();

htmlLabel=new HtmlOutputLabel();

htmlMess = new HtmlMessage();

htmlLabel.setValue(objABC.getLabelName());

htmlLabel.setId("lble"+i);

htmlText.setId("txt"+i);

htmlText.setImmediate(true);

htmlMess.setId("message"+i);

htmlMess.setFor("txt"+i);

htmlText.setRequired(objABC.isValidationRequired());

if(cotrolsPan.getChildren().contains(htmlText))

{

cotrolsPan.getChildren().remove(htmlText);

}

cotrolsPan.getChildren().add(htmlLabel);

cotrolsPan.getChildren().add(htmlText);

cotrolsPan.getChildren().add(htmlMess);

}

if(compType.equals("CheckBox"))

{

htmlSelect = new HtmlSelectBooleanCheckbox();

htmlLabel=new HtmlOutputLabel();

htmlMess = new HtmlMessage();

htmlLabel.setValue(objABC.getLabelName());

htmlLabel.setId("lble"+i);

htmlLabel.setValue(objABC.getLabelName());

htmlLabel.setId("lble"+i);

htmlSelect.setId("check"+i);

htmlSelect.setImmediate(true);

htmlMess.setId("message"+i);

htmlMess.setFor("check"+i);

htmlSelect.setRequired(objABC.isValidationRequired());

if(cotrolsPan.getChildren().contains(htmlSelect))

{

cotrolsPan.getChildren().remove(htmlSelect);

}

cotrolsPan.getChildren().add(htmlLabel);

cotrolsPan.getChildren().add(htmlSelect);

cotrolsPan.getChildren().add(htmlMess);

}

i++;

}

}

now How to create value bindings for these components

pra_n_dea at 2007-7-8 21:52:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
I already have shown you how to create value bindings. It is the same approach for every component.
BalusCa at 2007-7-8 21:52:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

ValueExpression value = FacesContext.getCurrentInstance().getApplication().getExpressionFactory()

.createValueExpression(FacesContext.getCurrentInstance().getELContext(), "#{myBean.text}", String.class);

this code needs a myBean with a property text...

that directly means for every component I have created I need property alread defined in a myBean class..

but the real fact is that I don't know how many components my code is

going to create..how to define properties for those components

Will u please go deep in to the matter?

pra_n_dea at 2007-7-8 21:52:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Then put it in a single map."#{myBean.map.text}" will resolve to myBean.getMap().get("text") and myBean.getMap().put("text", text);
BalusCa at 2007-7-8 21:52:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
R You Sure ...? This will solve my Problem..
pra_n_dea at 2007-7-8 21:52:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
> R You Sure ...? Have you tried it?
BalusCa at 2007-7-8 21:52:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
it's workingthanks ...
pra_n_dea at 2007-7-8 21:52:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Hi!

I'm new to jsf, i used to work with jsp and struts and i just started a project with portlet and jsf and i'm trying ti figure out how to do this

I have the same probleme... combobox with a list, the selection of one item will fire the onvaluechanged and then the bean should go in a property file to read it and add component on the page depending of the selection

I checked what you mention earlier but the part i'm missing is how to render it, because the component i just added(HtmlOutputText) is not showing up but if i debug it i can see it in the children of the viewroot.

i like snippet of code it helps ;-)

thanks,

Denis

denis791a at 2007-7-8 21:52:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...