tags:

views:

29

answers:

1

Hi!

Im using JSF 1.2 and need to include xhtml content represented as a String in a bean.

So, how can I get the content from a bean in xhtml but represented as a String and render it on the page? Here is an example:

myPage.xhml

...
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
...
<h:panelGrid>
   <a4j:outputPanel ajaxRendered="true">
      <ui:include src="#{myBean.someContent}" /> <!-- this doesnt work! -->
   </a4j:outputPanel>
</h:panelGrid>
...

MyBean.java

...
class MyBean ... {
   private String someContent = "<h:panelGrid><h:outputText value=\"Name:\"/><h:inputText value=\"#{anotherBean.name}\" /></h:panelGrid>";

   public String getSomeContent() {
      return someContent;
   }

   public void setSomeContent(String someContent) {
      this.someContent = someContent;
   }
}

i.e. in myPage.xhtml I want to read the someContent variable and include the content before page evaluation. The ui:include-tag nor the h:outputText escape="false" seems to work.

/happycoder

+1  A: 

You can't represent JSF components as a String. There is nothing like eval() in JSF. If you can't save it (programmatically) in its own file which you then in turn just include using ui:include, then you need to create the components programmatically. Here's a basic example:

<h:panelGroup binding="#{bean.group}" />

with

private HtmlPanelGroup group; // +getter +setter

public Bean() {
    group = new HtmlPanelGroup();

    HtmlPanelGrid grid = new HtmlPanelGrid();
    group.getChildren().add(grid);

    HtmlOutputText text = new HtmlOutputText(); // Why not h:outputLabel/HtmlOutputLabel?
    text.setValue("Name:");
    grid.getChilderen().add(text);

    HtmlInputText input = new HtmlInputText();
    input.setId("inputId"); // ID is mandatory on dynamic UIInput/UICommand!
    input.setValue(createValueExpression("#{anotherBean.name}", String.class));
    grid.getChildren().add(input);
}

private static ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    return facesContext.getApplication().getExpressionFactory().createValueExpression(
        facesContext.getELContext(), valueExpression, valueType);
}
BalusC
Ok, thank you. Ill try to solve my problem in another way then. /happycoder
happycoder