I18N:
Locale代表本地环境
Locale (String lang)
Locale (String lang,String country)
Locale (String lang,String country,String variant)
ResourceBundle加载不同的的资源文件
1.public class TestI18N {
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Locale locale=new Locale("ad","da");
ResourceBundle rb=ResourceBundle.getBundle("lang", locale);
String userName=rb.getString("username");
String userPassword=rb.getString("userPassword");
System.out.println(userName+" "+userPassword);
}
}
2.file文件命名格式:lang_zh_CN.properties
lang.properties //默认的
lang_zh_TW.properties
public class I18NTag extends TagSupport
{
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return super.doEndTag();
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
Locale locale=((HttpServletRequest)this.pageContext.getRequest()).getLocale
();
JspWriter out=this.pageContext.getOut();
ResourceBundle rb=ResourceBundle.getBundle("lang", locale);
String v=rb.getString(key);
try
{
out.write(v);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.SKIP_BODY;
}
}
自定义标签 .tld文件;
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>Hello I18N</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>my</short-name>
<uri>http://java.ming.com/my</uri>
<tag>
<description>
I18N--International
</description>
<name>inal</name>
<tag-class>com.xasxt.action.I18NTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description>
key
</description>
<name>key</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>boolean</type>
</attribute>
</tag>
</taglib>
监听器:
对session监听的类需要实现HttpSessionListener接口,对session属性监听的类需要实现
HttpSessionAttributeListener接口
相应的类必须在web.xml配置<Listener>
对application监听的类需要实现ServletContextListener接口,对session属性监听的类需要实现
ServletContextAttributeListener接口
相应的类必须在web.xml配置<Listener>
获取application对象:
ServletContext application=this.getServl
Locale代表本地环境
Locale (String lang)
Locale (String lang,String country)
Locale (String lang,String country,String variant)
ResourceBundle加载不同的的资源文件
1.public class TestI18N {
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Locale locale=new Locale("ad","da");
ResourceBundle rb=ResourceBundle.getBundle("lang", locale);
String userName=rb.getString("username");
String userPassword=rb.getString("userPassword");
System.out.println(userName+" "+userPassword);
}
}
2.file文件命名格式:lang_zh_CN.properties
lang.properties //默认的
lang_zh_TW.properties
public class I18NTag extends TagSupport
{
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return super.doEndTag();
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
Locale locale=((HttpServletRequest)this.pageContext.getRequest()).getLocale
();
JspWriter out=this.pageContext.getOut();
ResourceBundle rb=ResourceBundle.getBundle("lang", locale);
String v=rb.getString(key);
try
{
out.write(v);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.SKIP_BODY;
}
}
自定义标签 .tld文件;
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>Hello I18N</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>my</short-name>
<uri>http://java.ming.com/my</uri>
<tag>
<description>
I18N--International
</description>
<name>inal</name>
<tag-class>com.xasxt.action.I18NTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description>
key
</description>
<name>key</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>boolean</type>
</attribute>
</tag>
</taglib>
监听器:
对session监听的类需要实现HttpSessionListener接口,对session属性监听的类需要实现
HttpSessionAttributeListener接口
相应的类必须在web.xml配置<Listener>
对application监听的类需要实现ServletContextListener接口,对session属性监听的类需要实现
ServletContextAttributeListener接口
相应的类必须在web.xml配置<Listener>
获取application对象:
ServletContext application=this.getServl