Possible to have an ApplicationScoped bean that skins a JSF 2 application with a richfaces skin?

梦想与她 提交于 2019-12-06 13:00:57

问题


In production want the user to be able to write a properties file and upload that file to our production server. Once this is in place it will contain the properties needed for a richfaces skin. This file can be named whatever.

In development I want the properties file to be read from inside WEB-INF/myprop.properties where all my other properties files are. This file can be named whatever.

So far I have done this:

@ManagedBean(name="myCustomSkin ")
@ApplicationScoped
public class MyCustomSkin extends SkinFactoryImpl {
    /*
     * In here I call 
     * Skin s = this.buildSkin(context, "skin"); in my constructor
     * I am also overriding the loadProperties() method so it load my properties just fine
     * For some reason I can't get my app to actually use the properties I have loaded
     */
}

Any ideas? Basically I want to dynamically skin my richfaces application via an ApplicationScoped ManagedBean that gets initialized on startup of my Tomcat 7 server. Ideally the name of the skin file would be dynamic user input possibly read from the database or a different properties file.

EDIT 1: I have gotten it to load the properties file and then I manually (through java code) tried to insert a context-param by using servletContext.setInitParameter("org.richfaces.skin", this.skin); where this.skin is a String variable which I get the value for like this: this.skin = s.getName(); and if you see above s is just the Skin object I get back from this.buildSkin. This comes to a SkinNotFound Exception because I am assuming my skin isn't getting place in my class path or something.

EDIT 2: Using JSF 2.1.17, Richfaces 4.3.0, and Tomcat7

EDIT 3: Is there a way to tell richfaces to look in a different directory for the myskin.skin.properties file?

Thanks


回答1:


To implement something like this RichFaces source code is your best friend.

Here is what I have found how you can do what you want:

Add a file to a jar (or anywhere so it will appear in classpath) META-INF/services/org.richfaces.application.Module Content of the file should be com.example.CustomModule

Implementation of the custom module can be like this:

public class CustomModule implements Module {
    public void configure(ServicesFactory factory) {
        factory.setInstance(SkinFactory.class, new CustomSkinFactoryImpl());
    }
}

And then implement SkinFactory according to your needs, for example (if you want to extend default behavior with your CustomSkin):

public class CustomSkinFactoryImpl extends SkinFactoryImpl {
    public Skin getSkin(FacesContext context) {
        return new CompositeSkinImpl(new CustomSkin(), super.getSkin(context));
    }
}


来源:https://stackoverflow.com/questions/15098394/possible-to-have-an-applicationscoped-bean-that-skins-a-jsf-2-application-with-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!