我有一个问题,我的@ViewScoped managed bean 表现得像@RequestScoped managedBean,仅仅是因为我使用了 composite:insertChildren 标签。我已经阅读了关于该主题的其他帖子,并且知道 @ViewScoped managedBeans 的局限性,但我没有任何显式绑定(bind),也没有在我的复合组件中使用 JSTL。
我假设 insertChildren 标签被绑定(bind)到 managedBean,但我希望有人可以让我摆脱痛苦并告诉我一个解决方法 - 我真的不想开始使用 @ SessionScoped bean。 :)
这是我的简化示例。简单的托管 Bean:
@ManagedBean(name = "simpleMB")
@ViewScoped
public class SimpleManagedBean implements Serializable {
private static final long serialVersionUID = -1;
@NotNull
@Email
private String email;
public SimpleManagedBean() {
System.out.println("SimpleManagedBean");
}
@PostConstruct
public void postConstruct() {
System.out.println("Post construct");
}
public String submit() {
return null;
}
... setter and getter
}
将上面的 SimpleManagedBean 与下面的表单和复合组件(没有 insertChildren)结合使用,一切都按预期进行。我输入了一些文本,按提交,错误随输入的文本一起显示。在这一点上,我很高兴。
<h:form>
<foo:simpleNoChildren />
<h:commandButton id="submit"
value="Submit"
action="#{simpleMB.submit}" />
</h:form>
... and the composite component ....
<composite:interface />
<composite:implementation>
<h:panelGrid columns="3">
<h:outputText value="Email" />
<h:inputText id="email"
value="#{simpleMB.email}"
required="true" />
<h:message for="email" />
</h:panelGrid>
</composite:implementation>
现在,如果我将 panelGrid 及其组件移出复合组件并替换为 composite:insertChildren 标记,如下所示,当我输入一些文本并按提交时,会显示正确的错误消息,但由于再次调用@PostConstruct 方法,我输入的文本不再显示。 :(
<h:form>
<foo:simple>
<h:panelGrid columns="3">
<h:outputText value="Email" />
<h:inputText id="email" value="#{simpleMB.email}" required="true" />
<h:message for="email" />
</h:panelGrid>
</foo:simple>
<h:commandButton id="submit" value="Submit" action="#{simpleMB.submit}" />
</h:form>
... and my complicated composite component :) ...
<composite:interface />
<composite:implementation>
<composite:insertChildren />
</composite:implementation>
有什么想法或建议吗?
提前致谢。
最佳答案
对于有同样问题的其他人:
这是 JSF 中的一个已知错误 -> 更多信息请参阅 http://java.net/jira/browse/JAVASERVERFACES-2040
一种可能的解决方法是向复合界面添加一个方面,并使用 cc:renderFacet 而不是 cc:insertChildren 渲染该方面。
关于java - JSF 2 复合组件 insertChildren 标记使 @ViewScoped 变为 @RequestScoped :(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4083499/