<h:datatable>和SessionScoped bean </h:datatable>

时间:2010-07-30 09:46:26

标签: java jsf jsf-2 cdi session-scope

问题:寻找更合适的设计。

目标:我希望在每个用户的列中都有一些用户信息表和按钮“更改密码”,点击后:“更改密码”按钮消失(呈现=“假”)并出现inputSecreat字段 - 用户输入密码并提交密码(通过单击其他按钮或热键)。提交后 - “更改密码”按钮如前所述。

我的豆子

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);

    }
}

和页面:

<!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>

所以我在列中使用我的bean:

                    <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>

问题2:对于这种情况,什么可能是更好的设计?

问题3:是否可以在某些组件(在我的情况下为inputSecret)中监听某些事件(onclick,ondbclick在组件标记内),如果它被视为禁用=«true»?

谢谢!

1 个答案:

答案 0 :(得分:0)

将范围更改为ViewScope,无需在SessionScope中保留UserSession类。