struts实现语言自动切换

来源:互联网 发布:java buffer 编辑:程序博客网 时间:2024/05/19 08:39

需求

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>

效果图


感谢您的宝贵时间···


0 0