当前位置: 移动技术网 > IT编程>开发语言>Jsp > JSP多语言第一种

JSP多语言第一种

2018年12月26日  | 移动技术网IT编程  | 我要评论

以页面title举例:

读取多语言肯定要放在properties中,并且通过后台读取properties对应的键返回其值,则jsp页面需要引入类:


  

<%@page import="com.mytest.sys.config.systextconfig"%> 

<%@page import="com.mytest.sys.config.systextconfig"%>这里systextconfig类处理多语言

<title><%=systextconfig.getconfiginfo("bsd_text")+systextconfig.getconfiginfo("title")%></title> 

<title><%=systextconfig.getconfiginfo("bsd_text")+systextconfig.getconfiginfo("title")%></title>
systextconfig类的内容为

  

public class systextconfig { 
     
        private static  properties properties =null;//读取properties的资源文件  
     
    private  static long lastmodified=0;//内存中上次修改的时间 long型  
     
    private  static string filepath=null;//保存properties文件的绝对路径  
     
    private  static file  sysconfigfile=null; 
     
    
   static {//初始化static变量的值  
        stringbuffer configfilepath = new stringbuffer(); 
         
        string classpath= systextconfig.class.getclassloader().getresource("").getpath(); 
        string weninfopath = classpath.substring(0,classpath.lastindexof("classes")); 
         
        configfilepath.append(weninfopath).append("config"); 
         
        configfilepath.append(file.separator).append("systext.properties"); 
         
        filepath=configfilepath.tostring();  
    } 
    
   private static boolean isnotlastmodified(){ 
       sysconfigfile= new file(filepath); 
        
       if( sysconfigfile.lastmodified()!=lastmodified) 
           return true; 
           return false; 
   } 
   
   private static void loadsysconfigfile(){ 
        
       properties = new properties(); 
       
       fileinputstream fileinputstream; 
       try { 
            fileinputstream = new   fileinputstream(sysconfigfile); 
            properties.load(fileinputstream); 
             
        } catch (exception e) { 
            // todo auto-generated catch block  
            e.printstacktrace(); 
        } 
        
   } 
   public static string getconfiginfo(string key) { 
           //函数入口,首先校验properties文件的最近以次修改时间,如果不为0 则代表其修改过 则要再次加载loadsysconfigfile  
           if(isnotlastmodified()) loadsysconfigfile();    
       object object = properties.get(key);   //读取properties的键  
       string value =""; 
       try { 
           if(stringutils.isnotempty(object)){ 
               value = new string(object.tostring().getbytes("iso8859_1"));//通过键返回其值  
           } 
        } catch (unsupportedencodingexception e) { 
            e.printstacktrace(); 
        } 
       return value;    
   }    
 
} 

public class systextconfig {
 
        private static  properties properties =null;//读取properties的资源文件
 
 private  static long lastmodified=0;//内存中上次修改的时间 long型
   
    private  static string filepath=null;//保存properties文件的绝对路径
   
    private  static file  sysconfigfile=null;
   
  
   static {//初始化static变量的值
     stringbuffer configfilepath = new stringbuffer();
     
     string classpath= systextconfig.class.getclassloader().getresource("").getpath();
     string weninfopath = classpath.substring(0,classpath.lastindexof("classes"));
     
  configfilepath.append(weninfopath).append("config");
  
  configfilepath.append(file.separator).append("systext.properties");
  
  filepath=configfilepath.tostring();
    }
  
   private static boolean isnotlastmodified(){
    sysconfigfile= new file(filepath);
   
    if( sysconfigfile.lastmodified()!=lastmodified)
     return true;
     return false;
   }
 
   private static void loadsysconfigfile(){
   
    properties = new properties();
  
    fileinputstream fileinputstream;
    try {
   fileinputstream = new   fileinputstream(sysconfigfile);
   properties.load(fileinputstream);
   
  } catch (exception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
   
   }
   public static string getconfiginfo(string key) {
           //函数入口,首先校验properties文件的最近以次修改时间,如果不为0 则代表其修改过 则要再次加载loadsysconfigfile
           if(isnotlastmodified()) loadsysconfigfile();  
    object object = properties.get(key);   //读取properties的键
    string value ="";
    try {
     if(stringutils.isnotempty(object)){
      value = new string(object.tostring().getbytes("iso8859_1"));//通过键返回其值
     }
  } catch (unsupportedencodingexception e) {
   e.printstacktrace();
  }
    return value;  
   }  

}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网