JSF根据星期几注入bean

时间:2014-04-10 18:36:54

标签: java jsf java-ee primefaces

我有一个primefaces数据表,必须根据它的星期几显示数据。 我现在有一个检查当天的豆子;

 private AbstractDayController day;
 private int dayint;

 public AbstractDayController getDay() {
    Calendar calendar = Calendar.getInstance();
    dayint = calendar.get(Calendar.DAY_OF_WEEK);
    switch(dayint) {
        case Calendar.MONDAY:
            day = new MondayController();
        break;
        case Calendar.TUESDAY:
            day = new TuesdayController();
        break;
        case Calendar.WEDNESDAY:
            day = new WednesdayController();
        break;
        case Calendar.THURSDAY:
            day = new ThursdayController();
        break; 
        case Calendar.FRIDAY:
            day = new FridayController();
        break;
        case Calendar.SATURDAY:
            day = new SaturdayController();             
        break;
    }
    return day;
}

我的想法是通过调用来检索正确的bean;

<p:dataTable value="#{SetDay.day.dayTasks}" var="task">

其中SetDay是包含代码的Bean,而dayTasks是来自相关Bean的对象列表。但是这个方法给了我一个nullpointerException。

Monday-Saturday控制器是AbstractDayController的子类。

我在ThursdayController中获得了此方法的nullpointer异常;

@Inject
private ThursdayDAO dayDAO;

@Override
public List<Thursday> getDayTasks() {
    this.dayTasks = this.dayDAO.findAll();
    return this.dayTasks;
}

dayDAO正在解析为null。例如,如果我直接使用#{ThursdayController.dayTasks},所有内容都可以正常加载,无异常。

我真的不想为每个bean重新创建表,如果可能的话,我只想在这个请求的bean中调用相关的表。

感谢所有帮助。

修改 我通过将每个日期控制器注入到managedbean并在switch-case中使用它来暂时解决了这个问题。它很难看,但它确实有效;

@Inject
private MondayController monday;
@Inject
private TuesdayController tuesday;
@Inject
private WednesdayController wednesday;
@Inject
private ThursdayController thursday;
@Inject
private FridayController friday;
@Inject
private SaturdayController saturday;

private List dayTasks;

private int dayint;



public List getDayTasks() {
    Calendar calendar = Calendar.getInstance();
    dayint = calendar.get(Calendar.DAY_OF_WEEK);
    switch(dayint) {
        case Calendar.MONDAY:
            dayTasks = monday.getDayTasks();
        case Calendar.TUESDAY:
            dayTasks = tuesday.getDayTasks();
        case Calendar.WEDNESDAY:
            dayTasks = wednesday.getDayTasks();
        case Calendar.THURSDAY:
            dayTasks = thursday.getDayTasks();
        case Calendar.FRIDAY:
            dayTasks = friday.getDayTasks();
        case Calendar.SATURDAY:
            dayTasks = saturday.getDayTasks();
    }
    return dayTasks;
}

然后通过#{SetDay.dayTasks}

调用它

建议采用更清洁的方式来做到这一点。 有没有办法以编程方式注入bean?没有注释? 如果是这样,我可以在switch-case中做到这一点。

1 个答案:

答案 0 :(得分:-2)

使用哪个JSF实现?一些实现在调用方法throw reflication之前查看该字段,因为您的AbstractDayController日被定义为NULL

试试这个,

public AbstractDayController getDay() {
Calendar calendar = Calendar.getInstance();
dayint = calendar.get(Calendar.DAY_OF_WEEK);
switch(dayint) {
    case Calendar.MONDAY:
        return new MondayController();
    case Calendar.TUESDAY:
        return new TuesdayController();
    case Calendar.WEDNESDAY:
        return new WednesdayController();
    case Calendar.THURSDAY:
        return new ThursdayController();
    case Calendar.FRIDAY:
        return new FridayController();
    case Calendar.SATURDAY:
        return new SaturdayController();             
}

}