doing an international project , hml front page is used to implement dynamic data show + velocity , without using jsp, so there is no struts tags to facilitate the realization of internationalization .
check in online using velocity under the MessageTool can achieve internationalization struts of this .
However , when I can not get discovered and configured to the content , the following is the configuration file
<!-- velocity中配置MessageTool -->
<tool>
<key>message</key>
<scope>request</scope>
<class>org.apache.velocity.tools.struts.MessageTool</class>
</tool>
call in html :
<!-- 使用 ${message.get("name")} 相等于 ActionSupport 中的 getText("name")。 -->
国际化:$!{message},${message.get("name")}<br>
run after the first result of course is a memory address value , and the second because to get any value output string $ {message.get ("name")}.
This result is really driving me crazy a preliminary analysis , the piece seems to get less than struts resource file , but I can use the output of the action in which:
1
public class Test extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println(getText("name"));
so there is value in the console output .
ask the next big God : How do I use velocity to achieve international ?
------ Solution ---------------------------------------- ----
org.apache.velocity.tools.generic.ResourceTool you want to use this class , and then configure the internationalization files.
------ Solution ---------------------------------------- ----
messageResource.xml
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>/WEB-INF/messageResource/MessageResource</value>
</list></property>
test:
private static MessageSource messageSource;
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
messageSource = (MessageSource) applicationContext.getBean("messageSource");
MessageTool messageTool = new MessageTool(new MessageSourceAccessor(messageSource), locale);
ToolContext context = manager.createContexg
8h"@t();
context.put("messageTool", messageTool);
System.out.println("-------begin---------");
System.out.println(messageTool.get("code1"));// hello
System.out.println("-------end-----------");
message_en_US.properties examples:
code1=hello
code2=foo {0}
test.vm examples:
code1: $message.get("code1")
code2: $message.get("code2", ["bar"])
code3: $message.get("code3", "default")
------ Solution ------------------------------------- -------
good esoteric . . Give you the top.
------ For reference only -------------------------------------- -
Great God , give me a ballpark steps it? I try a lot will not
------ For reference only ------------------------------- --------
Many thanks for your detailed answer . I have solved this problem , although this is not the way you carry out his .
------ For reference only -------------------------------------- -
this problem is ResourceTool the tools I think writing is not very good.
desperation chops down only to find the source , ResourceTool support for the default locale initialization files and not good .
All My solution is to inherit under ResourceTool then rewrite get method, coupled with the default constructor to initialize properties
没有评论:
发表评论