简体   繁体   中英

SessionScope in JSF 2.0

I have a simple application in JSF2.0 . Where I have created a ManagedBean in Session scope .In this managed bean I have stored a property.But when I tried to access that property from a jsp it is not getting retrieved.I am storing the value of roleType. Here is the method inside the 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>

In faces-config.xml I have done this to register the 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>

This is my first attempt in JSF and am really stuck with this.

Like the comments say, make sure roleType is not returning null maybe the <c:if> is evaluating to false (or you're not getting the value APPROVER but also change

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

to an EL expression

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

or else you will not get the expected output. Consider also ditching JSTL altogether and use the rendered attribute for your conditional output (don't mix JSTL and JSF or you might run into some weird problems later)

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

better yet, try not to have a hard coded String and make a separate function in your object, something like

private boolean approver; 

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

//Setter omitted 

and modify <h:output> like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM