0

I'm starting my first steps in JSF.

I've already read this link

http://docs.oracle.com/javaee/6/tutorial/doc/bnawq.html#bnaww

in regards to map initialization.

The problem is, I want to populate my map with values residing in a file.

How can I do that?

I've tried not using faces-config.xml and calling a support method in the bean's constructor, but my select list box isn't populated.

My bean class:

@ManagedBean
public class ADGroupListBean {

private static final String WITH_ACCESS = "D:\\workspace\\AccessControl\\permissions.txt";
private static final String WITHOUT_ACCESS = "D:\\workspace\\AccessControl\\noPermissions.txt";
private Map<String,String> withAccess, withoutAccess;

private LDAPQueries queries;


public ADGroupListBean(){

    withAccess = new LinkedHashMap<String, String>();
    withoutAccess = new LinkedHashMap<String, String>();

    queries = new LDAPQueries();

    initList(WITH_ACCESS, withAccess);
    initList(WITHOUT_ACCESS, withoutAccess);

}   

private void initList(String filename, Map<String,String> list) {

    File f = new File(filename);

    if ( !f.exists() && f.getAbsolutePath().equals(WITHOUT_ACCESS) )
    {
        queries.queryAllGroups(WITHOUT_ACCESS);
    }

    try 
    {   

        Scanner sc = new Scanner(f);

        while (sc.hasNext())
        {
            String group = sc.nextLine();
            list.put(group, group);
        }

    }catch (IOException e) {

        e.printStackTrace();
    }


}

//  public void populateList() {
//      
//      
//  }

public Map<String,String> getWithAccess() {

    return withAccess;
}

public Map<String,String> getWithoutAccess() {

    return withoutAccess;
}

public void setWithoutAccess(Map<String,String> withoutAccess) {

    this.withoutAccess = withoutAccess;
}

public void setwithAccess(Map<String,String> withAccess) {

    this.withAccess = withAccess;
}

public void test() {

    System.out.println("workssssssssssssssssss");
}
}

As for my JSF file, it is like this:

<!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:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html">
<h:head><title>Your Title Here</title>
</h:head>
<h:body>
<h1>Your Heading Here</h1>

<h:form>
    <h:selectOneMenu value="teste">
         <f:selectItem itemLabel="" itemValue="" />
        <f:selectItems value="#{ADGroupListBean.withoutAccess}" />
</h:selectOneMenu>
</h:form>

</h:body>
</html>

I've tested the bean's functions in a test application, and everything works fine.

So my guess is the bean isn't instantiated?

Regards, Nuno.

nmmsantos
  • 315
  • 1
  • 6
  • 15
  • *So my guess is the bean isn't instantiated?* Why making assumptions if you're currently the only one in the position who can tell that? What does your debugger and/or logging and/or poor man's `System.out.println()` statements say? Check if the constructor is indeed invoked and check if the map is indeed properly populated and check if the getter method is indeed invoked. It's after all *your own* code so debugging should be very easy. – BalusC Jun 27 '12 at 16:28
  • I've tried doing a System.out.println() in the constructor, but nothing shows on the console, no errors. The select list simply appears empty. – nmmsantos Jun 28 '12 at 08:53
  • Found the error. I had the bean name was wrong. It should be aDGroupListBean :) – nmmsantos Jun 28 '12 at 09:00

0 Answers0