struts2国际化

  1. 在struts.xml中添加代码
    <!-- 通过“struts.custom.i18n.resources”属性指定国际化资源文件的基名为“message”,位于src目录下 -->
    	<constant name = "struts.custom.i18n.resources" value="message"/>
    	<constant name = "struts.i18n.encoding" value = "utf-8"/>

    注意:这里value的值是文件的基名

  2.  

    在src目录下

    加入message_zh_CN.properties

    titleone=登陆
    ttile2=欢迎页面
    head1=登陆
    head2=欢迎
    welcome=登陆成功
    form.name=用户名
    form.password=密码
    form.submit=提交

    加入 message_en_US.properties

    titleone=LoginForm
    ttile2=Login success
    head1=Login
    head2=welcome to login
    welcome=login success
    form.name=name
    form.password=password
    form.submit=submit

     

  3. 在页面前加

    <!-- 加载struts2标签库 -->
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

     

  4. 将需要使用<s:text name=../>的地方包裹在<s:i18n name="message"></s:i18n>中 ,这步非常重要,之前一直无法找到资源文件,后来看了书才知道是因为<s:text name=..>会加载<s:i18n name="message"></s:i18n>里指定的国际化资源文件,message是基名

  5. 在需要国际化的地方加上<s:text name=../>,记得写结束,比如loginForm.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
    	pageEncoding="utf-8"%>
    <!-- 加载struts2标签库 -->
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <s:i18n name="message">
    	<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><s:text name="titleone" /></title>
    </head>
    <body>
    
    	<center>
    		<span style="color: red; font-weight: bold"> <%
    //若错误信息不为空,则打印错误信息
    if (request.getAttribute("error") != null)
    {
    	out.println(request.getAttribute("error") + "<br/>");
    }
    %>
    		</span>
    		<h1>
    			<s:text name="head1" />
    		</h1>
    		<form action="login" method="post">
    			<s:text name="form.name" />
    			:<input type="text" name="username"><br>
    			<br>
    			<s:text name="form.password" />
    			:<input type="password" name="password"><br>
    			<br> <input type="submit" value="<s:text name="form.submit"/>">
    		</form>
    	</center>
    </body>
    	</html>
    </s:i18n>

     

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值