GWT: get locale information from server side?

Deadly 提交于 2019-12-08 01:38:52

问题


I use GWT along with Spring/Hibernate/AOP. I use an Aspect to send notification emails. In one of my Aspect, I want to get the current locale from GWT,so that I can send the localized email to the user. Is there a way to access GWT Locale data from the client side?

Thanks


回答1:


http://code.google.com/intl/es-ES/webtoolkit/doc/latest/DevGuideI18nLocale.html

has info about Locales in GWT.

I have two approaches:

1) session-less server: the method in the server that sends the email receives the locale from the client.

Let's say the interface has a method:

doStuffAndSendMails(MyObjectData myObj);

My proposal is to convert it to

doStuffAndSendMails(MyObjectData myObj, String localeStr);

and call it from the GWT client in this way:

doStuffAndSendMails(myObj, LocaleInfo.getCurrentLocale().getAsString());

2) session-aware server: I don't know if GWT allows using session... and... I prefer not to use it... but if you have to, you can send to the server the locale of the client and store it in the session...




回答2:


You can have the locale for every rpc method call without adding a locale parameter to your methods.

  1. add following to the web.xml:

    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.example.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>*.rpc</url-pattern>
    </servlet-mapping>
    
  2. and MyServlet class will be like this:

    public class MyServlet extends HttpServlet {
       @Override
       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ...
            ...
            Locale userPreferredLocale = request.getLocale();
            ...
            ...
    
        }
    }
    


来源:https://stackoverflow.com/questions/1956299/gwt-get-locale-information-from-server-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!