• 菜鸟学SSH(三)——Struts2国际化自动检测浏览器语言版


    前几天发了一篇Struts国际化的博客——《菜鸟学习SSH(二)——Struts2国际化手动切换版》,有网友提了一个意见,见下图:


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


    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,这样将浏览器的语言设置成中文,那么页面就以中文显示;把浏览器语言设置成英文,页面就以英文显示。这一个版本就是上面那位网友说的那种效果。这个版本实现起来要比上一个要简单,不过之前那种手动修改也有它的作用,大家在上网的时候都见过很多网站上有手动切换显示语言的链接吧(像微软、谷歌的网站上都有)!这是为了让那些身在国外的人能够自己切换到母语的显示页面而做的。这两种方式各有各的作用。


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



  • 相关阅读:
    mysql面试题
    Excel下载打不开
    Linux安装jdk1.8和配置环境变量
    Linux压缩、解压文件
    Linux常用命令1
    VMware下载安装及CentOS7下载安装
    ueditor的简单配置和使用
    linux的tomcat服务器上部署项目的方法
    TortoiseSVN客户端的使用说明
    CentOS 6.5系统上安装SVN服务器
  • 原文地址:https://www.cnblogs.com/liushuijinger/p/3446551.html
Copyright © 2020-2023  润新知