1. Where does SessionLocaleResolver store the user's selected Locale?
Why:SessionLocaleResolver keeps the Locale in the user's HttpSession. The same locale is used for the next requests until the session ends.
2. When is SessionLocaleResolver a good choice instead of the default header-based resolver?
Why: A header-based resolver depends on the browser's Accept-Language header. SessionLocaleResolver lets users pick a language and remembers it in the session.
3. How long does a locale stored by SessionLocaleResolver stay active for a user?
Why: The locale lives in the HttpSession, so it is used for all requests of that session. When the session ends, the stored locale is gone.
4. What does setDefaultLocale(..) do on a SessionLocaleResolver?
Why: The default locale is only a fallback. If the session has no locale yet, the default one is used. After a locale is set in the session, that one wins.
5. Which interface must a bean named localeResolver implement so that Spring MVC can use it?
Why: Spring MVC looks for a bean of type LocaleResolver to find the current locale. SessionLocaleResolver is one of its implementations.
6. Which method of LocaleResolver is used to change the current locale?
Why:LocaleResolver has resolveLocale(..) to read the locale and setLocale(..) to change it.
7. What is the job of LocaleChangeInterceptor?
Why: On every request, LocaleChangeInterceptor looks for a request parameter. If it is present, it calls setLocale(..) on the current LocaleResolver.
8. Why is LocaleChangeInterceptor often better than setting the locale inside a controller method?
Why: If many pages can change the language, doing it in one controller is not enough. An interceptor handles the locale parameter for all requests in one place.
9. What kind of Spring MVC component is LocaleChangeInterceptor?
Why:LocaleChangeInterceptor is an implementation of HandlerInterceptor. It must be registered as an interceptor in the MVC configuration.
10. What does setParamName(..) configure on a LocaleChangeInterceptor?
Why: The interceptor reads the new locale from a request parameter. setParamName(..) sets that parameter's name. The default name is locale.
11. What is the default request parameter name used by LocaleChangeInterceptor?
Why: If you do not call setParamName(..), the interceptor looks for a request parameter named locale.
12. Which value is a valid locale text that LocaleChangeInterceptor can read from a request parameter?
Why: The parameter value uses the same text format as Locale.toString(), for example de_DE or fr_FR.
13. What is the output of this code?
Locale l = Locale.GERMANY;
System.out.println(l);
Why:Locale.toString() prints the language code and the country code joined by an underscore. For Locale.GERMANY it is de_DE.
14. What is the output of this code?
Locale l = Locale.FRANCE;
System.out.println(l.getLanguage());
Why:getLanguage() returns only the lowercase language code, which is fr for Locale.FRANCE.
15. A user sends a request with ?lang=de_DE. What must be changed so the locale is updated?
LocaleChangeInterceptor i =
new LocaleChangeInterceptor();
registry.addInterceptor(i);
Why: The interceptor reads the parameter named by setParamName(..). The default name is locale, so lang must be configured.
16. Complete the bean method so that English (US) is used when the session has no locale yet.
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver r =
new SessionLocaleResolver();
// complete here
return r;
}
Why:setDefaultLocale(..) defines the fallback locale for sessions that do not have a locale stored yet.
17. A user opens /home with no locale parameter. The default locale is Locale.US. Then the user sends the requests below in the same session. What locale is used for the last request?
1. GET /home
2. GET /home?locale=fr_FR
3. GET /about
Why: Assuming LocaleChangeInterceptor is registered with the default parameter name, request 2 sets fr_FR in the session. Request 3 has no parameter, so the locale stored in the session is still used.
18. What does a controller handler method receive when it declares a Locale parameter?
Why: Spring MVC fills a Locale handler argument with the locale resolved for the current request by the configured LocaleResolver.
19. What will the dropdown below send to the server when the user selects German and submits?
Why: A form field sends its name and the selected option's value. Here the name is localeCode and the value is de_DE.
20. A config class has the code below. A user sends POST /?lang=fr_FR. What is stored in the session?
@Bean
public LocaleResolver localeResolver() {
return new SessionLocaleResolver();
}
@Override
public void addInterceptors(
InterceptorRegistry registry) {
LocaleChangeInterceptor i =
new LocaleChangeInterceptor();
i.setParamName("lang");
registry.addInterceptor(i);
}
Why: The interceptor is configured to read the lang parameter. It converts fr_FR into a Locale and passes it to the resolver, which stores it in the session.