Spring LdapTemplate读取 + Freemark简单合成页面

  • 客户近期频繁要求修改组织机构,维护部门苦不堪言。更新组织机构就要更新邮箱的通讯录,使用的应该是某流行的邮件系统,php的,版本挺老的,其中有个功能,在写收件人的时候输入的东西会autocomplate,这个东西维护部门需要维护一个超大的文件夹,如下图:一级目录是第一个字,二级目录是前两个字,三级目录是前三个字,以此类推,其中每个文件夹下包含一个result.php文件,内容为该级目录中所对应的全部人员。


  • 那我需要做什么呢?对,帮助他们生成这个东西。缕了一下思路,需要用到ldap、freemark的知识。
-------------------------------------------------------------------------------------------------------------
  • Spring大家应该不陌生,这里ldap数据的读取,我选用的是Spring的ldapTemplate 1.3.1,配置如jdbcTemplate:
	<bean id="contextSource"
		class="org.springframework.ldap.core.support.LdapContextSource">
		<property name="url" value="ldap://192.168.101.179:389" />
		<property name="base" value="" />
		<property name="userDn" value="cn=platoptuser,ou=内置帐号,ou=系统,o=北京XX局" />
		<property name="password" value="111111" />
	</bean>
	<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
		<constructor-arg ref="contextSource" />
	</bean>
  • 读取Ldap这块我遇到一个难点,读取出来人如何得到这个人属于哪个组织机构,网上搜索无果,通过查看包里的类找靠谱的方法,最后终于找到了ContextMapper,可以通过获取dn来取得组织机构
	public List<Person> getAllPersonNames() {
		// 正则表达式,为获取组织机构
		String regEx_script = "ou=([^,]*)?";
		final Pattern p = Pattern.compile(regEx_script, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);// 修改模式
		
		@SuppressWarnings("unchecked")
		List<Person> persons = ldapTemplate.search("o=北京XX局", "(objectClass=Person)",
				new ContextMapper() {
					@Override
					public Object mapFromContext(Object arg0) {
						DirContextAdapter adapter = (DirContextAdapter)arg0;
						Attributes attrs = adapter.getAttributes();
						Person bean = new Person();
						// 取值
						Attribute fullName = attrs.get("cn");
						Attribute mail = attrs.get("mail");
						Attribute title = attrs.get("title");
						Name dn = adapter.getDn();
						String org = "";
						// 取组织机构
						if(dn != null){
							Matcher m = p.matcher(dn.toString());
							MatchResult mr = null;
							// 获取匹配
							while (m.find()) {
								mr = m.toMatchResult();
								org = org + mr.group(1) + "/";
							}
						}
						// 给对象赋值
						try {
							bean.setFullName(fullName == null ? "" : fullName.get().toString());
							// ldap中有的邮箱有后缀有的没有
							String mailStr = mail == null ? "" : mail.get().toString();

							bean.setMail(mailStr);
							bean.setTitle(title == null ? "" : title.get().toString());
							bean.setOrg(org.equals("") ? "" : org.substring(0, org.length() - 1));
						} catch (NamingException e) {
							e.printStackTrace();
						}
						return bean;
					}
				});
		return persons;
	}
  • 使用Freemark生成result.php也遇到一个问题,打包成jar后,使用命令行方式输入java -jar xxx.jar后,提示找不到模板,原先使用的是setDirectoryForTemplateLoading需要修改为:
setTemplateLoader(new ClassTemplateLoader(this.getClass(), "/ftl"));
  • 最后的Freemark代码是这样的
public class TestFreemarker {

    private Configuration cfg;

    public Configuration getCfg() {
        return cfg;
    }

    public void init() throws Exception {
        cfg = new Configuration();
        cfg.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "/ftl"));
    }

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
    	TestFreemarker obj = new TestFreemarker();
        obj.init();
        
        List<Person> persons = new ArrayList<Person>();
        Person user = new Person();
        user.setFullName("陈小二");
        user.setMail("yueritian@sohu.com");
        user.setOrg("XX公司/集团");
        user.setTitle("程序员");
        persons.add(user);
        
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("persons", persons);
        
        Template t = obj.getCfg().getTemplate("result.ftl");
        Writer out = new OutputStreamWriter(new FileOutputStream("result.php"), "GBK");
        t.process(root, out);
        out.flush();
        out.close();
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值