簡體   English   中英

JSF 2.0中的SessionScope

[英]SessionScope in JSF 2.0

我在JSF2.0中有一個簡單的應用程序。 我在Session范圍內創建ManagedBean的位置。在此托管bean中,我存儲了一個屬性,但是當我嘗試從jsp訪問該屬性時,卻無法檢索到它。我正在存儲roleType的值。 這是ManagedBean內部的方法

public class LoginBean {

    private String userID;
    private List<String> roleNames; 
    private String roleType;


    public String getUserID() {
        return userID;
    }
    public void setUserID(String userID) {
        this.userID = userID;
    }

    public List<String> getRoleNames() {
        return roleNames;
    }
    public void setRoleNames(List<String> roleNames) {
        this.roleNames = roleNames;
    }

    public String getRoleType() {
        return roleType;
    }
    public void setRoleType(String roleType) {
        this.roleType = roleType;
    }
    public String createAuth() throws SQLException
    {
        Authorisation authCall=com.validation.client.authorization.concern.AuthorisationCall.createAuthorisation(userID);
        if(authCall.getUserRoles()==null || authCall.getUserRoles().size()<=0){
            return "login";
        }
        List<String> roleNameList=new ArrayList<String>(); 
        Iterator itr =authCall.getUserRoles().iterator();
        String roleType=null;
        while (itr.hasNext()){
            UserRole userRole=(UserRole)itr.next();
            roleNameList.add(userRole.getRoleName());   
            roleType=userRole.getRoleDescription();
        }
        setRoleNames(roleNameList);
        setRoleType(roleType);
   }
}

 The jsp page where I am retrieving the property is :

<c:if test="${loginBean.roleType=='APPROVER'}">
<h:outputText value="loginBean.roleType"/>
<c:if>

在faces-config.xml中,我這樣做是為了注冊bean

<managed-bean>
        <description>temporary authentication</description>
        <managed-bean-name>loginBean</managed-bean-name>
        <managed-bean-class>com.validation.client.authorization.concern.LoginBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

這是我在JSF中的首次嘗試,並且對此深有感觸。

就像評論中所說的那樣,請確保roleType不會返回null也許<c:if>的計算結果為false (或者您沒有獲得APPROVER的值,但還會更改

<h:outputText value="loginBean.roleType"/>

到EL表達式

<h:outputText value="#{loginBean.roleType}"/>

否則您將無法獲得預期的輸出。 還考慮完全放棄JSTL,並使用rendered屬性作為條件輸出(不要混合使用JSTL和JSF,否則以后可能會遇到一些奇怪的問題)

<h:outputText value="#{loginBean.roleType}" rendered="#{loginBean.roleType != 'APPROVER'}"/>

更好的是,嘗試不要使用硬編碼的String並在對象中創建單獨的函數,例如

private boolean approver; 

public boolean isApprover() {
    return approver; //Evaluate this condition wherever you think is appropriate
}

//Setter omitted 

並像這樣修改<h:output>

<h:outputText value="#{loginBean.roleType}" rendered = #{loginBean.approver}/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM