Implementing and configuring the managed bean

In this example, the JSF application needs a managed bean to accomplish the following tasks:
  • Loading the IVL file when the Web application starts
  • Creating a manager view in order to display the graphic view.

Implementing the managed bean

To create the Java implementation file:
  1. In the Package Explorer view, right-click the src folder and select New>Class.
    Eclipse
New Java class window showing source folder path.
  2. Enter SupportBean in the Name field and click Finish.
  3. Open the newly created file SupportBean.java located in the src folder and implement the SupportBean class as follows:
    import ilog.views.IlvManager;
    import ilog.views.IlvManagerView;
    import java.net.URL;
    import javax.faces.context.FacesContext;
    
    public class SupportBean {
    
    	private IlvManagerView managerView;
    
    	public SupportBean() {
    		initManagerView();
    	}
    
    	/**
    	 * Initializes the manager view with the current ivl file.
    	 */
    	protected void initManagerView() {
    		if (managerView == null) 
    			managerView = new IlvManagerView();
    		URL ivlURL;
    		try {
    			ivlURL = FacesContext.getCurrentInstance().getExternalContext().getResource("/java2d.ivl");      
    			IlvManager manager = managerView.getManager();
    			manager.setFileName(ivlURL);
    			managerView.fitTransformerToContent();
    			managerView.setKeepingAspectRatio(true);  
    		} catch (Exception e) {
    			e.printStackTrace();
    		}    
    	}
    
    	/**
    	 * Returns the manager view.
    	 * @return The manager view.
    	 */
    	public IlvManagerView getView() {
    		return managerView;
    	}
    }
    
The SupportBean class loads the file java2d.ivl located in the root folder of the Web application and creates a manager view for this file.

Configuring the managed bean

You must now declare this class as a managed bean so that it can be accessed by JViews JSF components.
To declare the class as a managed bean:
  • Open the file faces-config.xml located in the WebContent/WEB_INF folder and add the code shown in bold in the following code example:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <faces-config
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
        version="1.2">
        
    <managed-bean>
        <managed-bean-name>support</managed-bean-name>
        <managed-bean-class>SupportBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
      </managed-bean>
    
    </faces-config>
    
You have now implemented and configured the managed bean.