3

私はJSFとprimefacesが初めてで、非常に厄介な問題に直面しています。

私は素数についてもう少し学ぶために非常に基本的なアプリケーションを作成しています。いくつかのテキスト入力フィールド、2 つの日付ピッカー、1 つのドロップダウン (selectOneMenu) を持つ単純なフォームがあります。

フォームを送信すると、ドロップダウン メニューの値を除いて、すべての値がバッキング Bean に入れられます。そのアイテムのセッターは呼び出されません。public void saveNewActivity(ActionEvent evt)また、アプリケーションは、コマンド ボタンで定義されているように、コントローラーのメソッドを呼び出しません。ただし、htmlのドロップダウンメニューを削除またはコメントアウトすると、そのメソッドが呼び出されます(ただし、ドロップダウンメニューのフィールドは明らかにnullです)。

私は 2 日近く試してみましたが、まだ正しく動作しません。

次のコード(スニペット)があります:

私のhtml/jsfコード

<div id="newActivitycontent">
            <h:form id="newActivityForm">
                <h:messages id="messages"/>
                <table>
                    <tr>
                        <td>Gebruiker:</td>
                        <td><p:selectOneMenu value="#{plannedActivityController.newActivity.organiser}}"
                                             converter="#{userConverter}">
                            <f:selectItem itemLabel="Kies een gebruiker" itemValue=""/>
                            <f:selectItems value="#{plannedActivityController.users}" var="user"
                                           itemLabel="#{user.firstname} #{user.lastname}" itemValue="#{user}"/>
                        </p:selectOneMenu></td>
                    </tr>
                    <tr>
                        <td>Titel:</td>
                        <td><p:inputText value="#{plannedActivityController.newActivity.name}"/></td>
                    </tr>
                    <tr>
                        <td>Beschrijving:</td>
                        <td><p:inputText value="#{plannedActivityController.newActivity.desctription}"/></td>
                    </tr>
                    <tr>
                        <td>Startdatum:</td>
                        <td><p:calendar value="#{plannedActivityController.newActivity.startDateDate}"/></td>
                    </tr>
                    <tr>
                        <td>Einddatum:</td>
                        <td><p:calendar value="#{plannedActivityController.newActivity.endDateDate}"/></td>
                    </tr>
                </table>
                <p:commandButton id="btnSaveNewActivity" value="Opslaan"
                                 actionListener="#{plannedActivityController.saveNewActivity}"
                                 update=":overviewForm:activityTable messages"/>
                <p:commandButton id="btnCancelNewActivity" value="Annuleren"
                                 actionListener="#{plannedActivityController.cancelNewActivity}" onclick="hideAddNewUI()"
                                 update=":overviewForm:activityTable" type="reset" immediate="true"/>
            </h:form>
        </div>

そのコードで使用されるコントローラー:

@Named
@SessionScoped
public class PlannedActivityController implements Serializable {

    @Inject
    private ApplicationModel appModel;

    @Inject
    private SessionModel sessionModel;

    @Inject
    private ActivityMapper activityMapper;

    @Inject
    private UserMapper userMapper;

    private ActivityBean newActivity;

    private ActivityBean selectedActivity;

    private List<ActivityBean> activities;

    private List<UserBean> users;

    public PlannedActivityController() {
    }

    @PostConstruct
    public void onCreated() {
        convertActivities();
        onNewActivity();

        users = userMapper.mapToValueObjects(appModel.getUsers());
    }

    public void convertActivities() {
        List<PlannedActivity> originalActivities = appModel.getActivities();
        this.activities = activityMapper.mapToValueObjects(originalActivities);
    }

    public void onRowEditComplete(RowEditEvent event) {
        System.out.println("row edited : " + event.getObject());
        //TODO: save changes back to db!
    }

    public void onRowSelectionMade(SelectEvent event) {
        System.out.println("row selected : " + event.getObject());
        selectedActivity = (ActivityBean)event.getObject();
    }

    //Activity crud methods
    public void onNewActivity() {
        newActivity = new ActivityBean();
        newActivity.setId(new Date().getTime());
    }

    public void saveNewActivity(ActionEvent evt) {
        PlannedActivity newAct = activityMapper.mapToEntity(newActivity);
        if(newAct != null) {
            appModel.getActivities().add(newAct);
        }
        convertActivities();
    }

    public void cancelNewActivity() {
        //TODO: cleanup.
    }

    public void deleteSelectedActivity() {
        if(selectedActivity != null) {
            activities.remove(selectedActivity);
            appModel.setActivities(activityMapper.mapToEntities(activities));
            convertActivities();
        } else {
            //TODO: show error or information dialog, that delete cannot be done when nothing has been selected!
        }
    }

    //Getters & Setters
    public ApplicationModel getAppModel() {
        return appModel;
    }

    public void setAppModel(ApplicationModel appModel) {
        this.appModel = appModel;
    }

    public SessionModel getSessionModel() {
        return sessionModel;
    }

    public void setSessionModel(SessionModel sessionModel) {
        this.sessionModel = sessionModel;
    }

    public ActivityMapper getActivityMapper() {
        return activityMapper;
    }

    public void setActivityMapper(ActivityMapper activityMapper) {
        this.activityMapper = activityMapper;
    }

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public ActivityBean getNewActivity() {
        return newActivity;
    }

    public void setNewActivity(ActivityBean newActivity) {
        this.newActivity = newActivity;
    }

    public ActivityBean getSelectedActivity() {
        return selectedActivity;
    }

    public void setSelectedActivity(ActivityBean selectedActivity) {
        this.selectedActivity = selectedActivity;
    }

    public List<ActivityBean> getActivities() {
        return activities;
    }

    public void setActivities(List<ActivityBean> activities) {
        this.activities = activities;
    }

    public List<UserBean> getUsers() {
        return users;
    }

    public void setUsers(List<UserBean> users) {
        this.users = users;
    }
}

私のアクティビティビーン:

public class ActivityBean implements Serializable {

    private Long id = 0L;

    private String name;

    private String desctription;

    private UserBean organiser;

    private Calendar startDate;

    private Calendar endDate;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesctription() {
        return desctription;
    }

    public void setDesctription(String desctription) {
        this.desctription = desctription;
    }

    public UserBean getOrganiser() {
        return organiser;
    }

    public void setOrganiser(UserBean organiser) {
        this.organiser = organiser;
    }

    public Calendar getStartDate() {
        return startDate;
    }

    public void setStartDate(Calendar startDate) {
        this.startDate = startDate;
    }

    public Date getStartDateDate() {
        if(this.startDate == null) {
            return null;
        }
        return this.endDate.getTime();
    }

    public void setStartDateDate(Date startDate) {
        if(this.startDate == null) {
            this.startDate = new GregorianCalendar();
        }
        this.startDate.setTime(startDate);
    }

    public String getStartDateString() {
        if(this.startDate == null) {
            return null;
        }
        return startDate.get(Calendar.DAY_OF_MONTH) + "/" + startDate.get(Calendar.MONTH) + "/" + startDate.get(Calendar.YEAR) + "";
    }

    public Calendar getEndDate() {
        return endDate;
    }

    public void setEndDate(Calendar endDate) {
        this.endDate = endDate;
    }

    public Date getEndDateDate() {
        if(this.endDate == null) {
            return null;
        }
        return endDate.getTime();
    }

    public void setEndDateDate(Date endDate) {
        if(this.endDate == null) {
            this.endDate = new GregorianCalendar();
        }
        this.endDate.setTime(endDate);
    }

    public String getEndDateString() {
        if(this.endDate == null) {
            return null;
        }
        return endDate.get(Calendar.DAY_OF_MONTH) + "/" + endDate.get(Calendar.MONTH) + "/" + endDate.get(Calendar.YEAR) + "";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ActivityBean that = (ActivityBean) o;

        if (id != null ? !id.equals(that.id) : that.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
}

私のユーザービーン:

public class UserBean {

    private Long id;

    private String username;

    private String firstname;

    private String lastname;

    private String email;

    private String phone;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserBean userBean = (UserBean) o;

        if (id != null ? !id.equals(userBean.id) : userBean.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
}

また、selectOneMenu で使用されるコンバーターは次のとおりです。

@Named
public class userConverter implements Converter{

    @Inject
    private PlannedActivityController activityController;

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
        for (UserBean user : activityController.getUsers()) {
            if(user.getId().toString().equals(s)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
        if(o instanceof UserBean) {
            UserBean user = (UserBean)o;
            return user.getId().toString();
        }
        return "";
    }
}
4

1 に答える 1