vrijdag 23 januari 2009

JSF controllers and bean dependencies

I'm refreshing my JSF knowledge since I've been working with spring mvc instead for a while. In fact a lot is similar to spring or can be done using a similar approach.

JavaBean: Interconnection between front- and backend are the javabeans. These you should know, having a no args ctor, properties with accessors (getters and setters), binding of these properties in frontend, ...

public class JavaBean {

private String myProperty;

public JavaBean() {};

public void setMyProperty(String property) {
this.myProperty = property;
}

public String getMyProperty() {
return this.myProperty;
}
}


Using javabeans as controllers: In JSF javabeans can be configured using the faces-config.xml (or any custom named xml configuration file when it's pointed correctly). The properties can be easily set by providing setters and getters in the javabean class and databinding in the JSF page. You can use these managed beans as controllers by just creating some extra methods and the commandButton or commandLink jsf-component bound to these methods.

<!-- ... -->

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
template="/WEB-INF/template/default.xhtml">

<!-- ... -->

<!-- example of property binding with a checkbox -->
<h:selectBooleanCheckbox title="#{file.selected}"
value="#{file.selected}">

<!-- example of method binding with commandButton -->
<h:commandButton id="refresh" action="#{StatsController.refresh}"
value="#{bundle.refresh}"></h:commandButton>

<!-- ... -->


Bean dependencies: Service and other dependencies between beans can also be configured in this xml. Therefore create managed beans with no scope and then reference these in the actual javabean.
<!-- an example of a service -->
<managed-bean>
<description>fileservice</description>
<managed-bean-name>FileService</managed-bean-name>
<managed-bean-class>
group.project.services.FileServiceImpl
</managed-bean-class>
<managed-bean-scope>none</managed-bean-scope>
</managed-bean>

<!-- and this service being referenced in another bean -->
<managed-bean>
<description>
The controller for the list of files, select and process
</description>
<managed-bean-name>FilesController</managed-bean-name>
<managed-bean-class>
group.project.web.FilesController
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>service</property-name>
<value>#{FileService}</value>
</managed-property>
</managed-bean>

Geen opmerkingen:

Een reactie plaatsen