我正在使用Seam3国际化包在我的应用程序中实现消息传递。
简而言之,这就是我正在做的事情:
导入/注入所需的类:
import org.jboss.seam.international.status.Messages;
import javax.inject.Inject;
@Inject
private Messages messages;当发生错误时,我在我的支持bean中创建一条消息:
messages.error(new BundleKey("AppMsgResources", "errorMsgKey")).defaults("Error: Something bad happened!");最后,我在faces页面中显示消息,如下所示:
<h:messages />到目前为止我认为非常标准..。
我要实现的自定义逻辑是能够首先检查数据库表(让我们称这个表为MessageBundleOverride)是否有匹配的消息键。如果它存在,我希望使用MessageBundleOverride表中的值,而不是属性文件。如果它不存在或为空,我希望使用在属性文件中找到的值。
我想有一种方法可以实现Messages接口,并以某种方式将其注册到seam中,这样它就可以在"inject“期间获取我的消息实现,而不是Seam国际化包附带的默认MessagesImpl实现。我对Seam / Weld还是个新手,所以不确定这是不是一件简单的事情。
任何帮助都非常感谢,谢谢!
发布于 2012-03-28 06:00:41
在阅读完Weld文档后,我想出了一种完成此任务的方法:http://docs.jboss.org/weld/reference/latest/en-US/html/injection.html#alternatives
@Alternative
@RequestScoped
public class MyMessages extends MessagesImpl {
/*
* Override a method that you want to customize or write new code here
*/
@Override
public Set<Message> getAll() {
Set<Message> allMessages = super.getAll();
// do some custom logic here
applyOverrides(allMessages);
return allMessages;
}
...
// override any other method as needed
// You will probably have to override everything so it probably
// wouldnt make sense to extend the existing implementation)
...
}在beans.xml文件中,您必须将这个新类声明为默认类的替代类:
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<alternatives>
<class>com.company.project.view.messages.MyMessages</class>
</alternatives>
</beans>只要weld在你定义了MyMessages的包中拾取类,就应该做到这一点。
https://stackoverflow.com/questions/9881029
复制相似问题