Put a “Cool Face” on your Skyway 6.1 Apps- Part 3: Creating Skyway Modeling Project and Generating Spring Services
1: Intro | 2: JSF Setup | 3: SKyway Scaffolding | 4: Skyway & JSF
1: Intro | 2: JSF Setup | 3: SKyway Scaffolding | 4: Skyway & JSF
In this next part of the blog I’ll discuss the steps I took to set up a simple JSF project. Since this blog is not intended to be primer on JSF, I’ll just touch very briefly on the steps I took to set up the base JSF project. And if you’re interested you can download the completed version of the JSF portion of the project from here BaseJSF Project.
1. I started out by creating a new ICEfaces project. The readme file included with the ICEfaces plug-in, covers this in pretty good detail.
2. I then created a managed bean called CustomerBean.java. This will serve as the backing bean for the view layer / jsp pages.
3. Next I created a jsp page called customers.jsp. I used ICEfaces jsp template for this page.
4. On this jsp page I added an ICEfaces tab control; one tab for adding customers and one tab for listing customers. This will add just a touch of splash to this really basic web app.
5. All that was left to do then was to add entries to the faces-config.xml file to:
a. register the CustomerBean.java as a managed bean
b. configure the page flow for the customers.jsp page
6. I then tested the running app to see that when I tried to add a customer that it logged the message in the server console.
Note: In case you’re wondering why we are not persisting the data yet, remember that we’re going to use our Spring services tier generated for us by Skyway to handle this work. Read the rest of this entry »
I decided to try integrating Skyway with a JSF framework called ICEfaces. ICEfaces is one of the more popular JSF frameworks and it offers some very nice RIA capabilities. ICEfaces is a JSF extension framework that offers Ajax with a rich set of components to make it very easy to build RIA based UIs.
If you’ve not used JSF, it is primarily intended as a presentation layer framework for simplifying JEE development. So I decided I would use JSF for developing the UI and front controller and Skyway for defining the Spring services and persistence tier of the application.
In case you’re not familiar with Skyway, Skyway Visual Perspectives is an easy to use Spring Generation Framework that can be used to quickly and easily build out an entire Spring application. Skyway can also be used to allow developers to choose what parts of the application they design and what parts Skyway will create. It’s this flexibility we’ll tap into to show how easy this type of integration can be done.
Also new to 6.1 we now have a lot more control over how and where code is generated. When you create a project you have options for controlling things like the target projects and package names for your generated code. We can now direct our Skyway generated Spring app into one project or divide it up into many projects. One of those targets may also be an existing project. In this blog we’ll direct our generated code to go into an existing JSF-ICEfaces Project.
The remaining portions of this blog will discuss the steps I followed to integrate Skyway with JSF.
Note that while I’ll be talking about integrating Skyway and ICEfaces, this same basic recipe applies for integrating just about any JSF framework with Spring.
Since a picture is worth a thousand words, I thought I’d include a few screen shots to save me some typing and to give you the concept of what the project I built actually does.
As you can see this is a real simple web application. It allows you to add, update and view customers. Before I get started, there are some setup steps I’ll point out so you can follow along if you like. I’m using Eclipse 3.3.2 with the Skyway Visual Perspectives Community Edition plug-in and ICEfaces Eclipse plug-in installed.
At the time I’m writing this, Skyway 6.1 is not available for download yet, but it should be in the very near future. Once Skyway 6.1 is available, you can download either the plug-in or RCP version of Skyway. You’ll find everything you need at the Skyway Community site in the Getting Started area.
I also download the ICEfaces Eclipse Plug-in from their community site here. Be sure that you download the ICEfaces Project integration for Eclipse 3.3.
Once both plug-ins were installed, I set up a connection to a local MySQL database and to a Tomcat server. That was it and now we’re ready to go.
Part 1 Introduction and setup
Part 2 Creating 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
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.
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.
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>
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.
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.
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.