Disable components in a large project

你。 提交于 2019-12-23 19:36:23

问题


with a lot of developers and plenty of juniors I want to disable certain components such as <p:spacer> to prohibit using components for html/css issues. I want to limit the available components for libraries like omnifaces / primefaces / richfaces to a whitelist / blacklist thing basically.

Would this be a reasonable feature request for a library like omnifaces or is it to hard to build / to localized?


回答1:


Basically, you can achieve this by providing a custom Application implementation (based on ApplicationWrapper) wherein you override the desired createComponent() method and throw e.g. IllegalArgumentException when a blacklisted component type and/or renderer type is passed.

Here's a kickoff example:

public class YourApplication extends ApplicationWrapper {

    private static final Set<String> BLACKLISTED_COMPONENT_TYPES = unmodifiableSet(new HashSet<>(asList(
        "org.primefaces.component.Spacer",
        "com.example.SomeComponentType",
        "com.example.OtherComponentType"
        // Etc..
    )));

    private final Application wrapped;

    public YourApplication(Application wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public UIComponent createComponent(FacesContext context, String componentType, String rendererType) {
        if (BLACKLISTED_COMPONENT_TYPES.contains(componentType)) {
            throw new IllegalArgumentException("You are not allowed to use this component.");
        }

        return super.createComponent(context, componentType, rendererType);
    }

    @Override
    public Application getWrapped() {
        return wrapped;
    }

}

You can get it to run with this factory:

public class YourApplicationFactory extends ApplicationFactory {

    private final ApplicationFactory wrapped;

    public YourApplicationFactory(ApplicationFactory wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public Application getApplication() {
        return new YourApplication(wrapped.getApplication());
    }

    @Override
    public void setApplication(Application application) {
        wrapped.setApplication(application);
    }

}

Which is registered in faces-config.xml as below:

<factory>
    <application-factory>com.example.YourApplicationFactory</application-factory>
</factory>



回答2:


You can use tag file feature of jsf. You will declare tag file for each component that you want to use. After that, your team will only use these tag file in your project.



来源:https://stackoverflow.com/questions/28560957/disable-components-in-a-large-project

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