malejpavouk
malejpavouk

Reputation: 4445

JSF2 Locale handler

I am upgrading my app to JSF 2 from JSF 1.2.

I have ran into the following issue. I had locale handler which handled the locale used by examing the URL of the request.

@Configurable
public class MutationViewHandler extends FaceletViewHandler {

@Autowired
private LanguageMutationServiceIface mutationService;

public MutationViewHandler(ViewHandler parent) {
    super(parent);
}

@Override
public Locale calculateLocale(FacesContext context) {
    String mutation = FacesUtil.getRequestParameter("mutation");
    if (mutation == null) {
        return new Locale(mutationService.getDefaultLanguageMutation().getName());
    } else {
        return new Locale(mutation);
    }
}
}

But in JSF 2 is this class deprecated and causes error (NPE) when used in MyFaces. I have tried to implement it using simple ViewHandler, but it forces me to implement many methods where I wish to use the default behaviour.

Thanks for help in advance.

Upvotes: 1

Views: 717

Answers (1)

BalusC
BalusC

Reputation: 1109875

You need to extend ViewHandlerWrapper instead.

Provides a simple implementation of ViewHandler that can be subclassed by developers wishing to provide specialized behavior to an existing ViewHandler instance. The default implementation of all methods is to call through to the wrapped ViewHandler.

Usage: extend this class and override getWrapped() to return the instance we are wrapping.

So, you can just do

public class MyViewHandler extends ViewHandlerWrapper {

    private ViewHandler wrapped;

    public MyViewHandler(ViewHandler wrapped) {
        this.wrapped = wrapped;
    }
    
    @Override
    public ViewHandler getWrapped() {
        return wrapped;
    }

    @Override
    public Locale calculateLocale(FacesContext context) {
        // Do your thing here.
    }

}

Upvotes: 2

Related Questions