2013-11-26 52 views
3

我儘量讓contants使用JSF 2.2,EL 3.0我的web應用程序的工作..EL 3.0:從類添加的常量ELContext和Spring管理豆

關於這個職位in stackoverflow有可能用EL 3.0做這個。像這樣:

@ManagedBean(eager=true) 
@ApplicationScoped 
public class Config { 

    @PostConstruct 
    public void init() { 
     FacesContext.getCurrentInstance().getApplication().addELContextListener(new ELContextListener() { 
      @Override 
      public void contextCreated(ELContextEvent event) { 
      event.getELContext().getImportHandler().importClass("com.example.YourConstants"); 
     } 
     }); 
    } 
} 

是否有任何示例如何讓這個工作,如果豆不是由jsf管理?

回答

1

查看Omnifaces importConstants。它應該爲你做所有的工作。

http://showcase.omnifaces.org/taghandlers/importConstants

從omnifaces例如採取...

Managed Bean的

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 

import org.omnifaces.showcase.model.ExampleEnum; 

@ManagedBean 
@RequestScoped 
public class ConstantsBean { 

    public static final String CONSTANT1 = "Constant one"; 
    public static final String CONSTANT2 = "Constant two"; 

    private ExampleEnum exampleEnum; 

    public ExampleEnum getExampleEnum() { 
     return exampleEnum; 
    } 

    public void setExampleEnum(ExampleEnum exampleEnum) { 
     this.exampleEnum = exampleEnum; 
    } 

} 

ExampleEnum

public enum ExampleEnum { 

    ONE, TWO, THREE; 

    public String getFriendlyName() { 
     return name().charAt(0) + name().substring(1).toLowerCase(); 
    } 

} 

在XHTML

使用示例
<o:importConstants type="org.omnifaces.showcase.taghandlers.ConstantsBean" /> 
<o:importConstants type="org.omnifaces.showcase.model.ExampleEnum" /> 

<h3>All constants of <code>ConstantsBean</code></h3> 
<ul> 
    <li>#{ConstantsBean.CONSTANT1}</li> 
    <li>#{ConstantsBean.CONSTANT2}</li> 
</ul> 

<hr /> 

<h3>All values of <code>ExampleEnum</code></h3> 
<ul> 
    <li>#{ExampleEnum.ONE}</li> 
    <li>#{ExampleEnum.TWO}</li> 
    <li>#{ExampleEnum.THREE}</li> 
</ul> 
相關問題