日期:2014-05-17  浏览次数:20690 次

Struts 国际化 i18N properties 数据动态
struts 的国际化中的 properties 里面的value 可不可以动态啊

就比如用代码修改 里面的数据

用什么方法比较好?

用代码修改property文件?

还是用代码动态生成Key 和value 存起来



------解决方案--------------------
用代码修改property文件
------解决方案--------------------
http://wellfrog.iteye.com/blog/611934
http://blog.csdn.net/liuyuhua0066/article/details/7635375

------解决方案--------------------
http://blog.csdn.net/liuyuhua0066/article/details/7635375

------解决方案--------------------
Java code

public class GetProperty {
  public static String readValue(String filePath,String key){
  Properties props = new Properties();
  try {
  InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
  props.load(ips);
  String value = props.getProperty(key);
  System.out.println(key+"="+value);
  return value;
  } catch (FileNotFoundException e) {
  e.printStackTrace();
  return null;
  } catch (IOException e) {
  e.printStackTrace();
  return null;
  }
  }
  //读取全部信息
  public static void readProperties(String filePath) {
  Properties props = new Properties();
  try {
  InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
  props.load(ips);
  Enumeration enum = props.propertyNames();
  while(enum.hasMoreElements()){
  String key = (String)enum.nextElement();
  String value = props.getProperty(key);
  System.out.println(key+"="+value);
  }
  } catch (FileNotFoundException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  }
  }
  public static void writeProperties(String filePath,String paraKey,String paraValue){
  Properties props = new Properties();
  try {
  OutputStream ops = new FileOutputStream(filePath);
  props.setProperty(paraKey, paraValue);
  props.store(ops, "set");
  } catch (IOException e) {
  e.printStackTrace();
  }
  }
}

------解决方案--------------------
觉得一般不太需要动态修改资源文件的内容,通常情况下它是配置好的,这样每次读取也都是相同的值。
动态修改的话,这样在用做按钮或菜单的国际化时,导致这次看是某值,下次看又是另一个值了,有点乱。

要改的话,也可以
Java code
Properties prop = new Properties(); 
       try { 
           InputStream is= new FileInputStream(...); 
           prop.load(is); 
           OutputStream os= new FileOutputStream(...); 
           prop.setProperty(propertyName, propertyValue); 
           prop.store(os, "comments"); 
           os.close();
           is.close(); 
       } catch (IOException e) { 

       }

------解决方案--------------------
配置数据如果要改的话,放到数据库里得啦,做成一个数据字典
------解决方案--------------------
不能动态的给,只能写死,如果了改了页面,那就要在配置文件里面作相应的改动
------解决方案--------------------
属性文件中用占位符{0},{1}等,可以实现动态!