We’re almost done; we have just a few more changes to make. In this next section we’ll complete our backing bean so it can call the new Spring services that Skyway generated for us. Once we’ve done this we’ll need to tweak our faces configuration file to use Spring’s JSF variable resolver. And then we’ll be ready to test it all out, so let’s get going.
Refactoring the CustomerBean.java to use the new Spring services.
First I declared three private variables and I let Eclipse generate the appropriate imports plus getters and setters for me. The “customerService” is an interface to the service that Skyway created. The right instance of this service will be injected at run time. The “customer” variable will be used for persisting data. The “customers” variable will be used to hold a list of customers.
private CustomerService customerService;
private Customer customer;
private Set<Customer> customers;
Next I implemented the add and getCustomers methods. For now we’ll just look at the add() method, but the getCustomers is very similar. You’ll note that inside the try block, we call the spring service’s operation to save a customer. Yes, it’s really that easy to call our Skyway / Spring service!
public String add(ActionEvent event){
customer = new Customer();
customer.setId(id);
customer.setName(name);
try {
customerService.saveCustomerOperation(customer);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
That’s it for changes to the CustomerBean.java managed bean. That was really pretty simple. Next we need to add a two entries to our faces configuration file.
Editing the faces-config.xml
Variable Resolver
To enable JSF to be able to resolve variables from Spring’s application context we’ll need to add an entry to the faces-config.xml file. Edit the faces-config and near the top we’ll add the <application> parameter. Inside this parameter, we’ll add the variable-resolver entry as shown below to this portion of the faces-config file. By adding this entry, jsf will first look for any variables in the JSF context and then search the Spring context.
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
Injecting the Spring service into our jsf Backing Bean – CustomerBean.java
In the faces-config you’ll notice that the customerBean managed bean is defined. We want to inject our Spring bean into this jsf managed bean by adding a managed property value to this spring entry.
<managed-bean>
<managed-bean-name>
customerBean</managed-bean-name>
<managed-bean-class>
com.myJSF.web.CustomerBean</managed-bean-class>
<managed-bean-scope>
session</managed-bean-scope>
<managed-property>
<property-name>customerService</property-name>
<value>#{CustomerService}</value>
</managed-property>
</managed-bean>
Note that the property-name entry, customerService, matches the variable name and setter method name of the property we defined in customerBean.java. The value #{CustomerService}matches the Spring bean id defined in the mySkywayJSF-generated-service-context.xml file. If you open this file you’ll see the entry for this service.
And THAT’S IT, We’re Done!
The application is now ready to deploy and run locally. You can access the project through this URL: http://localhost:8080/myJSF/faces/customers.jsp.
Summary and Appendix
You should now have a good idea of how to easy it is to integrate Skyway with a JSF application. To summarize the high level steps we followed were:
1. We took our existing JSF application and ensured we had a working front end.
2. Next we created a skyway modeling project and set the generated code targets to reference our existing JSF project.
3. We then created a data type for our Customer and then let Skyway’s scaffolding generate the customer CRUD operations for us.
4. Next we refactored the jsf backing bean’s code to use the new Spring customer operations.
5. We finally edited the JSF project’s faces-config to:
a. Use Spring’s DelegatingVariableResolver.
b. Inject our Spring customer service into the JSF backing bean.
As you can see it’s really pretty simple once you understand the places you need to touch.
If you are interested in the completed version of this project it can be downloaded here:Completed Version.
Part 1 Introduction and setup
Part 2 Create the JSF – ICEfaces project
Part 3 Create the Skyway modeling project and generate our services
Part 4 Tying it all together – integrating Skyway generated Spring services with JSF.