Posted on 2006-09-25 00:48 
kook 阅读(224) 
评论(0)  编辑  收藏  所属分类: 
Struts 
			 
			
		 
		
		在Struts框架中,往往需要对资源文件进行配置,从而实现国际化的目的。
资源文件中允许使用静态和动态文本。通过动态文本来动态的设定其显示的内容,从而能更好的减少代码的冗余,然而动态文本中的参数指定,是通过什么来实现的呢?
下面是ApplicationResources.properties资源文件中的内容:
		
				
						
								1 # Resources for parameter 'com.kook.struts.ApplicationResources'
				
				
						
				
				
						
								2 # Project P/
				
				
						
								Demo_242103
								3 
				
				
						
				
				
						
								4 
				
				
						
				
				
						
								5 
				
				
						
				
				
						
								6 hello={0} is the brother of {1
				
				
						
								}
								7 hello.brother=
				
				
						
								zhangsan
								8 hello.person=lisi
				
		
		
		通常我们可以在验证框架的配置文件中来配置其动态参数的值,如:
		
				
						
								1 <field property="name" depends="required">
				
				
						
				
				
						
								2                 <arg0 name="required" key="hello.brother"/>
				
				
						
				
				
						
								3  </field>
				
		
		
		但是具体的实现可以通过java.text.MessageFormat类来实现
		
				
						
								1       MessageResources messages =
				
				
						
								 getResources(request);
								2         String title = messages.getMessage("hello"
				
				
						
								);
								3         String[] arr = new String[2
				
				
						
								];
								4         arr[0] = messages.getMessage("hello.brother"
				
				
						
								);
								5         arr[1] = messages.getMessage("hello.person"
				
				
						
								);
								6         String newMessage =
				
				
						
								 MessageFormat.format(title, arr);
								7         System.out.println(newMessage);
				
		
		
		最后输出:zhangsan is the brother of lisi