禁用大型项目​​中的组件

Disable components in a large project

有很多开发人员和很多初级人员,我想禁用某些组件,例如 <p:spacer> 以禁止使用组件解决 html/css 问题。我想基本上将 omnifaces / primefaces / richfaces 等库的可用组件限制为白名单/黑名单。

对于像 omnifaces 这样的库来说,这是一个合理的功能请求吗?还是很难构建/本地化?

您可以使用jsf的标签文件功能。您将为要使用的每个组件声明标记文件。之后,您的团队将只在您的项目中使用这些标签文件。

基本上,您可以通过提供自定义 Application implementation (based on ApplicationWrapper) wherein you override the desired createComponent() method 并抛出例如IllegalArgumentException 当传递了列入黑名单的组件类型 and/or 渲染器类型时。

这是一个启动示例:

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;
    }

}

您可以通过这家工厂将其送到 运行:

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);
    }

}

faces-config.xml中注册如下:

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