2012-03-26 70 views
1

我正在使用Seam 3 Internationalization包在我的应用程序中实现消息传递。自定义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!"); 

最后,我在我的脸上显示如下消息:

<h:messages /> 

非常标准的,到目前为止,我认为......

我要实现自定义的逻辑是能够首先检查数据库表(可以称之为表MessageBundleOverride),用于匹配的消息密钥。如果存在,我想使用MessageBundleOverride表中的值而不是属性文件。如果它不存在或为空,我想使用属性文件中找到的值。

我在想这样做的Weld/CDI方式,我可以实现消息接口并注册接缝,以便它在“注入”而不是默认的MessagesImpl实现中获取我的消息实现。附带Seam国际化套件。我对Seam/Weld有点新鲜,所以不确定这是否是一件简单的事情。

任何帮助非常感谢, 谢谢!

回答

1

想通了能完成这一操作的一种方式阅读文档焊后: 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> 

只要焊接正在拾取定义了MyMessages的包中的类,就应该这样做。