FacesComponent 在一个页面中工作,但在其他页面中不起作用(从 ajax 事件中检索空值)



我不知道是否有解决问题。如果是这样,我找不到它。

我正在创建一个带有两个启动事件的selectonemenu的复合组件。问题是该组件在一个JSF中起作用,但在其他JSF中不行。

该组件在三个不同的XHTML内部。当我更改第一个SelectOneMenu的值时,在所有这些中都会触发事件,但仅在XHTML之一中,SelectItem的值并非null。在其余的中,检索的值是" null"

 //In p001DatosNotificacion.xhtml, it retrieves the selected value of the selectonemenu (b, for example)
 //In the others it retrieves null
 UISelectOne oneVoewl = (UISelectOne)event.getSource();
 System.out.println("One Voewl > " + oneVoewl.getValue());

有人可以帮我,告诉我我在哪里犯了错误?

预先感谢!

代码

组件支持bean

@FacesComponent(value="letterExample")
public class LetterExample extends UINamingContainer{
    public void voewlAjaxListener(AjaxBehaviorEvent event){
        UISelectOne oneVoewl = (UISelectOne)event.getSource();
        System.out.println("One Voewl > " + oneVoewl.getValue());
    }

复合组件注意:我正在使用现有的bean进行测试

<cc:interface componentType="letterExample" >
    <cc:attribute name="bean" type="es.ccasa.sgnx.business.bean.Direccion" />
</cc:interface>

<cc:implementation>
    <p:selectOneMenu id="oneMenuVoewl" value="#{cc.attrs.bean.codigoPais}" 
        binding="#{cc.uiSelectOneVoewl}" >
            <f:selectItem itemLabel="Aaaa" itemValue="a" />
            <f:selectItem itemLabel="Eeee" itemValue="e" />
            <f:selectItem itemLabel="Iiii" itemValue="i" />
            <f:selectItem itemLabel="Oooo" itemValue="o" />
            <f:selectItem itemLabel="Uuuu" itemValue="u" />
        <p:ajax event="change"  listener="#{cc.voewlAjaxListener}" update="consonantWrapper" />
    </p:selectOneMenu>
    <h:panelGroup id="consonantWrapper" layout="block">
        <p:selectOneMenu id="oneMenuConsonant"  value="#{cc.attrs.bean.codigoProvincia}"
            binding="#{cc.uiSelectOneConsonant}">
                <f:selectItem itemLabel="Bbbb" itemValue="b" />
                <f:selectItem itemLabel="Cccc" itemValue="c" />
                <f:selectItem itemLabel="Dddd" itemValue="d" />
        </p:selectOneMenu>
    </h:panelGroup>
</cc:implementation>

bean

public class Direccion implements Serializable {
    private String codigoPais; /* with its getter and setter*/
    private String codigoProvincia; /* with its getter and setter*/
}

XHTML 这是我在XHTML页面中使用的方式:(控制器更改的名称取决于当前控制器)

<sgnx:letter-example bean="#{controller.direccion}" />

@Named Beans

@Named
@ViewScoped
public class P001DatosNotificacionController
    private Direccion direccion; /* with its getter and setter*/
@Named
@ViewScoped
public class P001GestionNotificacionesController
    private Direccion direccion; /* with its getter and setter*/
@Named
@ViewScoped
public class P002BandejaProvisionalNotificacionesController
    private Direccion direccion; /* with its getter and setter*/

几周后,我回到了这个问题。我决定从头开始重新开始。

当我开始编写控制器代码并用@ViewScoped注释时,我会意识到javax.faces.beanjavax.faces.view软件包

那是错误。我使用了错误的组合:

import javax.inject.Named;    
import javax.faces.bean.ViewScoped;

因此,解决方案与使用适当的包装组合一样容易:

import javax.inject.Named;
import javax.faces.view.ViewScoped;

在这些帖子中是答案和解释:

  1. 使用JSF 2.2
  2. ,在每个回发请求上重新创建@ViewScoped Bean
  3. CDI中的ManagedProperty @Named Bean返回null
  4. 将CDI Bean注入JSF @ViewScoped Bean

最新更新