vivmal
vivmal

Reputation: 317

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

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.

Upvotes: 2

Views: 2642

Answers (2)

Denees
Denees

Reputation: 9198

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>

Upvotes: 1

codea
codea

Reputation: 1252

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.

Upvotes: 2

Related Questions