前幾天發(fā)了一篇Struts國際化的博客——《菜鳥學習SSH(二)——Struts2國際化手動切換版》,有網(wǎng)友提了一個意見,見下圖:
于是就有了下面修改的版本:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.custom.i18n.resources" value="bbs2009"></constant>
<package name="/" namespace="/" extends="struts-default" >
<action name="*-*" class="com.lsj.action.{1}Action" method="{2}">
<result>/{1}-{2}.jsp</result>
</action>
</package>
</struts>
登錄頁
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登錄</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="Login-login" method="post">
<s:property value="getText('login.username')"/> <input name="username" />
<s:property value="getText('login.password')"/><input name="password" type="password" />
<input type="submit" value="<s:property value="getText('login.login')"/>" />
</form>
</body>
</html>
OK,這樣將瀏覽器的語言設(shè)置成中文,那么頁面就以中文顯示;把瀏覽器語言設(shè)置成英文,頁面就以英文顯示。這一個版本就是上面那位網(wǎng)友說的那種效果。這個版本實現(xiàn)起來要比上一個要簡單,不過之前那種手動修改也有它的作用,大家在上網(wǎng)的時候都見過很多網(wǎng)站上有手動切換顯示語言的鏈接吧(像微軟、谷歌的網(wǎng)站上都有)!這是為了讓那些身在國外的人能夠自己切換到母語的顯示頁面而做的。這兩種方式各有各的作用。
兩種方式各司其職,具體要怎么用還得看你想要實現(xiàn)什么樣的需求了。這是兩種不同的國際化實現(xiàn)方法,今天把這種自動識別瀏覽器語言的方法也寫出來供大家把玩把玩,希望各位玩的開心。
更多建議: