Struts国际化自动检测浏览器语言版 .

 

前几天发了一篇Struts国际化的博客,有网友提了一个意见,见下图:


于是就有了下面修改的版本:


web.xml

<?xml version="1.0" encoding="UTF-8"?>  
  1. <web-app version="2.5"   
  2.     xmlns="http://java.sun.com/xml/ns/javaee"   
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.   <welcome-file-list>  
  7.     <welcome-file>index</welcome-file>  
  8.   </welcome-file-list>  
  9.   <filter>  
  10.       <filter-name>struts2</filter-name>  
  11.       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.   </filter>  
  13.   <filter-mapping>  
  14.     <filter-name>struts2</filter-name>  
  15.     <url-pattern>/*</url-pattern>  
  16.   </filter-mapping>  
  17.     
  18. </web-app>  

 

<?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" ?>  
  1.   
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"  
  3.     "http://struts.apache.org/dtds/struts-2.1.dtd">  
  4.   
  5. <struts>    
  6.       
  7.     <constant name="struts.devMode" value="true"></constant>  
  8.     <constant name="struts.custom.i18n.resources" value="bbs2009"></constant>  
  9.     <package name="/" namespace="/" extends="struts-default" >  
  10.           
  11.        <action name="*-*" class="com.lsj.action.{1}Action" method="{2}">  
  12.             <result>/{1}-{2}.jsp</result>  
  13.        </action>  
  14.    
  15.     </package>     
  16.        
  17. </struts>  
<?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"%>  
  1. <%  
  2. String path = request.getContextPath();  
  3. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  4. %>  
  5. <%@taglib uri="/struts-tags" prefix="s" %>  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html>  
  8.   <head>  
  9.     <base href="<%=basePath%>">  
  10.       
  11.     <title>登录</title>  
  12.       
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.   
  19.   </head>  
  20.     
  21.   <body>  
  22.   <form action="Login-login" method="post">  
  23.     <s:property value="getText('login.username')"/> <input name="username" />  
  24.     <s:property value="getText('login.password')"/><input name="password" type="password" />  
  25.     <input type="submit" value="<s:property value="getText('login.login')"/>/>   
  26.   </form>  
  27.     
  28.   </body>  
  29. </html>  
<%@ 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,这样将浏览器的语言设置成中文,那么页面就以中文显示;把浏览器语言设置成英文,页面就以英文显示。这一个版本就是上面那位网友说的那种效果。这个版本实现起来要比上一个要简单,不过之前那种手动修改也有它的作用,大家在上网的时候都见过很多网站上有手动切换显示语言的链接吧(像微软、谷歌的网站上都有)!这是为了让那些身在国外的人能够自己切换到母语的显示页面而做的。这两种方式各有各的作用。


两种方式各司其职,具体要怎么用还得看你想要实现什么样的需求了。这是两种不同的国际化实现方法,今天把这种自动识别浏览器语言的方法也写出来供大家把玩把玩,希望各位玩的开心。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值