0

I wish to pre-populate a jsf form based on the parameters in a query string. How do I go about this?

Mark W
  • 5,824
  • 15
  • 59
  • 97

2 Answers2

3

Use the f:viewParam utility. Having this url:

/myForm.xhtml?nameField=Anthony

You can set the view parameter before the form itself gets rendered. Using this code in your form view:

<f:metadata>
    <f:viewParam name="nameField"
        value="#{formBean.name}" />
</f:metadata>
<h:form>
    <h:inputText value="#{formBean.name}" />
    <h:commandButton value="Send name" action="#{formBean.sendName}" />
</h:form>

The value you have specified will be set as default for the input.

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • This is great thanks. However now I have a problem that the ajax for the update of these inputs is not triggered :( – Mark W Jan 15 '14 at 11:41
  • I'll have a go at solving myself first – Mark W Jan 15 '14 at 11:58
  • That's just the way to go. Good luck! – Aritz Jan 15 '14 at 11:59
  • an update. I solved the problem mentioned in the above comment by calling the action on the page load with primefaces' remoteCommand autorun="true" – Mark W Jan 17 '14 at 11:28
  • Depending on your case consider also using [`f:viewAction`](http://www.oracle.com/technetwork/articles/java/jsf22-1377252.html) if JSF 2.2 available. – Aritz Jan 17 '14 at 11:40
-1

Write a bean function and call in ActionListener of that jsf page

Bean function code -

public void viewProperty(ActionEvent actionEvent) {
    
    FacesContext context = FacesContext.getCurrentInstance();
    
    BindingContext bindingContext = BindingContext.getCurrent();
    
    DCDataControl dc  = bindingContext.findDataControl("AppModuleDataControl"); // Name of application module in datacontrolBinding.cpx
    
    AppModuleImpl appM = (AppModuleImpl)dc.getDataProvider();
    
    ViewObject vo = appM.findViewObject("propertyInformationNew1"); //Name of ViewObject
    String brokerUsername = MyADFUtil.getFromSessionScope("brokerUsername").toString();   
    vo.setNamedWhereClauseParam("pBrokerProp", brokerUsername); //first value is parameter name and second one is value of that parameter
    vo.executeQuery();

}