【发布时间】:2014-08-11 03:36:57
【问题描述】:
我正在尝试将 JSF 视图呈现为字符串。我有一个 Singleton、一个支持 bean 和一个类似于以下示例的视图。
我不知道如何将我的支持 bean 注入我的单例中。 使用下面的代码,控制台中会打印日志行“Fine: TestBean created手动”,这意味着 bean 没有正确实例化。我还收到以下警告:
严重:无法在实例 javax.faces.ViewRoot 上调用 @PostConstruct 注释方法
还有一个异常堆栈:
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)
代码的目的是呈现个性化的邮件。我正在尝试将一些值放在支持 bean 中,然后将视图呈现为字符串作为邮件正文。 我尝试了支持 bean 的各种范围。
谢谢,
查理
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);
}
}
}
【问题讨论】: