views:

1409

answers:

3

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 Im trying to do is create my own tag library with the following tag . 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 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 diplay 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 ect.

Any clues?

A: 

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.

Phill Sacre
Very very interesting. Atm by doing the getChildre().add(link) it appends my button/link all the way on the end of the page. How can I output it where I want it in my sequence of htmloutputs? For example Im writing a div and I want the link to be within that div. Thanks alot so far!
ChrisAD
To sum it up I basicly want to add those child components where I want them. Atm they just append at the end of the GUI.
ChrisAD
Have you tried calling encodeBegin(...) etc on those components you want to render?
Phill Sacre
hmm no I havent. I have encodeBegin and encodeEnd in my program, but everything I render is done in my encodeBegin method. Its really big with several hundred lines of code atm. Am I doing this the wrong way maybe?
ChrisAD
A: 

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

}
McDowell
A: 

This problem was solved by adding new components by using HtmlCommandButton button = new HtmlCommandButton(); button.encodeAll(context);

ChrisAD