Struts2: After switching language, how do I return to the same page the user was on?
I am using struts2 and in my struts.xml I wrote the following code to switch locales -
<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 switching the language, one page will appear (login.jsp). But I want to go back to the page where the user was before switching the language.
Thanks in advance.
+2
a source to share
2 answers
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 like this:
<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 a getter / setter for "currentPage" in the action class.
This is not the best way to do it, but it was ok for my application.
+2
a source to share
Also I did this with an AJAX request for LocaleAction, on success, just refresh the page with jQuery and you will stay on the same page where you were before changing the locale.
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 links:
<span style="float: right;">
<s:a id="ro" cssClass="lang">Română</s:a>
•
<s:a id="ru" cssClass="lang"></s:a>
•
<s:a id="en" cssClass="lang">English</s:a>
</span>
+1
a source to share