JSF2.0中的会话范围

JSF2.0中的会话范围,jsf,Jsf,我在JSF2.0中有一个简单的应用程序。我在会话作用域中创建了ManagedBean。在此ManagedBean中,我存储了一个属性。但是,当我试图从jsp访问该属性时,它没有被检索到。我存储的是roleType的值。 下面是ManagedBean中的方法 public class LoginBean { private String userID; private List<String> roleNames; private String roleTy

我在JSF2.0中有一个简单的应用程序。我在会话作用域中创建了ManagedBean。在此ManagedBean中,我存储了一个属性。但是,当我试图从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>
公共类登录域{
私有字符串用户标识;
私人名单;
私有字符串角色类型;
公共字符串getUserID(){
返回用户标识;
}
public void setUserID(字符串userID){
this.userID=userID;
}
公共列表getRoleNames(){
返回角色名称;
}
公共无效设置角色名称(列出角色名称){
this.roleNames=roleNames;
}
公共字符串getRoleType(){
返回角色类型;
}
public void setRoleType(字符串roleType){
this.roleType=roleType;
}
公共字符串createAuth()引发SQLException
{
authorization authCall=com.validation.client.authorization.concern.authorizationcall.createauthorization(userID);

如果(authCall.getUserRoles()==null | | authCall.getUserRoles().size()如注释所示,请确保
roleType
没有返回
null
,可能
的计算结果为
false
(或者您没有获得值
审批人
),但也会更改

<h:outputText value="loginBean.roleType"/>
更好的是,尽量不要使用硬编码的
字符串,在对象中创建一个单独的函数,比如

private boolean approver; 

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

//Setter omitted 
然后像这样修改

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


在JSP中访问托管bean不是一个好的做法。这是要求还是您想尝试一下?@erencan您在哪里读到的?我想这是最常见的做法。可能您的
roleType
属性仍然为
null
因为尚未分配值。您在哪里调用
createAuth()
method?对属性的访问必须是EL表达式:
{loginBean.roleType}
@XtremeBiker首先,你应该避免JSP中的Scriptlet,这是访问JSP中托管bean的方式。错用JSP中的MVC模式,它们有不同的生命周期。如果你查看链接,你会注意到jstl是从JSP访问托管bean的另一种方式,这是OP在其示例中所写的方式。事实上这个问题本身与Scriptlet无关。
<h:outputText value="#{loginBean.roleType}" rendered="#{loginBean.roleType != 'APPROVER'}"/>
private boolean approver; 

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

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