gpt4 book ai didi

jsf - 静态服务 JSF 组件框架 Javascript 和 CSS

转载 作者:行者123 更新时间:2023-12-04 06:25:08 25 4
gpt4 key购买 nike

我正在使用 RichFaces 3.3.1,我想弄清楚是否有办法将 RichFaces CSS 和 Javascript 作为静态资源从另一个 Web 服务器(如 Apache 或 Nginx)托管。

我试过设置 org.ajax4jsf.RESOURCE_URI_PREFIX web.xml 中的初始化参数到另一个 Web 服务器,但 URI 仍然是相对于 Web 应用程序的。

我还尝试从 RichFaces jar 中提取两个 Javascript 文件

  • framework.pack.js
  • ui.pack.js

  • 将以下内容添加到 web.xml。
    <context-param>
    <param-name>org.richfaces.LoadScriptStrategy</param-name>
    <param-value>NONE</param-value>
    </context-param>

    然后在 xhtml 中包含来自其他服务器的 Javascript 文件。不幸的是,在我这样做之后,许多 RichFaces 组件都无法工作。

    任何其他想法如何做到这一点?
    有没有人使用 JSF 组件框架成功地做到这一点?

    最佳答案

    我意识到这不是 3.3 版的答案,但是如果您可以升级到 Richfaces 4 final,那么他们就有这个漂亮的小功能,您可以将其添加到 web.xml:

    <context-param>
    <param-name>org.richfaces.staticResourceLocation</param-name>
    <param-value>#{resourceLocation}</param-value>
    </context-param>

    然后在 META-INF 中,您可以包含一个静态资源映射文件。完整路径/文件名是:

    META-INF/richfaces/static-resource-mappings.properties

    其中的内容是这样的:
    jquery.js=https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
    richfaces.js=https://mystaticimageserver.com/img/richfaces.js

    编辑:

    我有时做的另一件事(在 JSF 2 中)只是抑制库提供者包含的资源版本。然后我只是创建一个静态版本,并将它自己包含在我的 facelets 模板中或在您的项目中有意义的任何地方。它不太优雅,但如果您为了性能或类似目的而组合资源,则效果很好。

    为此,我首先通过将其添加到 faces-config.xml 来为脚本和样式表定义一些客户渲染器:

    面孔-config.xml
    <render-kit>
    <renderer>
    <component-family>javax.faces.Output</component-family>
    <renderer-type>javax.faces.resource.Script</renderer-type>
    <renderer-class>com.davemaple.jsf.resource.ScriptRenderer</renderer-class>
    </renderer>
    <renderer>
    <component-family>javax.faces.Output</component-family>
    <renderer-type>javax.faces.resource.Stylesheet</renderer-type>
    <renderer-class>com.davemaple.jsf.resource.StylesheetRenderer</renderer-class>
    </renderer>
    </render-kit>

    然后我向 web.xml 添加一些我想抑制的文件或类:

    web.xml
    <context-param>
    <param-name>com.davemaple.jsf.resource.suppressedScriptResourceClasses</param-name>
    <param-value></param-value>
    </context-param>

    <context-param>
    <param-name>com.davemaple.jsf.resource.suppressedScripts</param-name>
    <param-value>javax.faces:jsf.js :jquery.js :richfaces.js :richfaces-event.js :richfaces-base-component.js org.richfaces:message.js org.richfaces:richfaces-csv.js</param-value>
    </context-param>

    <context-param>
    <param-name>com.davemaple.jsf.resource.suppressedStylesheetResourceClasses</param-name>
    <param-value></param-value>
    </context-param>

    <context-param>
    <param-name>com.davemaple.jsf.resource.suppressedStylesheets</param-name>
    <param-value>org.richfaces:msg.ecss</param-value>
    </context-param>

    ScriptRenderer.java
    /**
    * A Customer ScriptRenderer that allows specific resources to be
    * suppressed and managed manually (CDN, Static Resource Server, Combining resources)
    *
    */
    public class ScriptRenderer extends ScriptStyleBaseRenderer {

    private static final String DELIMITER = " ";
    private static final String LIBRARY_DELIMITER = ":";

    private static final String SUPRESSED_CLASSES_INIT_PARAM = "com.davemaple.jsf.resource.suppressedScriptResourceClasses";
    private final List<Class<?>> supressedClasses = new ArrayList<Class<?>>();

    private static final String SUPRESSED_SCRIPTS_INIT_PARAM = "com.davemaple.jsf.resource.suppressedScripts";
    private final List<String> suppressedScriptNames = new ArrayList<String>();

    /**
    * Constructor
    */
    public ScriptRenderer() {
    super();

    String suppressedClasses =
    FacesContext.getCurrentInstance().getExternalContext().getInitParameter(SUPRESSED_CLASSES_INIT_PARAM);

    if (suppressedClasses != null
    && !suppressedClasses.isEmpty()) {
    for (String suppressedClassString : suppressedClasses.split(DELIMITER)) {
    try {
    this.supressedClasses.add(Class.forName(suppressedClassString));
    } catch (ClassNotFoundException ex) {
    }
    }
    }

    String suppressedScripts =
    FacesContext.getCurrentInstance().getExternalContext().getInitParameter(SUPRESSED_SCRIPTS_INIT_PARAM);

    if (suppressedScripts != null
    && !suppressedScripts.isEmpty()) {
    for (String suppressedScript : suppressedScripts.split(DELIMITER)) {
    this.suppressedScriptNames.add(suppressedScript);
    }
    }
    }

    /**
    * Returns a boolean indicating if the component should
    * be encoded and thus rendered
    *
    * @param component
    * @return isSuppressed
    */
    protected boolean isSuppressed(UIComponent component) {

    if (this.supressedClasses.contains(component.getClass())) {
    return true;
    }

    if (component.getAttributes().containsKey("name")
    && component.getAttributes().containsKey("library")) {

    String key = component.getAttributes().get("library") + LIBRARY_DELIMITER + component.getAttributes().get("name");

    if (this.suppressedScriptNames.contains(key)) {
    return true;
    }
    }

    if (component.getAttributes().containsKey("name")
    && !component.getAttributes().containsKey("library")) {

    String key = LIBRARY_DELIMITER + component.getAttributes().get("name");

    if (this.suppressedScriptNames.contains(key)) {
    return true;
    }
    }

    return false;
    }

    @Override
    protected void startElement(ResponseWriter writer, UIComponent component) throws IOException {
    writer.startElement("script", component);
    writer.writeAttribute("type", "text/javascript", "type");
    }

    @Override
    protected void endElement(ResponseWriter writer) throws IOException {
    writer.endElement("script");
    }

    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {

    if (!this.isSuppressed(component)) {
    super.encodeBegin(context, component);
    }
    }


    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {

    if (!this.isSuppressed(component)) {

    Map<String, Object> attributes = component.getAttributes();
    Map<Object, Object> contextMap = context.getAttributes();

    String name = (String) attributes.get("name");
    String library = (String) attributes.get("library");

    String key = name + library;

    if (null == name) {
    return;
    }

    // Ensure this script is not rendered more than once per request
    if (contextMap.containsKey(key)) {
    return;
    }
    contextMap.put(key, Boolean.TRUE);

    // Special case of scripts that have query strings
    // These scripts actually use their query strings internally, not
    // externally
    // so we don't need the resource to know about them
    int queryPos = name.indexOf("?");
    String query = null;
    if (queryPos > -1 && name.length() > queryPos) {
    query = name.substring(queryPos + 1);
    name = name.substring(0, queryPos);
    }

    Resource resource = context.getApplication().getResourceHandler()
    .createResource(name, library);

    ResponseWriter writer = context.getResponseWriter();
    this.startElement(writer, component);

    String resourceSrc;
    if (resource == null) {
    resourceSrc = "RES_NOT_FOUND";
    } else {
    resourceSrc = resource.getRequestPath();
    if (query != null) {
    resourceSrc = resourceSrc
    + ((resourceSrc.indexOf("?") > -1) ? "+" : "?") + query;
    }
    }

    writer.writeURIAttribute("src", resourceSrc, "src");
    this.endElement(writer);
    super.encodeEnd(context, component);

    }
    }

    }

    ScriptRenderer.java
    /**
    * A Customer StylesheetRenderer that allows specific resources to be
    * suppressed and managed manually (CDN, Static Resource Server, Combining resources)
    *
    */
    public class StylesheetRenderer extends ScriptStyleBaseRenderer {

    private static final String DELIMITER = " ";
    private static final String LIBRARY_DELIMITER = ":";

    private static final String SUPRESSED_CLASSES_INIT_PARAM = "com.davemaple.jsf.resource.suppressedStylesheetResourceClasses";
    private final List<Class<?>> supressedClasses = new ArrayList<Class<?>>();

    private static final String SUPPRESSED_STYLESHEETS_INIT_PARAM = "com.davemaple.jsf.resource.suppressedStylesheets";
    private final List<String> suppressedStylesheets = new ArrayList<String>();

    /**
    * Constructor
    */
    public StylesheetRenderer() {
    super();

    String suppressedClasses =
    FacesContext.getCurrentInstance().getExternalContext().getInitParameter(SUPRESSED_CLASSES_INIT_PARAM);

    if (suppressedClasses != null
    && !suppressedClasses.isEmpty()) {
    for (String suppressedClassString : suppressedClasses.split(DELIMITER)) {
    try {
    this.supressedClasses.add(Class.forName(suppressedClassString));
    } catch (ClassNotFoundException ex) {
    }
    }
    }

    String suppressedStylesheets =
    FacesContext.getCurrentInstance().getExternalContext().getInitParameter(SUPPRESSED_STYLESHEETS_INIT_PARAM);

    if (suppressedStylesheets != null
    && !suppressedStylesheets.isEmpty()) {
    for (String suppressedStylesheet : suppressedStylesheets.split(DELIMITER)) {
    this.suppressedStylesheets.add(suppressedStylesheet);
    }
    }
    }

    /**
    * Returns a boolean indicating if the component should
    * be encoded and thus rendered
    *
    * @param component
    * @return isSuppressed
    */
    protected boolean isSuppressed(UIComponent component) {

    if (this.supressedClasses.contains(component.getClass())) {
    return true;
    }

    if (component.getAttributes().containsKey("name")
    && component.getAttributes().containsKey("library")) {

    String key = component.getAttributes().get("library") + LIBRARY_DELIMITER + component.getAttributes().get("name");

    if (this.suppressedStylesheets.contains(key)) {
    return true;
    }
    }

    if (component.getAttributes().containsKey("name")
    && !component.getAttributes().containsKey("library")) {

    String key = LIBRARY_DELIMITER + component.getAttributes().get("name");

    if (this.suppressedStylesheets.contains(key)) {
    return true;
    }
    }

    return false;
    }

    @Override
    protected void startElement(ResponseWriter writer, UIComponent component)
    throws IOException {
    writer.startElement("style", component);
    writer.writeAttribute("type", "text/css", "type");
    }

    @Override
    protected void endElement(ResponseWriter writer) throws IOException {
    writer.endElement("style");
    }

    @Override
    protected String verifyTarget(String toVerify) {
    return "head";
    }

    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {

    if (!this.isSuppressed(component)) {
    super.encodeBegin(context, component);
    }
    }


    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {

    if (!this.isSuppressed(component)) {
    Map<String, Object> attributes = component.getAttributes();
    Map<Object, Object> contextMap = context.getAttributes();

    String name = (String) attributes.get("name");
    String library = (String) attributes.get("library");
    String key = name + library;

    if (null == name) {
    return;
    }

    // Ensure this stylesheet is not rendered more than once per request
    if (contextMap.containsKey(key)) {
    return;
    }
    contextMap.put(key, Boolean.TRUE);

    Resource resource = context.getApplication().getResourceHandler()
    .createResource(name, library);

    ResponseWriter writer = context.getResponseWriter();
    writer.startElement("link", component);
    writer.writeAttribute("type", "text/css", "type");
    writer.writeAttribute("rel", "stylesheet", "rel");
    writer.writeURIAttribute("href",
    ((resource != null) ? resource.getRequestPath()
    : "RES_NOT_FOUND"), "href");
    writer.endElement("link");
    super.encodeEnd(context, component);
    }
    }

    }

    关于jsf - 静态服务 JSF 组件框架 Javascript 和 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6126279/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com