Use message bundle in Java class with Seam

て烟熏妆下的殇ゞ 提交于 2019-11-27 22:41:42

问题


I want to use my message bundle (messages_fr.properties) in a Java class with seam.

In a jsf file, all work fine like this:

<h2>#{msg.newCustomer}</h2>

But in my Java class, I tried to do this:

statusMessages.addToControlFromResourceBundle("refArbor", "#{messages['error_refArborDoesntExist']}");

or this:

statusMessages.addToControlFromResourceBundle("refArbor", "error_refArborDoesntExist");

or again this:

statusMessages.addToControlFromResourceBundle("refArbor", "#{msg.error_refArborDoesntExist}");

But the message showned is:

error_refArborDoesntExist

And not the real message.

How can I use my bundle is a Java class ?

Thanks.


回答1:


I usually inject the resource bundle, or the resource into the class and use it from there. Example:

@In("#{messages['name']}")
private String name

Or,

@In
private Map<String, String> messages;

wich injects the resourceBundle as a Map. (make sure the it's named messages in this case).




回答2:


There are a number of ways you can do it.

StatusMessages.instance().addFromResourceBundle("msg.newCustomer);
//This will add the msg.newCustomer message to the view

String msg = org.jboss.seam.international.Messages.instance().get("msg.newCustomer);
//This will put the msg.newCustomer message in variable msg



回答3:


It's working with your solution:

@In
private Map<String, String> messages;

Just place the messages.properties in WEB-INF/classes/




回答4:


String msg = org.jboss.seam.international.Messages.instance().get("key") worked for me!

I had the same problem using an entity bean.



来源:https://stackoverflow.com/questions/3555712/use-message-bundle-in-java-class-with-seam

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!