使用带有JSF <selectitems>标签</selectitems>的Apache配置时遇到问题

时间:2012-10-15 16:00:32

标签: java jsf properties

我正在开展一个项目,我对JSF不太熟悉,所以请纠正这个问题的任何空白。

我有一个属性文件,其中包含域的值...例如

domain=.com
domain=.net

在我的Bean中我有这个

private String domain;
private String[] domainSelection;

public void initProp(){

   try {
      Configuration config = new PropertiesConfiguration("prop.properties");
      domainSelection = config.getStringArray("domain");

   } catch (ConfigurationException e) {
      Log.error("Error");
   }

}

在我拥有JSF的.jsp页面中

<rich:select id="domain" value="#{Bean.domain}"                                               
      required="true">
     <f:selectItems itemValue="#{Bean.domainSelection}" />
</rich:select>

当我调试这个时,我在domainSelection中获得了两个值,但我需要将它们传递给JSF,我无法弄清楚如何做到这一点。

1 个答案:

答案 0 :(得分:1)

对不起,初步回答我完全错过了这个问题。

private List<SelectItem> domains = new ArrayList<SelectItem>();
//for each domain
domains.add("com",firstFromDomainSelection);
domains.add("net",secondFromDomainSelection);

<f:selectItems value="#{Bean.domains}" />

因此,这需要getDomains来检索它们。

修改

只要您再次阅读属性文件,我相信它是可行的。要记住的一件事是文件可能已经在.war中,因此您将不得不想办法重新添加或只是将其添加到已部署的文件夹中。

每当视图想要获取列表时,它将调用getDomains(),这意味着我们应该在那里使用逻辑来调用它所谓的时间。由于文件IO,可能会有轻微的性能损失。

private List<SelectItem> domains;
private Configuration config = new PropertiesConfiguration("prop.properties"); // with accessors

public List<SelectItem> getDomains(){
  domains = new ArrayList<SelectItem>();
  String[]  domainSelection = getConfig().getStringArray("domain");
  for(String domain : domainSelection ){
     //Define desired logic for the value if its the same (.com) pass the same as value
     domains.add( new SelectItem(domain ,domain)); // SelectItem(value, label);
  }
  return domains;
}

我会做什么

我不会使用属性文件,而是使用表的域,只是动态地将这些记录添加到表中,然后相应地检索它们。当该视图上有许多请求时,它可能会减慢速度 - 至少是轻微的。要记住的另一个问题是apache缓存这些文件。要时刻铭记在心。使用db表是更安全的IMO。