中英文切换(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) {
this. sKey = 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.0</ tlib-version>
< short-name>zsz</ short-name>
< uri>http://www.zsz.org</ uri>
< tag>
< name>i18n</ name>
< tag-class>com.i18n.tag.I18nTag</ tag-class>
< body-content>empty</ body-content>
< attribute>
< name>sKey</ name>
< required>true</ required>
</ 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>
< a href= "?language=zh">中</ a>< a href= "?language=en">en</ a>< hr>
< zsz :i18n sKey= "tag"></ zsz :i18n> < zsz :i18n sKey= "china"/>
< hr>
${i18n.el } : ${i18n.china }
</ body>
</ html>
中英文切换(servlet)相关推荐
- SpringBoot中英文切换/国际化——java后端怎么用预置文本的内容替换web网页内容(Resource Bundle)
文章目录 创建预置文件文本内容 Thymeleaf修改前端使其获得预置文本内容 html页面设置跳转标签和传值 创建一个MyLocaleResolver配置文件 在MvcConfiguration中注 ...
- SpringMVC实现中英文切换
说明:该配置基于SpringMVC环境搭建的基础上,实现的中英文切换 步骤一:在spring核心配置文件中配置 <!-- 配置国际化语言配置文件 --> <bean name ...
- springboot国际化配置中英文切换
1.新建项目 第一步新建springboot项目,勾选web依赖,或者后面进入在pom.xml中加入也可以. 2.项目结构 项目pom.xml <?xml version="1.0&q ...
- SpringBoot 国际化(中英文切换)
中英文切换 一.新建文件夹及文件 二.进行可视化配置 三.配置 四.使用key来获取国际化内容 五.国际化解析器 六.将自定义的国际化组件注册到Bean 一.新建文件夹及文件 1.在resources ...
- SpringMVC 国际化-中英文切换
国际化是为了实现不同地区页面呈现不同的语言问题: 编写程序实现国际化,在xml文件中做响应的配置,然后编写.properties文件即可. 实现登录界面的中英文切换: xml文件配置: <!-- ...
- ae中英文切换_AE技巧,AE CC软件如何切换中英文版
平时使用,中文界面更容易操作,但有时候需要切换成英文版使用,那AE怎么切换中英文界面呢?今天就来跟大家讲解一下切换语言界面的方法. 中文界面固然看着熟悉,对于入门来说也是非常有利的.但是,我们在学习过 ...
- jenkins页面中英文切换配置
jenkins中英文切换配置 Jenkins--英文切换成中文 更新了一下jenkins的war包, 结果启动后成英文的了,后来百度出解决方案: 原文地址:https://blog.csdn.net/ ...
- Spring boot切换Servlet容器
切换Servlet容器 Spring boot默认配置Tomcat作为Servlet容器 引入web模块,默认使用嵌入式的Tomcat 可以切换Jetty.Undertow 默认配置 Pom文件,查看 ...
- 中英文切换_王者荣耀:模拟战奖励一览,新增中英文切换,李白第二套星元来袭...
王者荣耀体验服在今日凌晨更新后新增了不少功能,比如模拟战加入了精灵机制,以及局内新增语言设置,另外李白继千年之狐后,又有一套皮肤加入"星计划",即李白团战精神皮肤敏锐之力将迎来星元 ...
最新文章
- android 添加广告用proguard混淆后不显示问题解决方法
- 切点、切面:@Aspect、@PointCut相关的个人总结
- 数据库 CURD测试题【简单】
- 按钮右对齐_Python Tkinter Button按钮
- openshift使用_OpenShift v3:使用WildFly和MySQL的Java EE 7入门
- [Leedcode][JAVA][第42题][动态规划][双指针][栈]
- 长春学校计算机科学技术学院,长春大学计算机科学技术学院
- android adb server didn't ack
- cocos2d-x之json文件读取初试
- 5.26 考试修改+总结
- guido发布python版本的年份_Guido van Rossum
- android html5小游戏源代码下载,HTML5小游戏源代码大全
- C语言实现求斐波那契数列中的第n项
- 可“一键”轻松激活Office 2010系列“VOL”版本的又一款迷你“KMS服务器”
- 科研突破:非宁静无以致远
- MATLAB系统仿真其三:Ornstein-Uhlenbeck(OU)噪声
- 【华为认证】HCIA-DATACOM技术分享-VRP系统基本操作-入门级手册(一)
- vue 事件调用 传参_Vue 事件如何传递参数?
- html 填表模板,WEB前端开发简历自我评价填写样本
- 前端逻辑练习题+学习记录 不定期更新