码迷,mamicode.com
首页 > 其他好文 > 详细

struts2 资源国际化

时间:2016-06-06 06:48:00      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <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.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.custom.i18n.resources" value="app" />
  <package name="default" namespace="/" extends="struts-default">
    <action name="firstAction" class="com.huawei.s2.action.FirstAction" >
      <result name="success" >/ok.jsp</result>
    </action>
  </package>
</struts>

src下的配置文件

app_en_US.properties:

welcome.msg=welcome to beijing

app_zh_CN.properties:

welcome.msg=\u5317\u4EAC\u6B22\u8FCE\u4F60

action:

import com.opensymphony.xwork2.ActionSupport;
//ActionSupport:资源国际化及表单验证类
public class FirstAction extends ActionSupport{
  public String execute(){
    String str = getText("welcome.msg");//会根据浏览器设置的语言来读取相应的信息
    System.out.println(str+"============================");
    return "success";
  }
}

jsp:

1.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>struts2资源国际化</title>
  </head>
  <body>
    <form action="firstAction">
      <input name="uname" value="zhangsan" /><br/>
      <input type="submit" value="提交" />
    </form>
  </body>
</html>

ok.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP ‘ok.jsp‘ starting page</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>
    <h1>提交成功</h1>
  </body>
</html>

struts2 资源国际化

标签:

原文地址:http://www.cnblogs.com/hwgok/p/5562505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!