Java/XPages中调用ApplicationScope存储的Hashtable的get方法问题
问题解决思路与代码示例
报错原因分析
报错The method get(String) is undefined for the type FooterLinkBean说明你从ApplicationScope中取出的gr_footerlink对象不是预期的Hashtable,而是FooterLinkBean实例——大概率是键名大小写不匹配,或者错误地实例化了Bean而非直接从Scope中获取Hashtable。
正确解决步骤
1. 确认ApplicationScope中Hashtable的存储键名
确保存入Hashtable时的键名和取出时完全一致(Xpages的Scope键名区分大小写),存入代码示例:
// 构建并填充Hashtable Hashtable<String, String> footerLinks = new Hashtable<>(); footerLinks.put("key1", "value1|value2|value3"); // 存入ApplicationScope FacesContext.getCurrentInstance().getExternalContext().getApplicationMap() .put("Gr_footerlink", footerLinks);
2. 在SessionScope托管Bean中正确获取Hashtable
不要实例化FooterLinkBean,直接从ApplicationScope中取出并强制转换类型:
// 获取ApplicationScope容器 Map<String, Object> appScope = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap(); // 取出目标Hashtable并强制转换类型 Hashtable<String, String> grFooterLink = (Hashtable<String, String>) appScope.get("Gr_footerlink"); // 安全调用get方法并处理竖线分隔字符串 if (grFooterLink != null) { String packedString = grFooterLink.get(searchKey); if (packedString != null) { // 拆分竖线分隔的字符串,注意转义| String[] components = packedString.split("\\|"); // 处理拆分后的组件逻辑 } }
3. 排查常见错误点
- 键名大小写问题:比如存入用
gr_footerlink,取出时写成Gr_footerlink,会导致取出错误的对象类型 - 空指针预防:取出后先判断Hashtable和
packedString是否为null,避免后续报错 - 不要错误实例化Bean:Hashtable是存在ApplicationScope的全局对象,直接从Scope获取即可,无需实例化其他Bean
内容的提问来源于stack exchange,提问作者user3517070
相关产品推荐
相关产品推荐

