- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何从 java 代码访问消息包以根据当前语言环境获取消息?
我尝试使用 @ManagedProperty
如下所示:
@Named
@SessionScoped
public class UserBean implements Serializable {
@ManagedProperty("#{msg}")
private ResourceBundle bundle;
// ...
public void setBundle(ResourceBundle bundle) {
this.bundle = bundle;
}
}
但是,它仍然是
null
.似乎它在
@Named
中不起作用.
faces-context.xml
中注册资源包的方式:
<application>
<message-bundle>validator.messages</message-bundle>
<locale-config>
<supported-locale>en_US</supported-locale>
<supported-locale>ua_UA</supported-locale>
</locale-config>
<resource-bundle>
<base-name>lang.messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
作者更新:
16:29:10,968 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/WEBSearchPrime_JB_lang].[Faces Servlet]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet Faces Servlet threw exception: org.jboss.weld.exceptions.IllegalProductException: WELD-000054 Producers cannot produce non-serializable instances for injection into non-transient fields of passivating beans\\n\\nProducer\: Producer Method [PropertyResourceBundle] with qualifiers [@Any @Default] declared as [[method] @Produces public util.BundleProducer.getBundle()]\\nInjection Point\: [field] @Inject private model.UserBean.bundle
注意,我还放了 Serializable 接口(interface)
最佳答案
您不能使用 @javax.faces.bean.ManagedProperty
在使用 @Named
注释的 CDI 托管 bean 中.您只能在带有 @ManagedBean
注释的 JSF 托管 bean 中使用它。 .
您需要使用 @javax.faces.annotation.ManagedProperty
相反,连同 @Inject
.这是在 JSF 2.3 中引入的。
@Inject @javax.faces.annotation.ManagedProperty("#{msg}")
private ResourceBundle bundle;
@Dependent
注入(inject)。 .因此请注意,当您将其注入(inject)
@SessionScoped
时bean,那么它基本上会变成
@SessionScoped
也因此永远坚持最初注入(inject)的值(value)。因此, session 后期任何潜在的语言环境更改都不会反射(reflect)在那里。如果这是一个拦截器,那么你真的应该将它注入(inject)到
@RequestScoped
中。或
@ViewScoped
仅,或使用
@Producer
如下所示。
@Produces
的“CDI 生产者”。其中返回具体类型,即
PropertyResourceBundle
如果是
.properties
基于文件的资源包。
@RequestScoped
public class BundleProducer {
@Produces
public PropertyResourceBundle getBundle() {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().evaluateExpressionGet(context, "#{msg}", PropertyResourceBundle.class);
}
}
@Inject
private PropertyResourceBundle bundle;
关于jsf - 通过@ManagedProperty 注入(inject)ResourceBundle 似乎在@Named 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28045667/
我想让数据库对象仅在需要时才创建,所以我想做这样的事情 public class Data { public List getGroups(){ List MyList=new ArrayLi
如何使用@ManagedProperty在支持组件中? 这是合作伙伴选择器复合组件。该组件检查数据库中输入的合作伙伴代码,如果代码有效,则填写合作伙伴名称。 组件:
@ManagedProperty("#{sessionBean}") 未正确注入(inject)。 sessionBean 在 JAR 文件中声明,它也有一个 JSF 2.0 兼容的 faces-co
我正在尝试在多个 SessionScoped bean 之间共享一个对象。虽然我收到了错误,但我真的不知道为什么。 @ManagedProperty(value="#{tb}") private te
我问的问题可能是一个非常简单的问题。我想在登录成功后显示用户详细信息。 我的登录 Controller public LoginController() { } public String get
我正在使用 Spring 和 JSF 2 创建一个 Web 应用程序。 业务对象保存在 Spring 容器中,我使用 @ManagedProperty 将它们注入(inject)托管 Bean,如下所
我正在尝试通过以下方式在我的 FacesConverted 中注入(inject)一个 ManagedBean: @ManagedBean @RequestScoped @FacesConverter
这个问题在这里已经有了答案: Backing beans (@ManagedBean) or CDI Beans (@Named)? (5 个回答) 6年前关闭。 好的,所以我有一个需要引用另一个 (
我正在尝试将 Richfaces 4 展示中的一些代码转换为使用 CDI 而不是 JSF 注释。 我知道我可以使用 @Named 来替换 @MangedBean 和 @Inject 来替换 @Mana
我有两个 jsf Controller ,分别是 ControllerA 和 ControllerB。两者都在 session 范围内。我想从 ControllerA 调用 ControllerB 中
我有一个我正在构建的 JSF 验证器,其中包含我想从 ResourceBundle 加载的属性。但是,我不太确定如何工作,因为它没有正确加载。关于如何完成这项工作的任何想法? 我试过使用 @PostC
我试图将一个 session 范围的 bean 的值注入(inject)一个 View 范围的 bean,但它一直返回 null,这是一个片段: import javax.faces.applicat
我有一个 CDI 托管 bean,我想将请求参数设置为托管属性: import javax.inject.Named; import javax.enterprise.context.RequestS
我需要在同一个 bean 上设置 2 个不同的 ManagedProperty。所以我试过: @ManagedBean(name="selector") @RequestScoped public c
我试图通过 @ManagedProperty 注释将整个 JSF 托管 bean 注入(inject)另一个托管 bean(非常类似于 Possible to inject @ManagedBean
我是 JSF 的新手,我想知道是否可以根据不同的情况将基类的不同子类作为 MangedProperty 注入(inject)?例如,我有这个托管 bean: @ManagedBean @Session
我的 bean 有这个: @ManagedBean @ViewScoped public class BookBean implements Serializable { @Man
首先我想说我是 JSF 的新手。 我想创建简单的复合组件,可用于编辑文章。它应该以这种方式工作: 复合组件看起来像这样 ArticleBean负责复合组件的数据处理(这里是save方法) 每个想要使
像这样定义 View 参数有什么区别: 并在 ManagedBean 中定义属性,如下所示: @Inject @ManagedProperty(value = "#{param.id}")
我尝试学习 JSF 并遇到与 ManagedProperty 相关的问题。但是我尝试使用它,它总是失败 - 异常指针为空。我做错了什么? 我在stackoverflow上阅读了一些“类似的帖子”,但它
我是一名优秀的程序员,十分优秀!