JSF ResponseWriter 自定义组件

发布于 2024-07-15 07:48:40 字数 790 浏览 14 评论 0原文

我了解 ResponseWriter 上的 startElement、endElement 和 writeAttribute 方法。 我的问题是,我想通过像 HtmlCommandLink link = new HtmlCommandLink(); 那样声明来输出 ah:commandLink 。

如何在我自己的组件中输出这样的其他 UIComponent? 我可能还想在我的组件中使用一些 RichFaces ajax 的东西,所以希望我可以避免从头开始做这一切。

编辑:我想要做的是使用以下标签 创建我自己的标签库。 每个评论都有一个回复按钮,当单击回复按钮时,我会在评论下方呈现回复表单。 一旦渲染完成,我想输出例如 richfaces 组件。 这必须在我自己的 java 标记文件中完成,我调用了 CommentsTreeUI.java

通常,我使用 writer.startElement("input", myComponent); 输出显示表单和按钮的所有元素; writer.writeAttribute("type", "button", null); 但如果我可以这样做,例如 startElement("a4j:commandbutton", myComponent) 这将对我有很大帮助,因为它具有所有内置的 ajax 功能等。

有任何线索吗?

I know about startElement, endElement, and writeAttribute methods on ResponseWriter. My problem is that I want to for example output a h:commandLink by declaring it like HtmlCommandLink link = new HtmlCommandLink(); .

How can I output other UIComponents like this in my own component? I might want to use some RichFaces ajax stuff in my components aswell so hoping I can avoid making it all by scratch.

Edit: What I'm trying to do is create my own tag library with the following tag <myTags:commentTree>. Every comment have a reply button, when the reply button is clicked I render the reply form beneath the comment. Once that is rendered, I would like to output for example the richfaces <a4j:commandButton> component. This have to be done inside my own java tag file which Ive called for CommentsTreeUI.java.

Normally I output all my elements that display the forms and buttons with writer.startElement("input", myComponent); writer.writeAttribute("type", "button", null); but if I could instead do for example startElement("a4j:commandbutton", myComponent) that would help my ALOT since it has all the built in ajax features etc.

Any clues?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深如许 2024-07-22 07:48:41

通过使用添加新组件解决了这个问题

HtmlCommandButton button = new HtmlCommandButton();
button.encodeAll(context);

This problem was solved by adding new components by using

HtmlCommandButton button = new HtmlCommandButton();
button.encodeAll(context);
谁把谁当真 2024-07-22 07:48:41

您可以这样做:

HtmlCommandLink link = new HtmlCommandLink();
getChildren().add(link);

它确实取决于您想要对子组件执行的操作,即如果您希望它们被自定义 HTML 包围(例如,在 HTML 列表中),您将需要更复杂的东西。

You can do something like this:

HtmlCommandLink link = new HtmlCommandLink();
getChildren().add(link);

It does depend on what you want to do with the child components though i.e. if you want them surrounded with custom HTML (in an HTML list, for example) you will need something a bit more complex.

流年里的时光 2024-07-22 07:48:41

制作复合控件的一种方法是使用 binding 属性将标记与您自己的代码关联:

<f:view>
    <h:form>
        <h:panelGroup binding="#{compositeControlBean.panelGrid}" />
    </h:form>
</f:view>

faces-config.xml 中的 bean 配置:

<managed-bean>
    <managed-bean-name>compositeControlBean</managed-bean-name>
    <managed-bean-class>
        composite.CompositeControlBean
    </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

bean 代码:

/**
 * Configure this bean in request scope as "compositeControlBean".
 */
public class CompositeControlBean {

  private transient UIComponent panelGrid;

  public UIComponent getPanelGrid() {
    if (panelGrid == null) {
      panelGrid = createCompositePanel();
    }
    return panelGrid;
  }

  public void setPanelGrid(UIComponent panelGrid) {
    this.panelGrid = panelGrid;
  }

  private UIComponent createCompositePanel() {
    initContextMemebers();

    UIComponent commandLink = createCommandLink();

    String id = view.createUniqueId();
    UIComponent panelGrid = application
        .createComponent("javax.faces.HtmlPanelGroup");
    panelGrid.setId(id);
    panelGrid.setRendererType("javax.faces.Group");

    panelGrid.getChildren().add(commandLink);

    return panelGrid;
  }

  private UIComponent createCommandLink() {
    // create control
    String id = view.createUniqueId();
    UIComponent commandLink = application
        .createComponent("javax.faces.HtmlCommandLink");
    commandLink.setId(id);
    commandLink.setRendererType("javax.faces.Link");
    // set attributes (bind to printHello method)
    Map<String, Object> attributes = commandLink
        .getAttributes();
    MethodExpression action = expressionFactory
        .createMethodExpression(elContext,
            "#{compositeControlBean.printHello}",
            String.class, new Class<?>[0]);
    attributes.put("value", "print hello");
    attributes.put("actionExpression", action);
    return commandLink;
  }

  private transient FacesContext context;
  private transient Application application;
  private transient ELContext elContext;
  private transient ExpressionFactory expressionFactory;
  private transient UIViewRoot view;

  private void initContextMemebers() {
    context = FacesContext.getCurrentInstance();
    application = context.getApplication();
    elContext = context.getELContext();
    expressionFactory = application.getExpressionFactory();
    view = context.getViewRoot();
  }

  public String printHello() {
    System.out.println("Hello");
    return null;
  }

}

One approach to making composite controls is to use the binding attribute to associate the tag with your own code:

<f:view>
    <h:form>
        <h:panelGroup binding="#{compositeControlBean.panelGrid}" />
    </h:form>
</f:view>

The bean configuration in faces-config.xml:

<managed-bean>
    <managed-bean-name>compositeControlBean</managed-bean-name>
    <managed-bean-class>
        composite.CompositeControlBean
    </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

The bean code:

/**
 * Configure this bean in request scope as "compositeControlBean".
 */
public class CompositeControlBean {

  private transient UIComponent panelGrid;

  public UIComponent getPanelGrid() {
    if (panelGrid == null) {
      panelGrid = createCompositePanel();
    }
    return panelGrid;
  }

  public void setPanelGrid(UIComponent panelGrid) {
    this.panelGrid = panelGrid;
  }

  private UIComponent createCompositePanel() {
    initContextMemebers();

    UIComponent commandLink = createCommandLink();

    String id = view.createUniqueId();
    UIComponent panelGrid = application
        .createComponent("javax.faces.HtmlPanelGroup");
    panelGrid.setId(id);
    panelGrid.setRendererType("javax.faces.Group");

    panelGrid.getChildren().add(commandLink);

    return panelGrid;
  }

  private UIComponent createCommandLink() {
    // create control
    String id = view.createUniqueId();
    UIComponent commandLink = application
        .createComponent("javax.faces.HtmlCommandLink");
    commandLink.setId(id);
    commandLink.setRendererType("javax.faces.Link");
    // set attributes (bind to printHello method)
    Map<String, Object> attributes = commandLink
        .getAttributes();
    MethodExpression action = expressionFactory
        .createMethodExpression(elContext,
            "#{compositeControlBean.printHello}",
            String.class, new Class<?>[0]);
    attributes.put("value", "print hello");
    attributes.put("actionExpression", action);
    return commandLink;
  }

  private transient FacesContext context;
  private transient Application application;
  private transient ELContext elContext;
  private transient ExpressionFactory expressionFactory;
  private transient UIViewRoot view;

  private void initContextMemebers() {
    context = FacesContext.getCurrentInstance();
    application = context.getApplication();
    elContext = context.getELContext();
    expressionFactory = application.getExpressionFactory();
    view = context.getViewRoot();
  }

  public String printHello() {
    System.out.println("Hello");
    return null;
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文