Editing service invocation beans

A service invocation bean contains the code required to invoke a service and retrieve any results.

About this task

When you bind an EJB session bean or a Web service to a JSF component, the UI for an input form with a command button is generated in the JSF page to display the results of the service invocation. A service invocation bean is also generated to contain the code required to invoke the service and retrieve any results.

Tip: Service invocation beans are contained in the Java™ package services. The name of the bean is composed of the name of the service class and the method that it invokes. For example, a service invocation bean that invokes the service method ServiceMethod on the service class ServiceClass has the fully qualified name services.ServiceClass_ServiceMethod.

As you create your application, you may need to modify the generated service invocation bean. This topic discusses two cases in which you may want to edit the service invocation bean.

To edit a service invocation bean:

Procedure

  1. Double-click the method you want to edit, to open the service file in the editor to the method.
  2. Edit the method and save the file.

Editing a service invocation bean to automatically invoke a service

About this task

Clicking the generated command button submits the form, invoking the service. By default, when a page load for the first time, or reloads by an event other than clicking the generated command button, data is not displayed in the results section of the UI.

You may want to display data on page load or reload if:
  • The service does not take parameters as input.
  • The parameters required to invoke the service are not provided by user input.
  • Events after service invocation cause the page to reload, for example, the editing of results data.

To edit a service invocation bean to automatically invoke a service:

Procedure

  1. Double-click the getter method to open the service file in the editor to the getter method.
  2. Edit the getter method so that the service is invoked automatically when the page is loaded and the result bean is null.

Example

For example, the data returned by a service invocation is stored in the service invocation bean instance in a field named resultBean.

public ToDoList getResultBean() {
	if (resultBean == null) {
		resultBean = new ToDoList();
	}
	return resultBean;
}

In order to invoke the service automatically when the page is loaded and the result bean is null, the getter method is edited as follows:

public ToDoList getResultBean() {
	if (resultBean == null) {
		doAction();
		if (resultBean == null)
			resultBean = new ToDoList();
	}
	return resultBean;
}

Editing a service invocation bean to access data returned by a service

About this task

When a service is invoked from a JSF page, by default the results are in the request scope. Since the lifecycle of the request scoped data ends when a page is rendered, the associated service invocation bean needs to be modified in order to access to the result bean post rendering.

The service invocation bean, in this case, can be edited in several ways:
  • Change the scope of the bean from request to session by editing the appropriate code in the faces-config.xml file if the service information bean is a JSF managed bean.
    Important: By default, session scope is not used since it can lead to performance problems such as excessive memory usage. Before using this solution, consider how you will manage session scoped data.
  • Edit the service invocation bean so that the results of a service invocation are placed in session scope. This solution requires you to edit two methods:
    doAction()
    Edit the method to place the result bean on the session scope.
    public String doAction() {
    	...
    	resultBean = getService().getToDoList(paramBean.getName());
    	getSessionScope().put(MY_UNIQUE_KEY, resultBean);
    	...
    }
    getResultBean()
    Edit the method to retrieve the cached results from the session scope.
    public ToDoList getResultBean() {
    	if (resultBean == null) {
    		resultBean = (ToDoList) getSessionScope().get(MY_UNIQUE_KEY);
    		if (resultBean == null)
    			resultBean = new ToDoList();
    		}
    		return resultBean;
    }
    Important: Consider how this solution will affect the memory requirements for your application.
  • Edit the service invocation bean so that the param bean is stored in session scope. The param bean holds the parameters used to invoke the service. When the result bean is requested, but is out of scope, invoke the service again. This solution requires you to edit two methods:
    getParamBean()
    Edit the method to store the param bean in session scope.
    public ParamBean getParamBean() {
    	if (paramBean == null) {
    		paramBean = (ParamBean) getSessionScope().get(PARAM_BEAN_KEY);
    		if (paramBean == null) {
    			paramBean = new ParamBean();
    		}
    	}
    	return paramBean;
    }
    doAction()
    Edit the method to place the param bean on the session scope.
    public String doAction() {
    	getParamBean();
    	getSessionScope().put(PARAM_BEAN_KEY, paramBean);  //this line has been added
    	...
    }
    Important: This solution may take a long time to invoke the service and may have unintended consequences, for example a new order may be submitted, or a new entry may be added to a database each time the service is invoked.

Feedback