i would like to display all my user roles als checkboxes to assign the available roles to one user. Therefore i created a page called userDetail.xhtml to edit the users.
<h:form id="form" styleClass="dialog_content">
<p:panelGrid columns="4" styleClass="noBorders">
<p:outputLabel value="Username"/>
<p:inputText id="input_username" value="#{userPM.userName}"/>
<p:outputLabel value=""/>
<p:message for="input_username"/>
<p:outputLabel value="Vorname"/>
<p:inputText value="#{userPM.vorname}"/>
<p:outputLabel value="Nachname"/>
<p:inputText value="#{userPM.nachname}"/>
<p:outputLabel value="Rollen"/>
<p:selectManyCheckbox value="#{userPM.userRoles}" converter="roleConverter">
<f:selectItems value="#{rolePM.getAllRoles()}" var="role" itemLabel="#{role.bezeichnung}" itemValue="#{role}" />
</p:selectManyCheckbox>
<p:outputLabel />
<p:outputLabel />
<p:outputLabel value="Aktiv"/>
<p:selectBooleanCheckbox value="#{userPM.active}"/>
<h:outputText value="not selected" rendered="#{!userPM.active}"/>
<p:outputLabel value=""/>
</p:panelGrid>
<p:separator/>
<p:panelGrid style="width: 100%" styleClass="noBorders">
<p:row>
<p:column style="text-align: left;">
<p:commandButton icon="ui-icon-disk" value="Speichern" action="#{userPM.editUser()}"/>
</p:column>
<p:column style="text-align: right;">
<p:commandButton icon="ui-icon-closethick" value="Abbrechen" actionListener="#{userPMSuche.abortEditUser()}"/>
</p:column>
</p:row>
</p:panelGrid>
</h:form>
To handle the edit page, i created the following model.
The lastUser object will contain the user wich should be edited.
@ManagedBean
@SessionScoped
public class UserPM implements Serializable {
private static final long serialVersionUID = -5604907919976613472L;
private String userName;
private String vorname;
private String nachname;
private boolean active;
private List<Role> userRoles = new ArrayList<Role>();
private User lastUser;
@EJB
UserService userService;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public String getNachname() {
return nachname;
}
public List<Role> getUserRoles() {
return this.userRoles;
}
public void setNachname(String nachname) {
this.nachname = nachname;
}
public boolean getActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public void setLastUser(User user) {
this.lastUser = user;
}
public void setUserRoles(List<Role> userRoles) {
this.userRoles = userRoles;
}
public User getLastUser() {
return this.lastUser;
}
public void editUser() {
lastUser.setUsername(userName);
lastUser.setVorname(vorname);
lastUser.setNachname(nachname);
lastUser.setUser_roles(userRoles);
lastUser.setActive(active);
userService.mergeUser(lastUser);
RequestContext.getCurrentInstance().closeDialog("userDetail");
}
}
I also created a converter for the roles:
@FacesConverter("roleConverter")
public class RoleConverter implements Converter {
@EJB
private RoleService roleService;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String string) {
return roleService.getRoleByID(Integer.parseInt(string));
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
if (object instanceof Role) {
return String.valueOf(((Role) object).getId());
} else {
return "";
}
}
}
All the possible roles are displayed correctly. The getAsString() method is called on page load and the getAsObject() method is called when clicking the Save-Button (= Speichern).
But the editUser() method, wich is set as action behind the Save-Button will not be invoked after the getAsObject() method from the converter.
Am i right, that the editUser() method should be called after the converter by clicking the Save-Button? I also tried it with actionListener instead of action.
There is no exception printed on the console of my glassfish! The button simply not calls the action.
The aim is to select the roles for a specified user by clicking the checkboxes.
Thanks for your help!