前幾天發(fā)了一篇Struts國(guó)際化的博客——《菜鳥(niǎo)學(xué)習(xí)SSH(二)——Struts2國(guó)際化手動(dòng)切換版》,有網(wǎng)友提了一個(gè)意見(jiàn),見(jiàn)下圖:
于是就有了下面修改的版本:
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>
登錄頁(yè)
<%@ 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,這樣將瀏覽器的語(yǔ)言設(shè)置成中文,那么頁(yè)面就以中文顯示;把瀏覽器語(yǔ)言設(shè)置成英文,頁(yè)面就以英文顯示。這一個(gè)版本就是上面那位網(wǎng)友說(shuō)的那種效果。這個(gè)版本實(shí)現(xiàn)起來(lái)要比上一個(gè)要簡(jiǎn)單,不過(guò)之前那種手動(dòng)修改也有它的作用,大家在上網(wǎng)的時(shí)候都見(jiàn)過(guò)很多網(wǎng)站上有手動(dòng)切換顯示語(yǔ)言的鏈接吧(像微軟、谷歌的網(wǎng)站上都有)!這是為了讓那些身在國(guó)外的人能夠自己切換到母語(yǔ)的顯示頁(yè)面而做的。這兩種方式各有各的作用。
兩種方式各司其職,具體要怎么用還得看你想要實(shí)現(xiàn)什么樣的需求了。這是兩種不同的國(guó)際化實(shí)現(xiàn)方法,今天把這種自動(dòng)識(shí)別瀏覽器語(yǔ)言的方法也寫(xiě)出來(lái)供大家把玩把玩,希望各位玩的開(kāi)心。
更多建議: