【问题标题】:Richfaces Custom Component Renderer ProblemRichfaces 自定义组件渲染器问题
【发布时间】:2011-04-14 13:36:27
【问题描述】:

我已经创建了一个自定义组件,下面是组件的渲染器,在开始时组件渲染正常,但是 AjaxOuputPanel 永远不会在每次 ajax 请求时再次渲染......那么代码有什么问题

在添加 HtmlAjaxOutputPanel 类型的子组件的任何组件上也会重复这种情况,组件在启动时呈现正常,但 ajax 输出面板再也没有呈现...组件代码:

包 eg.com.etisalat.web.rich.components.inputtext;

import java.io.IOException;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlGraphicImage;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.ajax4jsf.component.UIAjaxOutputPanel;
import org.ajax4jsf.component.html.HtmlAjaxOutputPanel;
import org.ajax4jsf.renderkit.AjaxComponentRendererBase;

public class InputTextRenderer extends AjaxComponentRendererBase {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    protected Class getComponentClass() {
        return eg.com.etisalat.web.rich.components.inputtext.HtmlInputText.class;
    }

    public void doEncodeBegin(ResponseWriter writer, FacesContext context,
            UIComponent component) throws IOException {

        ELContext elContext = context.getELContext();
        Application app = context.getApplication();
        ExpressionFactory exprFactory = app.getExpressionFactory();

        String clientId = component.getClientId(context);
        String componentId = component.getId();
        component.getChildren().clear();

        String readonly = (String) component.getAttributes().get(Constants.ATT_READ_ONLY);
        String styleClass = (String) component.getAttributes().get(Constants.ATT_STYLE_CLASS);
        String required = (String) component.getAttributes().get(Constants.ATT_REQUIRED);
        String requiredMessage = (String) component.getAttributes().get(Constants.ATT_REQUIRED_MESSAGE);

        writer.startElement("div", component);
        getUtils().writeAttribute(writer, "id", clientId);

        String inputFieldId = componentId + "InputField";

        javax.faces.component.html.HtmlInputText inputTextCom = new HtmlInputText();
        inputTextCom.setId(inputFieldId);

        inputTextCom.setOnchange(inputFieldId + "StoredValue = this.value");

        if (null != readonly) inputTextCom.setReadonly(Boolean.parseBoolean(readonly));
        if (null != styleClass) inputTextCom.setStyleClass(styleClass);
        if (null != required) inputTextCom.setRequired(Boolean.parseBoolean(required));
        if (null != requiredMessage) inputTextCom.setRequiredMessage(requiredMessage);

        component.getChildren().add(inputTextCom);

        if(null != required && Boolean.parseBoolean(required)) {
            HtmlAjaxOutputPanel outpanel = new HtmlAjaxOutputPanel();
            outpanel.setId(componentId + "InputFieldValidation");
            outpanel.setAjaxRendered(true);

            HtmlGraphicImage icon = new HtmlGraphicImage();
            icon.setUrl((String)exprFactory.createValueExpression(elContext, "" +
                    "#{(not empty webUtil.messagesMap['" + inputFieldId + "']) ? '/images/info_red_16_16_.png' : " +
                            "'/images/info_yellow_16_16_.png'}"
                    , String.class).getValue(elContext));
            icon.setStyleClass("errorIcon");
            icon.setTitle(requiredMessage);

            outpanel.getChildren().add(icon);
            component.getChildren().add(outpanel);
        }

        String elementId = inputTextCom.getClientId(context);

        writer.startElement("script", component);
        writer.writeAttribute("type", "text/javascript", null);
        writer.write("if(typeof " + inputFieldId + "StoredValue != \"undefined\") document.getElementById('" + 
                elementId + "').value = " + inputFieldId + "StoredValue;");
        writer.endElement("script");
    }

    @Override
    protected void doEncodeEnd(ResponseWriter writer, FacesContext context,
            UIComponent component) throws IOException {
        writer.endElement("div");
    }
}

【问题讨论】:

  • 我使用的是 Richfaces 3.3.3 Final 和 JSF 1.2

标签: jsf richfaces


【解决方案1】:

好的,我找到了问题,问题出在 facelets.REFRESH_PERIOD 上,如果这个属性为正值,那么原始文档的每个请求都会重新构建 ui 树(调用 facelets 刷新方法)

所以解决方案是禁用该属性并将其设置为 -1

【讨论】:

    猜你喜欢
    • 2020-03-01
    • 2011-08-24
    • 2011-11-26
    • 2021-04-04
    • 2011-12-23
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多