JSFDays 2009

I was there as a speaker at JSFDays 2008 and it was just a great conference. This year JSFDays 2009 is in Austria again and many well known developers will be there as a speaker.

JSF spec lead Ed Burns, Jurgen Hoeller, my main man Matthias Wessendorf, Seam lead Pete Muir, Max Katz, Martin Marinschek, Kito Mann and many more great developers will make JSFDays09 a great conference. I am excited to meet and hangout with these folks again after JSFOne and JSFDays08.

Me? I have a session as well, my talk is about “Securing JSF Applications”.

You can find more info at the conference web site which is updated regularly.

http://conference.irian.at/

Posted in Java. 5 Comments »

FacesTrace 1.0.1 is released

Visual debugger of JSF is refreshed with a new release. This 1.0.1 version is a maintanence release and contains several bug fixes. Also I’ve moved the homepage of facestrace to Prime Tecnology’s web site.

In order to get the new lib you can either use Prime’s maven repository or get it directly from sourceforge.

Much more info is at the new home of facestrace which is here;

And the online demo is here;

P.S. Many thanks to Simon Kitching for his help on this release.

Posted in Java. 6 Comments »

JSF 2.0 Composite Slider

Some time ago I’ve posted an example to demonstrate how easy it is to create composite components with Facelets and JSF. The example is using scriptaculous slider widget and wraps it as a jsf component. In JSF 2.0 creating composite components is much easier thanks to the JSF 2.0 EZComp.

So here is the JSF 2.0 version of the slider component;

Page that has a slider;


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 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"
      xmlns:barca="http://java.sun.com/jsf/composite/barca">
<h:head>
    <title>Slider Demo</title>
</h:head>
<h:body>
    <h:form>
        <barca:inputSlider id="sld1" value="#{demo.number}" min="0" max="100"></barca:inputSlider>
    </h:form>
</h:body>
</html>

And inputSlider.xhtml


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:composite="http://java.sun.com/jsf/composite">
<body>

<composite:interface>
    <composite:attribute name="id" required="true" />
    <composite:attribute name="value" required="true" />
    <composite:attribute name="min" required="false" />
    <composite:attribute name="max" required="false"/>
</composite:interface>

<composite:implementation>

    <h:outputScript name="protototype.js" library="scriptaculous" target="head"/>
    <h:outputScript name="scriptaculous.js" library="scriptaculous" target="head"/>
    <h:outputScript name="effects.js" library="scriptaculous" target="head"/>
    <h:outputScript name="controls.js" library="scriptaculous" target="head"/>
    <h:outputScript name="slider.js" library="scriptaculous" target="head"/>

    <h:inputText id="sliderField" style="width:100px" value="#{compositeComponent.attrs.value}"></h:inputText>
<div id="#{compositeComponent.clientId}_track" style="width:105px;background-color:#aaa;height:5px;">
<div id="#{compositeComponent.clientId}_handle" style="width:5px;height:10px;background-color:#f00;cursor:move;"></div>
</div>
<script type="text/javascript">
    var slider_#{compositeComponent.attrs.id} = new Control.Slider('#{compositeComponent.clientId}_handle','#{compositeComponent.clientId}_track',{range:$R(#{compositeComponent.attrs.min},#{compositeComponent.attrs.max})});              

    slider_#{compositeComponent.attrs.id}.options.onSlide = function(value){
          $('#{compositeComponent.clientId}:sliderField').value = (value + '').split(".")[0];
    };
    </script>

</composite:implementation>
</body>
</html>

The output would be sth like;

You may ask so where do we write the xml to configure taglibs or any other thing. The answer is you don’t need any xml or annotation, the inputSlider.xhtml is enough! It is convention over configuration, if you put inputSlider.xml under your %WEBAPP%/resources/barca/inputSlider.xhtml you can add it the component taglib to your pages with xmlns:barca=”http://java.sun.com/jsf/composite/barca”. Also another cool thing is that assume you want to bundle your component library in a jar and distribute/reuse it in several applications. This time place your inputSlider.xhtml and other composite components in %YOUR_JAR%/META-INF/resources/barca/inputSlider.xhtml.

Compared to my facelets version of slider this one is much scalable, in JSF 2.0 inputSlider is not a tag that is added to the component tree, it is an actual component so that I can take the advantage of compositeComponent.clientId attribute to use multiple instances of slider in one page. In facelets version of slider I had to use tomahawk forceId. Also using the new h:outputScript library I can avoid including the same resource more than once if there are multiple sliders in the page. In summary EZComp keeps it’s promise and as you see really simplifies JSF component development.

Posted in Java. 4 Comments »

XML-less JSF Navigations

JSF’s declarative navigation management requires you to specify a navigation rule with navigation cases in faces-config.xml. If your application gets bigger, your faces-config.xml would get bigger as a result. Following the convention over configuration design paradigm, it is possible to avoid navigation rules in xml.

As usual we need to extend JSF and the idea is to plug-in a custom navigation handler. This navigation handler assumes the outcome is the name of the target view of the navigation.


public class LetsGetRidofXMLStuffNavigationHandler extends NavigationHandler{

    public final static String REDIRECT_PREFIX = "redirect";

    @Override
    public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) {
        if(outcome == null)
            return;            //no navigation

        ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
        String targetViewId = getTargetViewId(facesContext, outcome);

        if(isRedirect(outcome))
        {
             ExternalContext externalContext = facesContext.getExternalContext();
             String redirectPath = viewHandler.getActionURL(facesContext, targetViewId);

             try
             {
                 externalContext.redirect(externalContext.encodeActionURL(redirectPath));
             }
             catch (IOException e)
             {
                 throw new FacesException(e.getMessage(), e);
             }
        }
        else
        {
            UIViewRoot viewRoot = viewHandler.createView(facesContext, targetViewId);
            facesContext.setViewRoot(viewRoot);
            facesContext.renderResponse();
        }
    }

    private boolean isRedirect(String outcome) {
        return outcome.startsWith(REDIRECT_PREFIX);
    }

    private String getTargetViewId(FacesContext facesContext, String outcome) {
        String targetViewId;
        String viewSuffix = getDefaultViewSuffix(facesContext);

        if(isRedirect(outcome)) {
            targetViewId = "/" + outcome.split(":")[1] + viewSuffix;
        } else {
            targetViewId = "/" + outcome + viewSuffix;
        }

        return targetViewId;
    }

    private String getDefaultViewSuffix(FacesContext facesContext) {
        String suffix = facesContext.getExternalContext().getInitParameter("javax.faces.DEFAULT_SUFFIX");

        return suffix!=null ? suffix : ".jsp";
    }

}

After implementing this navigation handler, we need to plug it in so that it can take over navigation management. In faces-config;


<?xml version="1.0" encoding="utf-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

 <application>
  <navigation-handler>com.mycompany.myproject.stuff.LetsGetRidofXMLStuffNavigationHandler</navigation-handler>
 </application>

Now without defining an navigation rule in xml, in action binding methods just return the name of the page. For example, to navigate from a paged called login.jsf to mainpage.jsf, return “mainpage”.


public String login() {

//do some stuff

return "mainpage";

}

When the “mainpage” outcome reaches our navigation management, it finds the view id as /target.jsp or /target.xhtml(depending on what view technology you use) and then do a forward. For redirects add the redirect prefix so the string to be returned should be “redirect:mainpage”.

After all there is no need to define any navigation-rule in faces-config so we can get rid of this;

<navigation-rule>
    <from-view-id>/login.jsp</from-view-id>
    <navigation-case>
      <from-outcome>mainpage</from-outcome>
      <to-view-id>/mainpage.jsp</to-view-id>
    </navigation-case>
</navigation-rule>
Posted in Java. 5 Comments »

Speaking about JSF 2.0 in London

EG Group led by Ed Burns are doing a great job and I really appreciate all the hard work they put in. JSF 2.0 is on the horizon and I’ll be doing a “JSF 2.0 Preview” talk in London on the 17th of November. The talk is organized by London Java Users Group and will take place in Skills Matter.

The talk will cover the following

  • Introduction to JSF 2.0
  • New Features
    • Ajax Support
    • Easier way of Component Development
    • Resource Handling
    • New Scopes
    • PDL(Page Description Language)
    • System Events
    • Scripting Support
    • State Management
    • Component interoperability
    • Less configuration
    • And more…
  • Future of JSF

If you want to attend, free registration is required. Following is the link for it;

http://skillsmatter.com/event/java-jee/jsf-2-0

After the presentation we’ll be going to a pub for more talk and beer as usual. See you there!

jsf2poster

Moved to UK

I have moved to London from Turkey and it took quite some time to settle down so I haven’t been blogging since then. Now I am back with a couple of changes. I’ve decided to use my blog at Prime Technology for my Turkish entries and this one at wordpress for global ones:)

I am still trying to get used to life in UK, awful weather is kinda weird for a Turkish guy like me, I grew up in Antalya, coast to Mediterranean. Another thing is the English accent, I think Americans and English are two different cultures with a common language but now I am not sure if they are using the same language as well. Other than these, pubs really rock and I really enjoy the city. Cheers!