struts2 国际化例子图文教程

struts2 国际化例子也是在struts2框架上完成的,对搭建struts2框架有疑问的,可以参考笔者的第一篇博客!!!

1.新建action

package com.hnpi.action;
import com.opensymphony.xwork2.ActionSupport;
 
public class LoginAction extends ActionSupport{
	private static final long serialVersionUID = 8153220869598441387L;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
}

上面是action的代码!!!!

2.配置拦截器

我们要新建一个类然后配置我们的拦截器!!!

package com.hnpi.action;

import java.util.Locale;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class CheckInterceptor extends AbstractInterceptor{
	private static final long serialVersionUID = -1112495747942034188L;
 
	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		ActionContext ac = ai.getInvocationContext();
		Map session = ac.getSession();
		Locale locale = (Locale)session.get("WW_TRANS_I18N_LOCALE");
		if(locale==null){
			locale = new Locale("zh","CN");
			session.put("WW_TRANS_I18N_LOCALE",locale);
		}
		return ai.invoke();
	}
 
}

上面是拦截器的代码!!!

3.配置struts文件

然后我们在src包下新建一个struts.xml文件并配置

下面是struts文件的代码!!!!

<?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.costom.i18n.resources" value="globalMessages"/>
	<package name="default" extends="struts-default" namespace="/">
		<interceptors>
			<interceptor name="myInter" class="com.hnpi.action.CheckInterceptor"></interceptor>
		</interceptors>
		<action name="login" class="com.hnpi.action.LoginAction">
			<result name="success">/index.jsp</result>
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="myInter"></interceptor-ref>
		</action>
	</package>
</struts>

4.修改jsp页面

我们修改jsp页面,让页面效果更明显

下面是jsp页面代码!!!!

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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></title>
  </head>
  <body>
  <s:i18n name="globalMessages">
  	<s:text name="check"></s:text>:
  	<a href="login.action?request_locale=zh_CN"><s:text name="chinese"></s:text></a>  
	<a href="login.action?request_locale=en_US"><s:text name="english"></s:text></a>  
	<br/>
	<h3><s:text name="title"></s:text></h3>
	<s:form action="login" method="post">
			<table width="344" height="300">
				<tr>
					<td> </td>
				</tr>
				<tr>
					<td><s:text name="username" />
					</td>
					<td><s:textfield name="username" />
					</td>
				</tr>
				<tr>
					<td><s:text name="password" />
					</td>
					<td><s:password name="password" />
					</td>
				</tr>
				<tr>
					<td> </td>
					<td><s:submit value="%{getText('login')}" /></td>
				</tr>
			</table>
		</s:form>
	</s:i18n>
  </body>
</html>

5.新建英文资源文件

在src包下新建一个英文资源文件—zn_US

6.新建中文资源文件

在src包下新建一个中文资源文件—zh_CH
在这里插入图片描述

7.配置web.xml文件

项目结构

在这里插入图片描述

总结

项目代码有借鉴,在此感谢!!!
一个简单的国际化例子就完成了!!!
如有问题,请联系笔者:qq1967893975。

猜你喜欢

转载自blog.csdn.net/qq_40637873/article/details/83718357