Problem: searching for some more suitable design .
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.
my bean
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;
/**
* <p>Type: UserSession
* <p>User: kislo_metal
* <p>Date: Jun 21, 2010
* <p>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<Long, Boolean> 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<Long, Boolean> boolHashmap) {
this.boolHashmap = boolHashmap;
}
public void initBolHashMap() {
this.boolHashmap = new HashMap<Long, Boolean>();
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);
}
}
and page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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"
>
<body>
<cc:interface>
</cc:interface>
<cc:implementation>
#{i18n.user_managment_title}
<h:form>
<!--<f:ajax render="@form" execute="@form">-->
<h:dataTable id="userTable" var="puser" value="#{userTable.uList}" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Login"/>
</f:facet>
#{puser.login}
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="password"/>
</f:facet>
<h:panelGroup>
<!--#{puser.password} -->
<h:commandButton id="chps" value="#{i18n.chpsqd_button}" rendered="#{!userSession.getPWRender(puser.id)}"
action="#{userSession.setPWrenerTrue(puser.id)}">
<f:ajax render="pswd chps"/>
</h:commandButton>
<h:inputSecret id="pswd" rendered="#{userSession.getPWRender(puser.id)}" value="some"/>
<!--<h:inputSecret id="chpaswd" value="#{managePUser.password}">-->
<!--<f:ajax event="click" render="chpaswd"/>-->
<!--</h:inputSecret>-->
<!--<h:commandButton value="#{i18n.submit_button}">-->
<!--</h:commandButton>-->
</h:panelGroup>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="pId"/>
</f:facet>
#{puser.partnerid}
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="groupName"/>
</f:facet>
#{puser.groupName}
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="isActive"/>
</f:facet>
#{puser.userStatusid==2}
</h:column>
<h:column>
<h:commandButton value="#{i18n.delete_button}" action="#{managePUser.removeEntity(puser)}"
disabled="#{puser.groupName.equals('admin')}">
<f:ajax render="@form"/>
</h:commandButton>
</h:column>
<!--<h:column>-->
<!---->
<!--<h:commandButton value="#{i18n.edit_button}" action="#{managePUser.removeEntity(puser)}"/>-->
<!--</h:column>-->
<f:facet name="footer">
<!--<f:ajax render="footeradd userTable">-->
<h:panelGroup id="footeradd">
<h:panelGroup layout="block" rendered="#{!managePUser.padd}">
<h:commandButton value="#{i18n.user_manager_add_user}"
action="#{managePUser.setEnablepadd}">
<f:ajax render="footeradd"/>
</h:commandButton>
</h:panelGroup>
<h:panelGroup id="adduser" rendered="#{managePUser.padd}" layout="block">
<h:panelGrid columns="2">
<h:outputText value="#{i18n.username}"/>
<h:inputText id="username" value="#{managePUser.login}"/>
<h:outputText value="#{i18n.password}"/>
<h:inputSecret id="passowrd" value="#{managePUser.password}"/>
<h:commandButton value="#{i18n.cancel_button}"
action="#{managePUser.setDisblepadd}">
<f:ajax render="footeradd"/>
</h:commandButton>
<h:commandButton value="#{i18n.submit_button}" action="#{managePUser.submit}">
<f:ajax render="footeradd userTable" execute="@this username passowrd"/>
</h:commandButton>
开发者_高级运维 </h:panelGrid>
</h:panelGroup>
</h:panelGroup>
<!--</f:ajax>-->
</f:facet>
</h:dataTable>
<!--</f:ajax>-->
</h:form>
</cc:implementation>
</body>
</html>
so I using my bean in column at :
<h:commandButton id="chps" value="#{i18n.chpsqd_button}" rendered="#{!userSession.getPWRender(puser.id)}"
action="#{userSession.setPWrenerTrue(puser.id)}">
<f:ajax render="pswd chps"/>
</h:commandButton>
Question 2: what could be a better design for this situation ?
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» ?
Thank you!
Change your scope to ViewScope, there is no need to keep UserSession class in SessionScope.
精彩评论