如何防止f:selectItems显示空行?

时间:2016-07-14 07:18:33

标签: jsf

我有2个彼此相关的数据库表;

  

员工和联系

我想为Employee创建一个下拉菜单,并显示员工的名字和姓氏,它们是Contact表中的一列。

    public static void main(String[] args) {
    String url = "http://ip:8080/drools-wb/maven2wb/org/mydemo/myDemo/1.0/myDemo-1.0.jar";

    // make sure you use "LATEST" here!
    ReleaseIdImpl releaseId = new ReleaseIdImpl("org.mydemo", "myDemo", "1.0");

    KieServices ks = KieServices.Factory.get();

    ks.getResources().newUrlResource(url);

    KieContainer kieContainer = ks.newKieContainer(releaseId);

    // check every 5 seconds if there is a new version at the URL
    KieScanner kieScanner = ks.newKieScanner(kieContainer);
    kieScanner.start(5000L);
    // alternatively:
    // kieScanner.scanNow();

    Scanner scanner = new Scanner(System.in);
    while (true) {
        runRule(kieContainer);
        System.out.println("Press enter in order to run the test again....");
        scanner.nextLine();
    } 


}

private static void runRule(KieContainer kieKontainer) {
    StatelessKieSession kSession = kieKontainer.newStatelessKieSession("testSession");
    kSession.setGlobal("out", System.out);
    kSession.execute("testRuleAgain");
} 

联系表允许第一个名称列和姓氏列为空,因为它允许空字符串selectItems上显示的某些值为空。

如何防止显示/呈现这些空的名字或空名?

1 个答案:

答案 0 :(得分:0)

就像@ subodh-joshi所说的那样,您可以使用getActiveEmployees EmployeeController方法过滤员工,无需姓或者姓:

public List<Employee> getActiveEmployees() {
    List<Employee> employees = ... // get the active employees list
    List<Employee> validEmployees = new ArrayList<Employee>();

    for (Employee employee : employees) {
        if ((employee.getFirstName() != null && !employee.getFirstName().trim().isEmpty()) && (employee.getLastName() != null && !employee.getLastName().trim().isEmpty())) {
            validEmployees.add(employee);
        }
    }

    return validEmployees;
}

或者更好,您可以返回List<SelectItem>

public List<SelectItem> getActiveEmployees() {
    List<Employee> employees = ... // get the active employees list
    List<SelectItem> validEmployees = new ArrayList<SelectItem>();

    for (Employee employee : employees) {
        if ((employee.getFirstName() != null && !employee.getFirstName().trim().isEmpty()) && (employee.getLastName() != null && !employee.getLastName().trim().isEmpty())) {
            validEmployees.add(new SelectItem(employee, employee.getContact.getFirstName()));
        }
    }

    return validEmployees;
}

返回List SelectItem,无需使用itemValue的{​​{1}}和itemLabel属性。