我正在尝试将JSF视图呈现为字符串。我有一个Singleton,一个支持bean和一个视图simillar到下面的例子。
我无法弄清楚如何将我的支持bean注入我的单身人士。 使用下面的代码,在控制台中打印日志行“Fine:TestBean手动创建”,这意味着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)
代码的目的是呈现个性化邮件。我试图将一些valules放在一个支持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);
}
}
}
答案 0 :(得分:0)
@ManagedProperty
注释在@ManagedBean
之外无效。
来自http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedProperty.html
如果这个注释出现在一个类上 没有ManagedBean注释,实现必须 对此注释不采取任何措施
使用@Inject和@ApplicationScoped
注释您的beanimport javax.inject.Named;
import javax.enterprise.context.ApplicationScoped;
@Named
@ApplicationScoped
public class TestBean implements Serializable
在@Singleton
中添加@Inject注释@Singleton
@Startup
public class TestSingleton implements Serializable {
@Inject
private TestBean testBean;
答案 1 :(得分:-1)
我认为这是因为你有一个循环依赖。
TestBean
引用TestSingleton
和TestSingleton
要求注入TestBean
。