简体   繁体   English

将应用程序设置放在JSF中的位置

[英]Where to put application settings in JSF

I wondered what is the prefered place to put application wide presettings in JSF. 我想知道在JSF中放置应用程序范围预置的首选位置是什么。 I see 3 options: 我看到3个选项:

  1. web.xml web.xml
  2. properties file with <resource-bundle> configuration in faces-config.xml Faces-config.xml中具有<resource-bundle>配置的属性文件
  3. properties file and use it per java.util.Properties 属性文件,并根据java.util.Properties使用它

I would prefer Nr. 我更喜欢Nr。 2, because it seems to be most straightforward and easiest to use. 2,因为它似乎是最直接,最容易使用的。 But I am not sure if this is good, because in general I thought <resource-bundle> is planed for localized messages and not for application wide settings. 但是我不确定这是否好,因为通常我认为<resource-bundle>是针对本地化消息而不是针对应用程序范围的设置而设计的。

Option #1 and #2 have the advantage that you can also easily access the settings via EL. 选项#1和#2的优点是您还可以通过EL轻松访问设置。 With option #1 you can do #{initParam['keyUsedForContextParam']} . 使用选项#1,您可以执行#{initParam['keyUsedForContextParam']} With option #2 you can go the normal way for resource bundles #{rscBundleName['keyUsedInPropertiesFile']} . 使用选项#2,您可以按照常规方式使用资源束#{rscBundleName['keyUsedInPropertiesFile']} But as you already said: Most of the time you will use that for localization, so it will be a bit weird, but it would work. 但是正如您已经说过的:在大多数情况下,您将使用它进行本地化,因此这有点奇怪,但它会起作用。 But for application wide settings, I think the web.xml with custom context-params would probably be the more appropriate way. 但是对于应用程序范围的设置,我认为带有自定义context-params的web.xml可能是更合适的方法。

If you want to access the settings in your managed bean, you will have to access the settings via the FacesContext. 如果要访问托管bean中的设置,则必须通过FacesContext访问设置。 For option #1 that might look like FacesContext.getCurrentInstance().getExternalContext().getInitParameter("keyUsedForContextParam"); 对于选项#1,可能看起来像FacesContext.getCurrentInstance().getExternalContext().getInitParameter("keyUsedForContextParam"); . This is not that nice. 这不是很好。

Option #3 Also works of course, but you would need some kind of @ApplicationScoped utility bean, which will access the settings from your custom .properties -file. 选项#3当然也可以使用,但是您将需要某种@ApplicationScoped实用程序bean,它将从您的自定义.properties -file访问设置。 But that's actually not a very big problem. 但这实际上不是一个很大的问题。 You could access the settings via EL like this: #{settingsBean.get('keyUsedInPropertiesFile')} . 您可以通过EL这样访问设置: #{settingsBean.get('keyUsedInPropertiesFile')} This is more obvious than initParam for example. 例如,这比initParam更明显。 In your managed beans you can also just inject your SettingsBean and access the settings the same way. 在托管bean中,您也可以只注入SettingsBean并以相同方式访问设置。 Most easy to understand. 最容易理解的。

Option #3 also has a big not that obvious advantage. 选项#3也有很大的优势,但并不明显。 You can re-read the settings in real time. 您可以实时重新读取设置。 The web.xml and the resource bundles defined in the faces-config.xml will only be read on startup. 仅在启动时才会读取faces-config.xml中定义的web.xml和资源包。 The SettingsBean might re-read the .properties file every time and you would be able to change settings on-the-fly (make it @RequestScoped ). SettingsBean可能每次都会重新读取.properties文件,您将能够即时更改设置(将其设置为@RequestScoped )。 The bean alternative is also more powerful in general, you might for example define default-settings which apply when you do not define any value in your properties file and so on. 通常,bean替代方法也更强大,例如,您可以定义默认设置,这些默认设置在未在属性文件中定义任何值时适用,依此类推。

I'd advise you to go with Option #3. 我建议您选择选项3。

web.xml web.xml

I gather you mean <context-param> ? 我知道你的意思是<context-param>吗? This is usually to be used for configuration parameters of servlet/filter based 3rd party libraries (JSF API/impl, PrimeFaces, OmniFaces, Jersey, etc). 这通常用于基于servlet /过滤器的第三方库(JSF API / impl,PrimeFaces,OmniFaces,Jersey等)的配置参数。


properties file with <resource-bundle> configuration in faces-config.xml faces-config.xml具有<resource-bundle>配置的属性文件

This is usually to be used for localization of JSF pages, ie being able to switch to a completely different language site-wide. 通常将其用于JSF页面的本地化,即能够在站点范围内切换到完全不同的语言。


properties file and use it per java.util.Properties 属性文件,并根据java.util.Properties使用它

This is usually to be used for name=value pairs which can represent anything like localized text and configuration settings. 通常用于“名称=值”对,该对可以表示任何内容,例如本地化文本和配置设置。


You see, there's not really a "which should I use" case here. 您会看到,这里实际上没有“我应该使用”的情况。 The question "when should I use it" was been more appropriate. “我什么时候应该使用它”这个问题更合适。 The answer is ultimately, just use the right tool for the job. 最终的答案是,只需使用正确的工具即可完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM