2013-01-18

Wicket: Favicon

<wicket:link>
<link rel="shortcut icon" type="image/x-icon" href="ico/favicon.ico"/>
</wicket:link>

Put the icon relative to the Page it is in.

com/myapp/Page.html
com/myapp/Page.java
com/myapp/ico/favicon.ico

Disadvantage: Bound to component.

<link wicket:id="favicon" rel="shortcut icon" type="image/x-icon" href="images/ico/Leaf.ico"/>
public class BaseLayoutPage extends WebPage {

    public BaseLayoutPage() {

        add( new FavIconLink("favicon", "favicon.ico") );
public class FavIconLink extends ExternalLink
{
    public FavIconLink( String id, String path ){
        super( id, path );
        add(new AttributeModifier("type", "image/x-icon"));
        add(new AttributeModifier("rel", "shortcut icon"));
        this.setContextRelative( true );
    }
}

And put the favicon.ico into .jar's root, i.e. by putting to webapp/ in Maven WAR app.

Option 3: FavIconHeaderContributor behavior

public class WicketJavaEEApplication extends WebApplication {

    @Override protected void init() {
        super.init();
        getSharedResources().add("favicon", new ContextRelativeResource("WEB-INF/classes/Leaf.ico"));
        mountResource("/favicon.ico", new SharedResourceReference("favicon") );
add( new FavIconHeaderContributor( new SharedResourceReference("favicon") ) );
public class FavIconHeaderContributor implements IHeaderContributor
{
    private ResourceReference resRef;

    public FavIconHeaderContributor(ResourceReference ref){
        resRef = ref;
    }

    public void renderHead(IHeaderResponse response) {
        CharSequence url = RequestCycle.get().urlFor(resRef, null);
        // Wicket 1.5:
        response.renderString( this.getFavIconReference(url) );
        // Wicket 1.6:
        //response.render( new StringHeaderItem( this.getFavIconReference(url) ) );

    }

    private CharSequence getFavIconReference(CharSequence url) {
        StringBuilder sb=new StringBuilder();
        sb.append("<link rel=\"shortcut icon\" href=\"").append(url).append("\" type=\"image/x-icon\">\n");
        return sb.toString();
    }
}

0