开发者

struts2: After locale switching how to return on same page where user was on?

开发者 https://www.devze.com 2022-12-28 07:07 出处:网络
I am using struts2 and in my struts.xml I have written following code for locale switching - <action name=\"switchToEnglish\">

I am using struts2 and in my struts.xml I have written following code for locale switching -

<action name="switchToEnglish">
         <interceptor-ref name="i18n"/>
         <interceptor-ref name="basicStack"/>
         <result name="input">error.jsp</result>
         <result name="success">login.jsp</result>
</action>

<action name="switchToFrench">
         <interceptor-ref name="i18n"/>
         <interceptor-ref name="basicStack"/>
         <result 开发者_如何学编程name="input">error.jsp</result>
         <result name="success">login.jsp</result>
</action>

Now, after language switching same page (login.jsp) appears. But, I want to return on the page where user was before language switching.

Thanks in advance.


I had the same problem. I resolved it by passing the page name to the action(by GET or POST), then I use it in the result this way :

<action name="switchToEnglish">
     <interceptor-ref name="i18n"/>
     <interceptor-ref name="basicStack"/>
     <result name="input">error.jsp</result>
     <result name="success">%{currentPage}</result>
</action>

<action name="switchToFrench">
     <interceptor-ref name="i18n"/>
     <interceptor-ref name="basicStack"/>
     <result name="input">error.jsp</result>
     <result name="success">%{currentPage}</result>
</action>

Don't forget to set the getter/setter for "currentPage" in the action Class.

It's not the best way to do it but it was ok for my app.


Also I have made this, with an AJAX request to the LocaleAction, on success, just refresh the page with jQuery and you will stay on the same page where you was before the locale change.

script:

<script type="text/javascript">
$(document).ready(function(){

    $(".lang").click(function() {
        var id = $(this).attr("id");
        $.ajax({
            type: "POST",
            url: "locale.action?lang="+id,
            cache: false,
            success: function(){
                window.location.href='';
            }
        });
        return false;
    });
});
</script>

And the links:

       <span style="float: right;">
            <s:a id="ro" cssClass="lang">Română</s:a>
            &bull;
            <s:a id="ru" cssClass="lang">Русский</s:a>
            &bull;
            <s:a id="en" cssClass="lang">English</s:a>
        </span>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号