Java 和SessionScoped bean

Java 和SessionScoped bean,java,jsf,jsf-2,cdi,session-scope,Java,Jsf,Jsf 2,Cdi,Session Scope,问题:寻找更合适的设计 目标:我希望有一些用户信息的表格,在单击:change password button Redered=false并出现inputSecreat字段后,每个用户的列中都有按钮change password,用户输入密码并通过单击其他按钮或热键提交。提交后-更改密码按钮,如前所示 我的豆子 import javax.annotation.PostConstruct; import javax.ejb.EJB; import javax.faces.bean.SessionS

问题:寻找更合适的设计

目标:我希望有一些用户信息的表格,在单击:change password button Redered=false并出现inputSecreat字段后,每个用户的列中都有按钮change password,用户输入密码并通过单击其他按钮或热键提交。提交后-更改密码按钮,如前所示

我的豆子

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被设置为disabled=«true»,是否可以在组件标记内侦听某些事件onclick,ondbclick


谢谢大家!

将您的作用域更改为ViewScope,无需在SessionScope中保留UserSession类。

什么是o点事件?:我使用的是cdibean,而不是@ManagedBean。对于CDIBean来说,@viewscope只有很少的手工实现,据我所知,@ManagedBean范围不适用于CDIBean。