JSF - Custom Tags - Dynamic values disappear after refresh

左心房为你撑大大i 提交于 2019-12-25 04:26:53

问题


I've followed this tutorial for implementing a marquee-tag in JSF 2.1 and succeeded partly. As this tag does NOT support dynamic data, e.g. #{bean.var} as a value, I've decided to do it dirty inside the component.

However, after reloading my page, the value disappears. The tag is still there, but the content is gone.

  1. Could you show me how to implement is correctly, that I can use my dynamic value inside the value-attribute?
  2. Or would you point me into the right direction which code is causing the error in my component class?

Thank you very much!

http://myjavabuddy.blogspot.de/2013/04/writing-custom-components-for-jsf-20.html

This is my JSF

<customJSF:marquee value="" />

This is my Component

@FacesComponent ("amelunxenfast.prog3.wissensmanagement.components.marquee")
public class MarqueeComponent extends UIComponentBase {

  public static final String COMPONENT_TYPE = "com.himanshu.jsf.custom.marquee";

    String value = null;

    @EJB
    FeedEJB ejb;

    public String getValue() {
        return value;
    }

    @Override
    public String getFamily() {
        return COMPONENT_TYPE;

}

@Override
public void encodeBegin(FacesContext context) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    writer.startElement("marquee", this);
    writer.writeAttribute("scrollamount", "10", "");
    writer.write(ejb.getFeedString());
    writer.endElement("marquee");
}


@Override
public void encodeEnd(FacesContext arg0) throws IOException {
    super.encodeEnd(arg0);
}

public void setValue(String value) {
    this.value = value;
}

}


回答1:


Too long for a comment...

I don't think it's legit to inject @EJB inside @FacesComponent and i don't think it's a good practice, in this specific case.

I think a better approach should be extending TextRenderer with your own class, declare a new component in faces-config and use it the same way you use h:outputText (passing ValueExpression in value attribute)



来源:https://stackoverflow.com/questions/21948157/jsf-custom-tags-dynamic-values-disappear-after-refresh

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