Tuesday, March 17, 2009

Create your JSF componenst with out JSP or Facelets Tags

It is personal reference. Some love XML Syntax, some try to save them from the XML Jungle. JSF is a framework that allows you to create components dynamically. The JSF Tags are only option.

If you prefer to create your compnents without JSP or Facelets, and you don't want to envent the weel again, you can define your JSF Panel Grid in your JSP or Facelet as follow:





Define the bean in Spring config file as follow:





This bean holds a instance of the JSF HtmlPanelGrid which renders the JSF Panel Grid for you. The JSF components should be added to the Panel Grid with it's public method getChildren().add(Object o);. Here is the example:


package org.mypackage;

import javax.faces.application.Application;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;


public class MyBean {

private HtmlPanelGrid panelGrid;

public MyBean(){

}
public void init(){

Application application = FacesContext.getCurrentInstance().getApplication();
setPanelGrid((HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE));

HtmlOutputText text = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);

text.setValue("Hallo JSF with out Tags.");

getPanelGrid().getChildren().add(text);

}
public void setPanelGrid(HtmlPanelGrid panelGrid) {
this.panelGrid = panelGrid;
}
public HtmlPanelGrid getPanelGrid() {
return panelGrid;
}

}

  1. In line 16 is the public method which will be ivoked by Spring after instantiation of the Bean.
  2. Line 18 gets an instance of the current application.
  3. On line 19 we create an instance of HtmlOutputText with the help of the method createComponent which takes String as argument. Every JSF Component defines public static field COMPONENT_TYPE which returns the type of the component to be created.
  4. On line 21 a instance of HtmlOutputText is created in the same way as the HtmlOutputText.
  5. In line 23 we set the value of the output text.
  6. In line 25 we set the output text component as child of the Panel Grid.
On the end of the day, when JSF try's to render the Panel Grid with the ID dynamic in the JSP or Facelets, the Property panelGrid of the backing bean will be called and our instance would be copied and rendered.

In this way, we can create whole views or parts of it, as if we program in pure Java.

Monday, March 16, 2009

Basic RichFaces Project with Spring

To create basic RichFaces enabled project, you can run the following maven command provided by JBoss.
mvn archetype:generate
-DarchetypeGroupId=org.jboss.portletbridge.archetypes
-DarchetypeArtifactId=richfaces-basic
-DarchetypeVersion=1.0.0.B6
-DgroupId=org.whatever.project
-DartifactId=myprojectname
-DarchetypeRepository=http://repository.jboss.org/maven2/
Make sure that you use the proper version of the Portlet Bridge of Jboss. Read more here
To enable Spring to manage your JSF Beans you should add this context arguments to your web.xml




org.springframework.web.context.ContextLoaderListener





org.springframework.web.context.request.RequestContextListener




This are the listeners which enables Spring to load it's context and create the beans, and to process Request from JSF to Spring and preserve the session.

Inside of faces-config.xml :



org.springframework.web.jsf.DelegatingVariableResolver




This delegates the JSF Variable resolver to look up for EL Expressions defined beans in Spring. In other words, it replaces the standard JSF EL resolver.

Now you would be able to define your JSF Beans in Spring as follows:



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">







This bean can be used with your JSF Tag as usual:





In deed simple.

Here is the full web.xml:



xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


javax.faces.STATE_SAVING_METHOD
server




javax.portlet.faces.renderPolicy
ALWAYS_DELEGATE



org.ajax4jsf.RESOURCE_URI_PREFIX
rfRes



org.richfaces.LoadStyleStrategy
NONE



org.richfaces.LoadScriptStrategy
NONE



org.ajax4jsf.COMPRESS_SCRIPT
true



org.richfaces.CONTROL_SKINNING
enable



contextConfigLocation
classpath:spring-context.xml



RichFaces Filter
richfaces
org.ajax4jsf.Filter



richfaces
Faces Servlet
REQUEST
FORWARD
INCLUDE



com.sun.faces.config.ConfigureListener



org.springframework.web.context.ContextLoaderListener



org.springframework.web.context.request.RequestContextListener





Faces Servlet
javax.faces.webapp.FacesServlet
1




Faces Servlet
/faces/*


Faces Servlet
*.jsf



FacesServlet
*.mk




10



Restrict access to JSP pages

Restrict access to JSP pages



With no roles defined, no access granted



BASIC




Sunday, March 15, 2009

Adding Spring to the project

Spring is generally highly rated for it's IoC capabilities. It gained it's popularity while developers had hard times with EJB 2.0 Specs.

Since Spring can be needed in more sub-project or modules, it is only reasonable to create a central place in your project to implement your Spring code. Create a Maven module as described in the revious post. It should look similar to this:


The Spring configuration files should be placed inside of the src/main/resources folder.

The main Spring configuration file is named applicationContext.xml by default. How ever this is not mandatory. You can name it hwat ever you like. I called it spring-context.xml.

If you are using the Sprng for web app or portlets, you can tell Spring where to find your custom configuration file with the following entry in your web.xml descriptor:


Now that you have the basic structure for your Spring, you should add the Module in to the main Project pom.xml :


and add it as dependency to be available in other projects:
Now that the Spring module is configured and available as dependency for other modules, you can bind it in other modules by adding the dependency in their pom.xml as follows:

As you can notice, the dependency in other modules pom.xml is added without the version tag. In this way, the versioning of the project dependency's is handled central in the main project pom.xml.