中英文切换(servlet)

 

 doit 

 

创建过滤

packagecom.i18n.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@WebFilter(filterName = "Filter", urlPatterns = "/*",initParams ={@WebInitParam(name = "default",value = "en")})
public class MyFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponseresp, FilterChain chain) throwsServletException,IOException {
        HttpServletRequest request =(HttpServletRequest) req;
        HttpServletResponse response =(HttpServletResponse) resp;
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String .getParameter("language");
        if (lang != null) {
            try{
                String sFile =request.getServletContext().getRealPath("/") + "/WEB-INF/messages_" + lang + ".properties";
                Properties properties = new Properties();
                properties.load(new FileInputStream(sFile));
                request.getSession().setAttribute("i18n", properties);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

        chain.doFilter(req, resp);
    }


    public void init(FilterConfig config) throws ServletException {
        try {
            String .getInitParameter("default");
            if (lang == null) lang = "en";
            String sFile =config.getServletContext().getRealPath("/") + "/WEB-INF/messages_" + lang + ".properties";
            Properties properties = new Properties();
            properties.load(new FileInputStream(sFile));
           config.getServletContext().setAttribute("i18n", properties);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

 

 

 

创建properties文件

messages_en.properties

china=chinese
tag=tag
el=el

messages_zh.properties

china=中国
tag=标签
el=EL表达式

创建标签

package com.i18n.tag; 

import javax.servlet.jsp.JspException; 
import javax.servlet.jsp.JspWriter; 
import javax.servlet.jsp.PageContext; 
import javax.servlet.jsp.tagext.SimpleTagSupport; 
import java.io.IOException; 
import java.util.Properties; 

public class I18nTag  extends SimpleTagSupport { 
     private String  sKey

     public String getsKey() { 
         return sKey
    } 

     public void setsKey(String sKey) { 
         thissKey = sKey; 
    } 

    @Override 
     public void doTag()  throws JspException, IOException { 
        PageContext pageContext = (PageContext) getJspContext(); 
        Properties prop = (Properties) pageContext.getSession().getAttribute( "i18n"); 
         if (prop ==  null) prop = (Properties) pageContext.getServletContext().getAttribute( "i18n"); 
        JspWriter out = pageContext.getOut(); 
         if (prop ==  null
            out.print( sKey); 
         else
            
out.print(prop.getProperty( sKey)); 
    } 
}

 

配置TLD

xml version= "1.0"  encoding= "ISO-8859-1"?> 

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"

    < tlib-version>1.0tlib-version> 
    < short-name>zszshort-name> 
    < uri>http://www.zsz.orguri> 

   < tag
       < name>i18nname> 
       < tag-class>com.i18n.tag.I18nTagtag-class> 
       < body-content>emptybody-content> 
       < attribute
           < name>sKeyname> 
           < required>truerequired> 
       attribute> 
   tag> 

taglib>

 

使用i18n

<%@  page contentType=" text/html;charset=UTF-8"  language=" java" %> 
<%@  taglib prefix=" zsz"  uri=" http://www.zsz.org" %> 
html
  < head
    < meta charset= "UTF-8"
    < title>$Title$title> 
  head> 
  < body
  < href= "?language=zh">中a><  href= "?language=en">ena>< hr
zsz :i18n sKey= "tag">zsz :i18n>    < zsz :i18n  sKey= "china"/> 
  < hr
  ${i18n.el }  :   ${i18n.china } 
  body> 
html>

 

中英文切换(servlet)_第1张图片

 

你可能感兴趣的:(J2EE应用)