使用dataTable时不调用JSF2.0支持bean setter

时间:2013-09-16 06:02:52

标签: jsf-2

我有一个使用

的屏幕
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:body>

    <h:form>
    <f:event listener="#{pageload.getPageLoad}" type="preRenderView" />
    <h:dataTable value="#{pageload.fieldConfig}" var="field" 
    columnClasses="lblFirstCol,lblSecondCol,lblThirdCol,lblFourthCol" id="table1" styleClass="tblSecond" >
        <h:column >
        <h:outputText value="#{field.label_name}" />
        </h:column>
        <h:column>              
            <h:inputText value="#{searchdevice.device.terminal_name}" />
        </h:column>

    </h:dataTable> 

    <h:commandButton value="Submit" action="#{searchdevice.searchButtonAction}"/>
    </h:form>
</h:body>

我的支持豆

@ManagedBean(name="pageload")
@RequestScoped
public class PageLoadBean {

private List<FieldConfigVO> fieldconfig;
      //getters and setters

      // method  to populate the ArrayList
      public void getPageLoad(){
                   //getting populated from Database
         fieldconfig = common.getFieldConfig("001");        
      }  
  }

另一个输入bean

@ManagedBean(name="searchdevice")
@RequestScoped
 public class SearchDeviceBean {

private DeviceVO device;




public SearchDeviceBean() {
    device = new DeviceVO();
}

public DeviceVO getDevice() {
    return device;
}

public void setDevice(DeviceVO device) {
    this.device = device;
}

public String searchButtonAction(){
    System.out.println(device.getTerminal_name()+"****TERMINAL NAME******");
            FacesContext context = FacesContext.getCurrentInstance();
    if (context.getMessageList().size() > 0) {
        return(null);
    }else {

        return("success");
    }

}
     }

我的设备对象具有terminal_name属性。我有一个命令按钮,它调用SearchDeviceBean中的方法,并在提交表单时输入的任何值都不会被填充

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

您正在preRenderView事件中执行数据初始化逻辑。这是需要为回发准备模型的代码的错误位置。当JSF需要在表单提交期间更新模型值时,它遇到一个完全为空的fieldConfig,因此JSF无法在那里设置提交/转换/验证的值。在你的情况下,fieldConfig仅在后期阶段(渲染响应阶段)准备,因此为时已晚。

您需要在@PostConstruct中初始化它。在bean的构造和依赖性之后立即调用它。完全摆脱整个<f:event>并在@PostConstruct方法上添加getPageLoad()注释。顺便说一下,我还将该方法重命名为init()loadFieldConfig(),因为它根本不是一个getter方法,因此对于阅读/维护代码的其他人来说这个名字非常混乱。

另见: