Monday, August 23, 2010

Serving resource using JSR 286

In JSR 286, one more method is getting introduced in portlet life cycle that is serveResource, the advantage of this method is, we can get resource in portlet response which is not possible in JSR 168.

Let's take an example for better understanding the use of serveResource method. Suppose, you have a collection of images and you need to display different images according to the selection. What you will do is?
First, you need to create a resource URL as mentioned below,

ResourceURL resourceURL = renderResponse.createResourceURL();

Now, you need to make a hiperlink through which you call the serveResource method. The code for reference is,
<a href="<%=resourceURL.toString() %>">Resource</a>

When you click on this link, it makes an Ajax call using resourceURL and call the serveResource method. Once it is called you can set the response for images (which is not effected in JSR 168) and write this byte format of image in XML format. Now response is ready which is called using Ajax callback methods. The reference code lines are,

public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {   
 response.setContentType("image/jpeg");
 // Image in byte stream
 byte[] b = getImage(); //Returns image bytes
 // writing image byte stream on portlet response
 response.getPortletOutputStream().write(b);
}

Also, you can use different types of resources. What you need to do is?
you have to add the following code with resourceURL,
resourceURL.setResourceID("image");

Through this ID you can perform your logic according to the image resource ID. You can get Id in serveResource method using code below,

request.getResourceID().

Sunday, August 22, 2010

JSF Page navigation using RAD 7 Add Rule feature

JSF provides page navigation using navigation-rule of faces-config.xml file. In this rule, you need to mention source JSP, destination JSP file and a token which returns from managed-bean method.
I will do the same thing using ‘Add Rule’ feature of RAD 7. Let’s see how we can do it?

First you need to open the JSP page in design view and single click on ‘SAVE’ button. Thereafter, you need to select ‘Properties’ tab (Lower middle portion of RAD) and then select ‘hx:commandExButton’ option. There you can see Add Rule button.

Please click on ‘Add Rule’ button (Right upper portion of properties tab). You will get ‘Add Navigational Rule’ window. Here you will mention final JSP page, the outcome name and action if required. Press OK button once you fill all the required information.






You can see the added code in faces-config.xml file as,

<navigation-rule>
 <from-view-id>/UserInformationView.jsp</from-view-id>
  <navigation-case>
   <from-outcome>success</from-outcome>
   <to-view-id>/UserInfoDetails.jsp</to-view-id>
  </navigation-case>
</navigation-rule>

JSF page design using drag and drop feature

JSF is a standard specification for building user interfaces. In this specification, drag and drop feature is also useful. In this feature, you can use existing beans for designing the web page. Through drag and drop feature, we can design our page very fast. For designing, we will use ‘page data’ option of RAD which will available in ‘web’ perspective.
First, you need to create a bean in which you can use two fields username and address. Codes for bean are,

public class UserInfo {
    private String userName;
    private String address;
    public String getAddress() { return address;     }
    public void setAddress(String address) { this.address = address; }
    public String getUserName() { return userName; }
    public void setUserName(String userName) { this.userName = userName;     }
}

Now, you will need to use Page Data (lower left portion of the RAD) option of RAD in ‘web’ perspective. You would need to create a new ‘Page Bean’ using existing bean named ‘UserInfo’. You can create new ‘Page Bean’ once you right click on ‘Page Bean’ option and come into New>Java Bean option and click on ‘Java Bean’ option. You can see the picture below,

You can see ‘Add JavaBean’ window where you can provide a new name (name starts with first lower case character) and add existing java-bean class. Also, if you want to make this bean class as managed bean, you can check the checkbox and select the scope of bean and press finish button.






Now, you need to drag the ‘userInfo’ Page Bean and drop it into cursor location of JSP page.













You will see ‘Insert JavaBean’ wizard in picture below. Here you will see java bean entries as input field of JSP page.









You can add submit button also using ‘options’ button. When you click on options button, you will get following window. Provide label name and press OK button.













You can see additional JSP code in picture below,













The preview of JSP page as,








In this way, we can implement drag and drop feature using java-bean classes. Apart from this, JSF provides lots of components in palette which you can drag and drop it in JSP file for designing your page vary efficiently. For each page, JSF automatically creates java-code which you can find in pagecode package.

JSF portlet application directory structure and required JAR files

You can see directory structure of basic JSF portlet application in picture below,
















In this diagram, you can see ‘src’ folder which contains all the source code and WebContent folder which contains the web pages and WEB-INF directory. In WEB-INF directory, we do have three required files these are,

1) faces-config.xml file
2) portlet.xml file
3) web.xml file

and we do have four required JAR files. These are,

1) icu4j_3_4_1.jar
2) jsf-ibm.jar
3) jsf-impl-messages.jar
4) jsf-portletbridge.jar

JSF portlet development using RAD 7.0

IBM RAD 7.0 provides integrated feature for developing JSF based portlets.
Through RAD, you can develop basic JSF portlet.
Let’s see, how can we develop JSF portlet using RAD 7.0? First you need to open the RAD 7.0 and come into File>New>Project option. You can see the picture below.

 While creating new project, please select portlet project option and press Next button.
 You will get ‘New Portlet Project’ wizard where you need to mention project name, portal server version, portlet API and portlet type. Press Next once you fill all the required information.

 Now you need to add portlet modes and press finish button and you will get a basic JSF portlet application with required JAR file and configuration files.