struts2的国际化i18n

2024-05-27 05:32
文章标签 国际化 struts2 i18n

本文主要是介绍struts2的国际化i18n,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文主要介绍struts2的国际化,首先在struts.xml文件中配置 常量值

<constant name="struts.custom.i18n.resources" value="app"/>(app为properties部分文件名,全名为app_en_US.properties或app_zh_CN.properties)

app_en_US.propertie内容

city=NEW YORK
street=51 AVENUE
reg.error.name.null=username cannot null
reg.error.name.length=username's  length should be 5-10
country=CHINA
welcome.msg=welcome,{0}
submit=SUBMIT

app_zh_CN.properties内容

city=中国
street=山海
reg.error.name.null=用户名不能为空
reg.error.name.length=用户名长度应该在5-10之间
country=中国
welcome.msg=欢迎你,{0}
submit=注册
一.java中获取properties文件中的信息(与struts2无关,properties需直接放在src目录下)
public class Testi18n{public static void main(String [] args){ResourceBundle rb=ResourceBundle.getBundle("app",Locale.US);String city=rb.getString("city");System.out.println(city);}}
输出为  NEW YORK

二.在jsp页面中获取properties文件中的信息

i18n.jsp文件:

<%@page import="org.omg.CORBA.Request"%>
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<span style="white-space:pre">	</span><hr>
<span style="white-space:pre">	</span><%
<span style="white-space:pre">		</span>request.setAttribute("name", "SIEGE");
<span style="white-space:pre">	</span>%>
<span style="white-space:pre">	</span><!--  使用s:text 标签输出国际化消息-->  
<span style="white-space:pre">	</span><s:property value="getText('street')"/>
<span style="white-space:pre">	</span> <!--使用s:param为国际化信息的占位符传入参数-->  
<span style="white-space:pre">	</span><s:text name="welcome.msg">
<span style="white-space:pre">		</span><s:param><s:property value="#request.name"/></s:param>
<span style="white-space:pre">	</span></s:text>
<span style="white-space:pre">	</span> <!-- 在表单元素中使用key来指定国际化消息的key-->  
<span style="white-space:pre">	</span><s:form>
<span style="white-space:pre">		</span>  <s:submit name="submit" key="submit" />  
<span style="white-space:pre">	</span><!--通过ognl的%符号来获取值-->
<span style="white-space:pre">		</span>  <s:submit value="%{getText('submit')}"></s:submit>
<span style="white-space:pre">	</span></s:form>
</body>
</html>

输出结果为:





这篇关于struts2的国际化i18n的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1006610

相关文章

vue+el国际化-东抄西鉴组合拳

vue-i18n 国际化参考 https://blog.csdn.net/zuorishu/article/details/81708585 说得比较详细。 另外做点补充,比如这里cn下的可以以项目模块加公共模块来细分。 import zhLocale from 'element-ui/lib/locale/lang/zh-CN' //引入element语言包const cn = {mess

Struts2与struts1与springmvc比较

最近做项目用到了struts2,之前一直是用struts1和springMVC。感觉到了struts2从很大程度上和这两个还是有很大区别的,所以今天搜集了些资料,给他们做一下对比。            Struts1官方已经停止更新,现在用的也比较少,这里主要讲一下struts2和struts1比较都有哪些不同和进步。Struts2可以说不是完全从struts1改进来的,因为

struts2(三)---struts2中的服务端数据验证框架validate

struts2为我们提供了一个很好的数据验证框架–validate,该框架可以很方便的实现服务端的数据验证。 ActionSupport类提供了一个validate()方法,当我们需要在某一个action中进行数据验证时,可以重写这个方法。数据验证往往是在客户端向服务端提交表单信息时进行的,比如execute方法负责处理表单信息并返回相应的结果,在此之前,validate会先对提交的表单信息进

struts2(二)---ModelDriven模型驱动

这篇文章是在上一篇文章(http://blog.csdn.net/u012116457/article/details/48194905)的基础上写的,大家可以先快速阅读一下上一篇。 这篇文章用来写一下struts中的模型驱动机制modelDriven 1.为什么要用modelDriven struts中Action 用来处理业务逻辑,如果向上一篇文章中的做法,把实体类的属性及get se

Struts2(一)---struts2的环境搭建及实例

刚刚接触struts2,有点懵懵懂懂,还是习惯于先写代码,然后慢慢来理解其中的思想。 这篇文章主要内容是strusts的环境搭建及通过一个简单的例子来理解到底是怎么使用struts来简化编程的。 1.项目结构如下如,包括必须的包 2.web.xml <?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="

rails 中i18n实现本地化

在rails中写东西时,因为默认语言是英文的,所以很多已有的提示都是英语,而这对于一个中文网站来说并不友好. 所以一般会使用i18n来实现本地化 在application.rb文件中 写入 config.i18n.default_locale = 'zh_CN' 然后在config/locales文件下 创建 zh_CN.yml文件  内容格式如下: zh

ajax+json+Struts2实现list传递(转载)

一、首先需要下载JSON依赖的jar包。它主要是依赖如下:       json-lib-2.2.2-jdk15       ezmorph-1.0.4       commons-logging-1.0.4       commons-lang-2.4       commons-collections-3.2.1       commons-beanutils      二、

Struts2 s:token/标签 防止表单多次提交

<span style="font-size:10px;">1、使用Struts2的表单标签,其中需要增加token标签。如下:……Java代码<%@ taglib uri="/struts-tags" prefix="s" %> …… <s:form action="page1" theme="simple"> <s:datetimepicker name="order.dat

struts2中s:doubleselect/标签的使用(转)

先看bean中的代码: //城市对象 public class CityBean { private int cityid; private String cityname; public int getCityid() { return cityid; } public void setCityid(int cityid) { this.cityid = cityid; } pub

strut2的简单的国际化问题

国际化的操作的原理:根据请求头中的request_locale参数来区分不同的地区,传递此请求头信息,在strut中转一圈,它会返回不同地区的资源,实现国际化: 1.书写国际化文件的资源:资源名_语言_国家.properties 例如:reource_zh_CN.properties       resource_en_US.properties    reource_zh_C