Note that there are some explanatory texts on larger screens.

plurals
  1. PO<h:dataTable> and SessionScoped beans
    text
    copied!<p>Problem: searching for some more suitable design . </p> <p>Goal: I want to have table of some user information and with button "change password" in column for each user, after click : "change password" button disappeared (rendered ="false") and a inputSecreat field appeared - user entering the password and submitting it (by clicking some additional button or by hotkey ). after submit - "change password" button papered as before.</p> <p>my bean</p> <pre><code>import javax.annotation.PostConstruct; import javax.ejb.EJB; import javax.faces.bean.SessionScoped; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.inject.Inject; import javax.inject.Named; import javax.servlet.http.HttpServletRequest; import java.io.Serializable; import java.util.HashMap; /** * &lt;p&gt;Type: UserSession * &lt;p&gt;User: kislo_metal * &lt;p&gt;Date: Jun 21, 2010 * &lt;p&gt;Time: 4:41:57 PM */ @Named @SessionScoped public class UserSession implements Serializable { private String name; private String role; @Inject private org.slf4j.Logger log; @EJB private PersistenceService pservice; @Inject private RulesBean rules; private HashMap&lt;Long, Boolean&gt; boolHashmap; public UserSession() { } @PostConstruct public void postConstruct() { log.info("session init "); } public String getName() { if (name == null) getUserData(); return name == null ? "" : name; } public String getRole() { return role == null ? "" : role; } public void setRole(String newValue) { role = newValue; } public boolean isInRole() { ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); Object requestObject = context.getRequest(); if (!(requestObject instanceof HttpServletRequest)) { log.info("request object has type " + requestObject.getClass()); return false; } HttpServletRequest request = (HttpServletRequest) requestObject; return request.isUserInRole(role); } private void getUserData() { ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); Object requestObject = context.getRequest(); if (!(requestObject instanceof HttpServletRequest)) { log.info("request object has type " + requestObject.getClass()); return; } HttpServletRequest request = (HttpServletRequest) requestObject; name = request.getRemoteUser(); } public HashMap getBoolHashmap() { if (boolHashmap == null) initBolHashMap(); return boolHashmap; } public void setBoolHashmap(HashMap&lt;Long, Boolean&gt; boolHashmap) { this.boolHashmap = boolHashmap; } public void initBolHashMap() { this.boolHashmap = new HashMap&lt;Long, Boolean&gt;(); System.out.println(""); log.info("init map.."); log.info("loding list"); log.warn("user name = " + rules.getUserPrincipalName()); for (OasysUserEntity user : pservice.getUsersList(pservice.getUserByLogin(rules.getUserPrincipalName()).getPartnerid())) { boolHashmap.put(user.getId(), false); log.info("map =" + boolHashmap); } log.info("loaded list"); } public boolean getPWRender(long id) { if (boolHashmap == null) initBolHashMap(); //TODO if it null - exception will be thrown try { log.info("getting id =" + id); log.info("map is =" + boolHashmap); log.info("getting result = =" + boolHashmap.get(id)); log.info("getting result = =" + boolHashmap.get(Long.valueOf(id))); return boolHashmap.get(Long.valueOf(id)); } catch (NullPointerException e) { e.printStackTrace(); return false; } } public void setPWrenerTrue(long id) { if (boolHashmap == null) initBolHashMap(); log.info("setPWrenerTrue id =" + id); boolHashmap.put(id, true); log.info("setPWrenerTrue map is =" + boolHashmap); } } </code></pre> <p>and page:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:cc="http://java.sun.com/jsf/composite" xmlns:p="http://primefaces.prime.com.tr/ui" &gt; &lt;body&gt; &lt;cc:interface&gt; &lt;/cc:interface&gt; &lt;cc:implementation&gt; #{i18n.user_managment_title} &lt;h:form&gt; &lt;!--&lt;f:ajax render="@form" execute="@form"&gt;--&gt; &lt;h:dataTable id="userTable" var="puser" value="#{userTable.uList}" border="1"&gt; &lt;h:column&gt; &lt;f:facet name="header"&gt; &lt;h:outputText value="Login"/&gt; &lt;/f:facet&gt; #{puser.login} &lt;/h:column&gt; &lt;h:column&gt; &lt;f:facet name="header"&gt; &lt;h:outputText value="password"/&gt; &lt;/f:facet&gt; &lt;h:panelGroup&gt; &lt;!--#{puser.password} --&gt; &lt;h:commandButton id="chps" value="#{i18n.chpsqd_button}" rendered="#{!userSession.getPWRender(puser.id)}" action="#{userSession.setPWrenerTrue(puser.id)}"&gt; &lt;f:ajax render="pswd chps"/&gt; &lt;/h:commandButton&gt; &lt;h:inputSecret id="pswd" rendered="#{userSession.getPWRender(puser.id)}" value="some"/&gt; &lt;!--&lt;h:inputSecret id="chpaswd" value="#{managePUser.password}"&gt;--&gt; &lt;!--&lt;f:ajax event="click" render="chpaswd"/&gt;--&gt; &lt;!--&lt;/h:inputSecret&gt;--&gt; &lt;!--&lt;h:commandButton value="#{i18n.submit_button}"&gt;--&gt; &lt;!--&lt;/h:commandButton&gt;--&gt; &lt;/h:panelGroup&gt; &lt;/h:column&gt; &lt;h:column&gt; &lt;f:facet name="header"&gt; &lt;h:outputText value="pId"/&gt; &lt;/f:facet&gt; #{puser.partnerid} &lt;/h:column&gt; &lt;h:column&gt; &lt;f:facet name="header"&gt; &lt;h:outputText value="groupName"/&gt; &lt;/f:facet&gt; #{puser.groupName} &lt;/h:column&gt; &lt;h:column&gt; &lt;f:facet name="header"&gt; &lt;h:outputText value="isActive"/&gt; &lt;/f:facet&gt; #{puser.userStatusid==2} &lt;/h:column&gt; &lt;h:column&gt; &lt;h:commandButton value="#{i18n.delete_button}" action="#{managePUser.removeEntity(puser)}" disabled="#{puser.groupName.equals('admin')}"&gt; &lt;f:ajax render="@form"/&gt; &lt;/h:commandButton&gt; &lt;/h:column&gt; &lt;!--&lt;h:column&gt;--&gt; &lt;!----&gt; &lt;!--&lt;h:commandButton value="#{i18n.edit_button}" action="#{managePUser.removeEntity(puser)}"/&gt;--&gt; &lt;!--&lt;/h:column&gt;--&gt; &lt;f:facet name="footer"&gt; &lt;!--&lt;f:ajax render="footeradd userTable"&gt;--&gt; &lt;h:panelGroup id="footeradd"&gt; &lt;h:panelGroup layout="block" rendered="#{!managePUser.padd}"&gt; &lt;h:commandButton value="#{i18n.user_manager_add_user}" action="#{managePUser.setEnablepadd}"&gt; &lt;f:ajax render="footeradd"/&gt; &lt;/h:commandButton&gt; &lt;/h:panelGroup&gt; &lt;h:panelGroup id="adduser" rendered="#{managePUser.padd}" layout="block"&gt; &lt;h:panelGrid columns="2"&gt; &lt;h:outputText value="#{i18n.username}"/&gt; &lt;h:inputText id="username" value="#{managePUser.login}"/&gt; &lt;h:outputText value="#{i18n.password}"/&gt; &lt;h:inputSecret id="passowrd" value="#{managePUser.password}"/&gt; &lt;h:commandButton value="#{i18n.cancel_button}" action="#{managePUser.setDisblepadd}"&gt; &lt;f:ajax render="footeradd"/&gt; &lt;/h:commandButton&gt; &lt;h:commandButton value="#{i18n.submit_button}" action="#{managePUser.submit}"&gt; &lt;f:ajax render="footeradd userTable" execute="@this username passowrd"/&gt; &lt;/h:commandButton&gt; &lt;/h:panelGrid&gt; &lt;/h:panelGroup&gt; &lt;/h:panelGroup&gt; &lt;!--&lt;/f:ajax&gt;--&gt; &lt;/f:facet&gt; &lt;/h:dataTable&gt; &lt;!--&lt;/f:ajax&gt;--&gt; &lt;/h:form&gt; &lt;/cc:implementation&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>so I using my bean in column at :</p> <pre><code> &lt;h:commandButton id="chps" value="#{i18n.chpsqd_button}" rendered="#{!userSession.getPWRender(puser.id)}" action="#{userSession.setPWrenerTrue(puser.id)}"&gt; &lt;f:ajax render="pswd chps"/&gt; &lt;/h:commandButton&gt; </code></pre> <p>Question 2: what could be a better design for this situation ?</p> <p>Question 3: Is it possible to listen to some events (onclick, ondbclick inside component tag ) for some component (inputSecret in my situation) if it steed as disabled=«true» ?</p> <p>Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload