1

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.

Ayan Biswas
  • 1,641
  • 9
  • 39
  • 66
  • it is not a good practice accessing managed bean inside JSPs. is that the requirement or do you want to give a try? – erencan Jul 13 '13 at 10:17
  • 1
    @erencan where did you read that? I'd say is the most common practice. – Aritz Jul 13 '13 at 11:43
  • Probably your `roleType` property remains `null` cause not value has been yet assigned. Where are you calling `createAuth()` method? Also the access to the property must be an EL expression: `#{loginBean.roleType}` – Aritz Jul 13 '13 at 11:46
  • @XtremeBiker first of all, you should avoid scriptlets inside JSPs, which is the way of accessing managed beans in JSPs. http://stackoverflow.com/a/3180202/892994. Mising MVC Pattern in JSP and they have diffrent life cycles. – erencan Jul 13 '13 at 12:54
  • If you look at your link you'll notice that jstl is other way to access managed beans from jsp, which is by the way what the OP wrote in its example. In fact this question itself is not related with scriptlets. – Aritz Jul 13 '13 at 14:01
  • Use JSF instead of JSP. Use annotation instead of Configuration files. – Buddhika Ariyaratne Jul 14 '13 at 14:39

1 Answers1

0

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}/>
Andy
  • 5,900
  • 2
  • 20
  • 29