1

In the jsf page I created, 3 other jsp pages are added through page include, each of these has its own backing bean and pre populates from webservices, values through scriptcollector prerender method. While saving, how to get the submitted values in my save action method? Is it possible to get each of these beans with their current values from Faces Context? I am using JSF 1.1.

2 Answers2

1

You can inject one managed bean in other managed bean as managed property. In JSF 1.x this is to be done with the <managed-property> declaration in faces-config.xml.

Here's a kickoff example of how 3 web service beans are to be injected in 1 form bean.

<managed-bean>
    <managed-bean-name>webServiceBean1</managed-bean-name>
    <managed-bean-class>com.example.WebServiceBean1</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
    <managed-bean-name>webServiceBean2</managed-bean-name>
    <managed-bean-class>com.example.WebServiceBean2</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
    <managed-bean-name>webServiceBean3</managed-bean-name>
    <managed-bean-class>com.example.WebServiceBean3</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
    <managed-bean-name>formBean</managed-bean-name>
    <managed-bean-class>com.example.FormBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>webServiceBean1</property-name>
        <value>#{webServiceBean1}</value>
    </managed-property>
    <managed-property>
        <property-name>webServiceBean2</property-name>
        <value>#{webServiceBean2}</value>
    </managed-property>
    <managed-property>
        <property-name>webServiceBean3</property-name>
        <value>#{webServiceBean3}</value>
    </managed-property>
</managed-bean>

This way you can just access them "the usual way" inside the form submit method.

public class FormBean {

    private WebServiceBean1 webServiceBean1;
    private WebServiceBean2 webServiceBean2;
    private WebServiceBean3 webServiceBean3;

    public String submit() {
        // Here you can just access the submitted data through the injected beans.
    }

    // Add/generate setters. Getters are not necesary.
}

As an alternative, you can use Application#createValueBinding() to evaluate EL programmatically.

public String submit() {
    FacesContext context = FacesContext.getCurrentInstance();
    Application application = context.getApplication();
    WebServiceBean1 webServiceBean1 = (WebServiceBean1) application.createValueBinding("#{webServiceBean1}").getValue(context);
    WebServiceBean1 webServiceBean1 = (WebServiceBean2) application.createValueBinding("#{webServiceBean2}").getValue(context);
    WebServiceBean1 webServiceBean1 = (WebServiceBean3) application.createValueBinding("#{webServiceBean3}").getValue(context);
    // ...
}

Note that above methods are deprecated in JSF 1.2 and newer in favor of unified EL API. See also Get JSF managed bean by name in any Servlet related class.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

While saving, how to get the submitted values in my save action method?

In apply request values phase, the submitted values assigned and and can be accessed with the following method input.getSubmittedValue() where input is an input based UIComponent. The submitted value will be validated and converted during the validations phase and if this happens successfully then the value of the UIComponent will be set appropriately. You should be able to find the UIComponent you are looking from FacesContext.

Is it possible to get each of these beans with their current values from Faces Context?

I am really not sure what your aim is, but if all you want is just the other managed beans then you can get their values from the HttpSession which is available from the ExternalContext.

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("nameOfManagedBean");
maple_shaft
  • 10,435
  • 6
  • 46
  • 74