您的位置:首页 > 编程语言 > Java开发

struts实现语言自动切换

2017-03-17 20:37 344 查看
需求

1、通过点击需要转换的语言,来达到显示的数据为相应的语言。

demo

Action

/**
* Created by zhou on 2017/3/17.
*/
public class ChangeLanguageAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String lang = request.getParameter("lang");
Locale locale = Locale.getDefault();
if("zh".equals(lang)){
locale = new Locale("zh","CN");
}else if ("en".equals(lang)){
locale = new Locale("en","US");
}
this.setLocale(request,locale);
return mapping.findForward("index");
}
}

jsp
<%--
Created by IntelliJ IDEA.
User: zhou
Date: 2017/3/10
Time: 20:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href ="login.jsp">登录</a>
<a href="changeLang.do?lang=zh">中文</a>     <a href="changeLang.do?lang=en">英文</a>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: zhou
Date: 2017/3/11
Time: 8:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="login.do" method="post">
<bean:message key="login.form.field.username"/>: <input type="text" name="username"><br>
<bean:message key="login.form.field.password"/>: <input type="password" name="password"><br>
<input type="submit" value="<bean:message key="login.form.button.login"/>">
</form>
</body>
</html>


sturts-config.xml
<action path="/changeLang"
type="com.zhou.struts.ChangeLanguageAction"
>
<forward name="index" path="/index.jsp"/>
</action>

效果图



感谢您的宝贵时间···
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: