0
votes

I am trying to render a JSF view to a string. I have a Singleton, a backing bean and a view simillar to the examples below.

I can't figure out how to inject my backing bean into my singleton. With the code below, the log line "Fine: TestBean created manually" is printed in the console, meaning the bean is not correctly instanciated. I also get the following warning:

Severe: Unable to invoke @PostConstruct annotated method on instance javax.faces.ViewRoot

And an exception stack:

com.sun.faces.spi.InjectionProviderException: com.sun.enterprise.container.common.spi.util.InjectionException: Wrong invocation type
    at org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:231)
[...]
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Wrong invocation type
at org.glassfish.faces.integration.GlassFishInjectionProvider.getNamingEnvironment(GlassFishInjectionProvider.java:262)
at org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:229)

The purpose of the code is to render a personalized mail. I am trying to put some valules in a backing bean then render the view to string as the mail body. I've attempted various scopes for the backing bean.

Thanks,

Charly

test.xhtml:

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:c="http://java.sun.com/jsp/jstl/core">

    <h:body>
        <h1> Hello </h1>
        <p>
            Bean String : #{testBean.theString}
        </p>
        <h:form>
            <h:commandLink value="Render it"
                           action="#{testBean.actionRenderTest()}"/>
        </h:form>
    </h:body>
</html>

TestBean.java:

// skipped package and imports

@ManagedBean
@ApplicationScoped
public class TestBean implements Serializable{

    @EJB
    private TestSingleton singleton;

    private String theString;

    public String getTheString() {
        return theString;
    }

    public void setTheString(String theString) {
        this.theString = theString;
    }

    public void actionRenderTest() {
        singleton.renderTest();
    }

}

TestSingleton.java:

// package&imports

@Singleton
@Startup
public class TestSingleton implements Serializable {

    @ManagedProperty("#{testBean}")
    private TestBean testBean;


    public void renderTest() {
        if (testBean == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            testBean = context.getApplication().evaluateExpressionGet(context, "#{testBean}", TestBean.class);
            Logger.getLogger(TestSingleton.class.getName()).fine("TestBean created manually");
        }

        testBean.setTheString("string set from my singleton");
        try {
            FacesContext context = FacesContext.getCurrentInstance();
// store the original response writer
            ResponseWriter originalWriter = context.getResponseWriter();

// put in a StringWriter to capture the output
            StringWriter stringWriter = new StringWriter();
            ResponseWriter writer = createResponseWriter(context, stringWriter);
            context.setResponseWriter(writer);

// create a UIViewRoot instance
            ViewHandler viewHandler = context.getApplication().getViewHandler();
            final String template = "/test/test.xhtml";
            UIViewRoot view = viewHandler.createView(context, template);

// the fun part -- do the actual rendering here
            ViewDeclarationLanguage vdl = viewHandler
                    .getViewDeclarationLanguage(context, template);
            vdl.buildView(context, view);
            renderChildren(context, view);

// restore the response writer
            if (originalWriter != null) {
                context.setResponseWriter(originalWriter);
            }

            Logger.getLogger(TestSingleton.class.getName()).log(Level.FINE, stringWriter.toString());
        } catch (IOException ex) {
            Logger.getLogger(TestSingleton.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Create ResponseWriter. Taken from FaceletViewDeclarationLanguage.java of
     * MyFaces.
     */
    private ResponseWriter createResponseWriter(FacesContext context,
            Writer writer) {
        ExternalContext extContext = context.getExternalContext();
        Map<String, Object> requestMap = extContext.getRequestMap();
        String contentType = (String) requestMap.get("facelets.ContentType");
        String encoding = (String) requestMap.get("facelets.Encoding");
        RenderKit renderKit = context.getRenderKit();
        return renderKit.createResponseWriter(writer, contentType, encoding);
    }

    /**
     * Render a UIComponent. Taken from JSF.java of Seam 2.2.
     */
    private void renderChildren(FacesContext context, UIComponent component)
            throws IOException {
        List<UIComponent> children = component.getChildren();
        for (int i = 0, size = component.getChildCount(); i < size; i++) {
            UIComponent child = (UIComponent) children.get(i);
            renderChild(context, child);
        }
    }

    /**
     * Render the child and all its children components.
     */
    private void renderChild(FacesContext context, UIComponent child)
            throws IOException {
        if (child.isRendered()) {
            child.encodeAll(context);
        }
    }
}
2

2 Answers

0
votes

@ManagedProperty annotation has no effect outside the @ManagedBean.

From http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedProperty.html

If this annotation is present on a class that does not have the ManagedBean annotation, the implementation must take no action on this annotation

Annotate your bean using @Inject and @ApplicationScoped

import javax.inject.Named;
import javax.enterprise.context.ApplicationScoped;

@Named
@ApplicationScoped
public class TestBean implements Serializable

And add @Inject annotation in your @Singleton

@Singleton
@Startup
public class TestSingleton implements Serializable {

    @Inject
    private TestBean testBean;
-1
votes

I think this is because you have a cyclic dependance. TestBean reference TestSingleton and TestSingleton ask for an injection of TestBean.