请在下面找到我的代码段:
String[] attrIDs = {"title", "Depatrment", "DivisionDescription" };
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> answer = ctx.search(ldapServerSearchBase, FILTER, ctls);
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
String str_title = attrs.get("title").toString();
String str_dept = null;
String str_desc = null;
if(str_title.equals("Professor"))
{
str_dept = attrs.get("Depatrment").toString();
str_desc = attrs.get("DivisionDescription").toString();
}
System.out.println(str_title);
System.out.println(str_dept);
System.out.println(str_desc);
当我运行此代码时,当我输出str_dept和str_desc时,它总是显示为null。但它确实正确显示str_title为&#34;教授&#34;。 请帮助我理解这里可能出现的问题。
谢谢!
答案 0 :(得分:1)
您确定要获取的属性是否存在?
str_dept = attrs.get("Depatrment").toString();
str_desc = attrs.get("DivisionDescription").toString();
首先,Depatrment
拼错了,DivisionDescription
很可能不存在(至少在大多数目录服务的默认原理图中都没有)。您可能需要以下属性吗?:
DivisionDescription
=&gt; division
Depatrment
=&gt; department
但是,请检查您的目录服务是否实际上拥有所请求的属性(并且用户实际上在这些属性中有一些值)。
答案 1 :(得分:-1)
虽然我确信,如果提供了更多信息,有更好的方法可以做到这一点,下面的示例应该可以帮助您:
String[] attrIDs = {"title", "Depatrment", "DivisionDescription" };
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> answer = ctx.search(ldapServerSearchBase, filter, ctls);
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
String str_title=null;
String str_dept = null;
String str_desc = null;
if( attrs.get("title")!=null)
{
str_title = attrs.get("title").toString();
System.out.println(str_title);
if(str_title.equals("Professor"))
{
str_dept = attrs.get("Depatrment").toString();
str_desc = attrs.get("DivisionDescription").toString();
System.out.println(str_dept);
System.out.println(str_desc);
}
}
-Jim