0

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!

  • Simply means conversion failed. Put a `` on your page (or look in your browser's javascript console) to confirm – kolossus Apr 26 '14 at 06:21
  • Thanks for your answer colossus, but that also didn't solve my problem. I've checked these before and and also now, but there will not be printed a message on my page and also not in the console of my browser. The above page will be included in a dialog with ajax.. – HeneryHawk Apr 28 '14 at 06:06

1 Answers1

0

You do not need to create your own converter.

If you want a generic converter then Omnifaces SelectItemsConverter is the best in all.

Omni Faces Converter

If you don't want to use Omnifaces then use below generic converter

import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.WeakHashMap;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "entityConverter")
public class EntityConverter implements Converter {

    private static Map<Object, String> entities = new WeakHashMap<Object, String>();

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object entity) {
        synchronized (entities) {
            if (!entities.containsKey(entity)) {
                String uuid = UUID.randomUUID().toString();
                entities.put(entity, uuid);
                return uuid;
            } else {
                return entities.get(entity);
            }
        }
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
        for (Entry<Object, String> entry : entities.entrySet()) {
            if (entry.getValue().equals(uuid)) {
                return entry.getKey();
            }
        }
        return null;
    }

}
Makky
  • 17,117
  • 17
  • 63
  • 86
  • Thanks for your answer Makky, but that didn't solve my problem. I use now `Omnifaces`but when i select a item from my checkboxes, the `editUser` method to store the changes will not be invoked.. – HeneryHawk Apr 25 '14 at 20:11