菜鸟学SSH(三)——Struts2国际化自动检测浏览器语言版
2013-11-27 21:06
519 查看
前几天发了一篇Struts国际化的博客——《菜鸟学习SSH(二)——Struts2国际化手动切换版》,有网友提了一个意见,见下图:
于是就有了下面修改的版本:
web.xml
struts.xml
登录页
OK,这样将浏览器的语言设置成中文,那么页面就以中文显示;把浏览器语言设置成英文,页面就以英文显示。这一个版本就是上面那位网友说的那种效果。这个版本实现起来要比上一个要简单,不过之前那种手动修改也有它的作用,大家在上网的时候都见过很多网站上有手动切换显示语言的链接吧(像微软、谷歌的网站上都有)!这是为了让那些身在国外的人能够自己切换到母语的显示页面而做的。这两种方式各有各的作用。
两种方式各司其职,具体要怎么用还得看你想要实现什么样的需求了。这是两种不同的国际化实现方法,今天把这种自动识别浏览器语言的方法也写出来供大家把玩把玩,希望各位玩的开心。
于是就有了下面修改的版本:
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,这样将浏览器的语言设置成中文,那么页面就以中文显示;把浏览器语言设置成英文,页面就以英文显示。这一个版本就是上面那位网友说的那种效果。这个版本实现起来要比上一个要简单,不过之前那种手动修改也有它的作用,大家在上网的时候都见过很多网站上有手动切换显示语言的链接吧(像微软、谷歌的网站上都有)!这是为了让那些身在国外的人能够自己切换到母语的显示页面而做的。这两种方式各有各的作用。
两种方式各司其职,具体要怎么用还得看你想要实现什么样的需求了。这是两种不同的国际化实现方法,今天把这种自动识别浏览器语言的方法也写出来供大家把玩把玩,希望各位玩的开心。
相关文章推荐
- 菜鸟学SSH(三)——Struts2国际化自动检测浏览器语言版
- 菜鸟学SSH(二)——Struts2国际化手动切换版
- struts2国际化--更改浏览器默认语言环境
- 菜鸟学SSH(二)——Struts2国际化手动切换版
- struts2国际化设置浏览器语言
- 菜鸟学SSH(二)——Struts2国际化手动切换版
- struts根据浏览器自动选择文字语言(国际化)
- struts2国际化多语言自动切换和手动切换
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- Web框架——Struts2国际化实现用户自行选择语言
- 检测浏览器是否自动屏蔽了弹出窗口
- web国际化,使用accept-language+cookie自动选择语言跳转页面!
- 网页自动判断客户端浏览器使用的语言,并显示相应语言的页面
- SSH整合问题:struts2-spring-plugin自动注入action的属性所产生的问题
- springmvc国际化 基于浏览器语言的国际化配置
- 浏览器语种检测,适合于多语言版本的站点
- Java程序员从笨鸟到菜鸟之(六十六)细谈struts2(十二)struts2国际化底层大揭秘
- struts2 国际化,修改本地语言
- QT国际化示例, 检测系统语言,设置适合语言,按键切换显示语言
- 菜鸟学SSH(五)——Struts2上传文件