ViewScope bean是否允许ajax调用?
例如:
这是一个xhtml页面:
<h:body> <h:selectOneMenu value="#{test.selected}"> <f:selectItem itemValue="2" itemLabel="2" /> <f:selectItem itemValue="3" itemLabel="3" /> <f:ajax listener="#{test.updateData()}" /> </h:selectOneMenu> </h:body>这是ViewScoped Bean:
@ViewScoped @Named public class test implements Serializable{ private String selected; public void updateData(){ System.out.println("Ajax call successful."); } public String getSelected() { return selected; } public void setTest(String test) { this.selected = selected; } }当我尝试这个时,它没有用。
Do ViewScope beans allow ajax calls?
For example:
Here is a xhtml page:
<h:body> <h:selectOneMenu value="#{test.selected}"> <f:selectItem itemValue="2" itemLabel="2" /> <f:selectItem itemValue="3" itemLabel="3" /> <f:ajax listener="#{test.updateData()}" /> </h:selectOneMenu> </h:body>Here is the ViewScoped Bean:
@ViewScoped @Named public class test implements Serializable{ private String selected; public void updateData(){ System.out.println("Ajax call successful."); } public String getSelected() { return selected; } public void setTest(String test) { this.selected = selected; } }When I tried this, it didn't work.
最满意答案
实际上,到目前为止,您发布的代码中存在3个严重问题:
缺少<h:form> 。 不确定这是否是对问题或实际代码的粗心准备。 解决方案很明显:将分组的UIInput和UICommand组件放在一个独立的<h:form> 。
在当前的JSF版本中,JSF的范围注释@javax.faces.bean.ViewScoped不适用于CDI的bean管理注释@Named 。
基本上有3种解决方案:
请改用CDI的@ConversationScoped 。 另请参见如何在JSF 2.0 / 2.1中通过CDI替换@ManagedBean / @ViewScope作为具体示例。 然而,这会产生令人讨厌的副作用,它会将cid请求参数附加到每个结果URL。 使用MyFaces CODI将JSF @ViewScoped桥@ViewScoped CDI。 然而,这有一个令人讨厌的副作用,它将windowId请求参数附加到每个结果URL。 等待JSF 2.2发布,以便您可以使用兼容CDI的@javax.faces.view.ViewScoped注释将其与JSF视图状态联系起来。然而,这不是导致具体问题的原因,因为应该只调用bean操作,但bean的行为就像@RequestScoped 。
缺少setter方法。 但是,在成功提交表单时,应该向服务器日志抛出一个相当自解释的PropertyNotWritableException 。
第四种可能的原因根据目前提供的信息无法确定:缺少<h:head>会导致<f:ajax>失败,因为所需的jsf.js不能自动包括在内。
Actually, you've 3 severe problems in the code posted so far:
The <h:form> is missing. Not sure though if this is careless preparation of the question or the actual code. The solution is obvious: put grouped UIInput and UICommand components in an independent <h:form>.
In the current JSF version, JSF's scope annotation @javax.faces.bean.ViewScoped doesn't work on CDI's bean management annotation @Named.
There are basically 3 solutions:
Use CDI's @ConversationScoped instead. See also How to replace @ManagedBean / @ViewScope by CDI in JSF 2.0/2.1 for a concrete example. This has however the nasty side effect that it appends a cid request parameter to every outcome URL. Use MyFaces CODI to bridge JSF @ViewScoped to CDI. This has however the nasty side effect that it appends a windowId request parameter to every single outcome URL. Wait for JSF 2.2 to be released so that you can utilize the CDI compatible @javax.faces.view.ViewScoped annotation which ties it with the JSF view state.This is however hardly the cause of your concrete problem as the bean action should just be invoked, but the bean will just behave like a @RequestScoped.
The setter method is missing. This should however have thrown a rather self-explaining PropertyNotWritableException to the server logs when the form is successfully submitted.
There is a fourth possible cause which can't be for sure be confirmed based on the information provided so far: a missing <h:head> would cause the <f:ajax> to fail because the required jsf.js couldn't be auto-included.
更多推荐
selected,test,ajax,public,body>,电脑培训,计算机培训,IT培训"/> <meta name=
发布评论