练习——多语言版本
1)在当前Web应用的WEB-INF目录下放置两个文本文件
一为messageresource.properties,内容为:
hello.title = helloapp
hello.hello = Hello
login.title = helloapp
login.user = User Name
login.password = Password
login.submit = Submit
二为messageresource_ch.properties,内容为:
hello.title = helloapp的hello页面
hello.hello = 你好
login.title = helloapp的登录页面
login.user = 用户名
login.password = 口令
login.submit = 提交
在Eclispe中可能要修改属性文件的默认字符集才能保存中文的属性信息
2)新建初始化属性信息的Servlet为:
package servlets;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InitServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
Properties ps = new Properties();
Properties ps_ch = new Properties();
try {
ServletContext context = config.getServletContext();
InputStream in = context
.getResourceAsStream("/WEB-INF/messageresource.properties");
ps.load(in);
InputStream in_ch = context
.getResourceAsStream("/WEB-INF/messageresource_ch.properties");
ps_ch.load(in_ch);
in.close();
in_ch.close();
// 由于保存在ServletContext中,所以是Web全局变量
context.setAttribute("ps", ps);
context.setAttribute("ps_ch", ps_ch);
} catch (Exception e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
3)修改web.xml文件,使得Servlet自动装载
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>myweb</display-name>
<servlet>
<description>
</description>
<display-name>InitServlet</display-name>
<servlet-name>InitServlet</servlet-name>
<servlet-class>servlets.InitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/InitServlet</url-pattern>
</servlet-mapping>
</web-app>
4)新建标签,为:
package taglib;
import java.util.Properties;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
public class MessageTag extends TagSupport {
private String key = null;
public void setKey(String key) {
this.key = key;
}
public int doEndTag() throws JspException {
try {
Properties ps = (Properties) pageContext.getAttribute("ps",
pageContext.APPLICATION_SCOPE);
Properties ps_ch = (Properties) pageContext.getAttribute("ps_ch",
pageContext.APPLICATION_SCOPE);
HttpSession session = pageContext.getSession();
String language = (String) session.getAttribute("language");
String message = null;
if (language != null && language.equals("Chinese")) {
message = (String) ps_ch.get(key);
message = new String(message.getBytes("ISO-8859-1"), "GB2312");
} else
message = (String) ps.get(key);
pageContext.getOut().print(message);
} catch (Exception e) {
throw new JspTagException(e.getMessage());
}
return EVAL_PAGE;
}
}
5)修改mytags.tld文件,为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.2</jspversion>
<shortname></shortname>
<uri></uri>
<info></info>
<tag>
<name>message</name>
<tagclass>taglib.MessageTag</tagclass>
<bodycontent>empty</bodycontent>
<info>produce message by key</info>
<attribute>
<name>key</name>
<required>true</required>
</attribute>
</tag>
</taglib>
注意:其中的bodycontent用于描述标签处理器如何使用标签体的内容。有三种取值:empty:表示标签体必须为空;JSP:表示脚本元素和模板及其它标签一样被评估;tagdependent:体内容被原封不动写入BodyContent,其它脚本元素以源码形式出现,而不被JSP容器解释
6)修改index.jsp文件,为:
<%@page c%>
<%@ taglib prefix="mm" uri="mytags.tld"%>
<html>
<head>
<title><mm:message key="hello.title" /></title>
</head>
<body>
<b><mm:message key="hello.hello" /></b>
</body>
</html>
此时显示英文界面信息
再次修改显示中文信息,为:
<%@page c%>
<%@ taglib prefix="mm" uri="mytags.tld"%>
<%
session.setAttribute("language","Chinese");
%>
<html>
<head>
<title><mm:message key="hello.title" /></title>
</head>
<body>
<b><mm:message key="hello.hello" /></b>
</body>
</html>
7)完善
增加default.html文件,为:
<html>
<head>
<title>helloapp</title>
</head>
<body>
<p><font size="7">Welcome to HelloApp</font></p>
<p><a href="index.jsp?language=English">English version </a>
<p><a href="index.jsp?language=Chinese">中文版</a>
</body>
</html>
修改web.xml文件,增加:
<welcome-file-list>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
修改index.jsp文件为:
<%@page c%>
<%@ taglib prefix="mm" uri="mytags.tld"%>
<html>
<%
String language = request.getParameter("language");
if (language == null)
language = "English";
session.setAttribute("language", language);
%>
<head>
<title><mm:message key="login.title" /></title>
</head>
<body>
<br>
<form name="loginForm" method="post" action="DisplayServlet">
<table>
<tr>
<td>
<div align="right"><mm:message key="login.user" />:</div>
</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>
<div align="right"><mm:message key="login.password" />:</div>
</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="Submit" name="Submit"
value=<mm:message key="login.submit" />></td>
</tr>
</table>
</form>
</body>
</html>
此时可以看到不同语言的显示效果
继续增加处理表单的DisplayServlet,为:
package servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class DisplayServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
HttpSession session = request.getSession(true);
String username = request.getParameter("username");
session.setAttribute("USER", username);
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context
.getRequestDispatcher("/info.jsp");
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
增加info.jsp文件,为:
<%@page c%>
<%@ taglib prefix="mm" uri="mytags.tld"%>
<html>
<head>
<title><mm:message key="hello.title" /></title>
</head>
<body>
<%
String user = (String) session.getAttribute("USER");
String language = (String) session.getAttribute("language");
if (language != null && language.equals("Chinese")) {
user = new String(user.getBytes("ISO-8859-1"), "GB2312");
}
%>
<b><mm:message key="hello.hello" /> : <%=user%></b>
</body>
</html>
[此贴子已经被作者于2010-12-12 17:59:03编辑过]