2

I would like to initialize my textfield at runtime. For example, I have primefaces inputtext like this:

<p:inputText value="#{myBean.value}" id="inputText" />

And a bean class:

@PostConstruct
public void onPageLoad() {
    ....
    ....
    inputText.setMaxlength(15);
    inputText.setStyle(".....");
}

Is it possible to do this with jsf 2.0?

demdem
  • 182
  • 3
  • 14

2 Answers2

3

You could do so by binding the component to the bean:

<p:inputText binding="#{bean.input}" ... />

with

private InputText input; // +getter+setter

@PostConstruct
public void init() {
    input = new InputText();
    input.setMaxlength(15);
    input.setStyle("background: pink;");
}

// ...

This is however not the recommended approach. You should rather bind the individual attributes to a bean property instead.

<p:inputText ... maxlength="#{bean.maxlength}" style="#{bean.style}" />

with

private Integer maxlength;
private String style; 

@PostConstruct
public void init() {
    maxlength = 15;
    style = "background: pink;";
}

// ...

Even more, if your app is well designed, then you should already have such a bean object for this (why/how else would you like to be able to specify it during runtime?). Make it a property of the managed bean instead so that you can do something like:

<p:inputText ... maxlength="#{bean.attributes.maxlength}" style="#{bean.attributes.style}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks in advance. I would like to list all my parameter values ,which is coming from db, in a gridPane. But the number of values that will be shown in its own UIComponent. For example here is my records from db; 1)type=text,value="systemName",maxChar="10" (will be shown in inputText) 2)type=date,value="createdTime"(will be shown in calendar component) 3)type=number,value="versionNumber"(will only allow numbers in inputtext) As you see, there will be different kind of components on my page and also I need to customize it on runtime.This is my problem. – demdem Jun 04 '12 at 20:50
  • Parameter values? You mean attribute values, right? Well, you could go ahead with binding. An alternative is to create a custom tag file. – BalusC Jun 04 '12 at 20:53
  • @BalusC: Everytimes BalusC even amaze myself!. Can you refer to me some good books on JSF beside The Complete Reference. Even though its a nice one but I must admit that it is not streamlined properly. – Farhan stands with Palestine Dec 15 '14 at 14:03
0

For this you can fetch component object from jsf framework using below code

UIComponent componentObj = FacesContext.getCurrentInstance().getViewRoot().findComponent(id)

then you can type cast component object in your tag component type like if you are using inputText

HtmlInputText inputTextObj = (HtmlInputText) componentObj;

and HtmlInputText class have all the getter setter for all available attribute in tag so you can set values like

inputTextObj.setMaxlength(15);
inputTextObj.setStyle(....);
inputTextObj.setDisabled();
inputTextObj.setReadonly();
Lokesh Gupta
  • 304
  • 2
  • 8