JSP多语言第一种
程序员文章站
2022-04-15 13:29:00
以页面title举例:
读取多语言肯定要放在properties中,并且通过后台读取properties对应的键返回其值,则jsp页面需要引入类:
<%@pag...
以页面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; } }
上一篇: JSP页面dialog弹出层
下一篇: 非服务器控件绑定数据