Locale Resolvers - Using Locales for i18n
With the help of spring's i18n support it can resolve the messages using client's locale.When the request comes DS looks for any configured LocaleResolver (if not it uses the default one called "LocaleResolver") and tries to find the locale using RequestContext.geLtLocale(). We have various LocaleResolvers like AcceptHeaderLocaleResolver, which checks the "accept-language" header sent by client, Like wise, CookieLocaleResolver - inspects a Cookie might exist on client's machine to see if locale is specified. If so then it uses that information and you could set when this cookie expires as well..<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="cookieName" value="clientlanguage"/> <!-- in sec. -1: cookie is not persisted (deleted when browser closed)--> <property name="cookieMaxAge" value="100000"> </bean>Similarly other classes such as SessionLocaleResolver allows to get locales form session associated with user's request. We could always change the locales by intercepting incoming requests by adding a bean called LocaleChangeInterceptor and it will detect the parameter in the request to change the locale (by calling setLocale() on LocaleResolver), Below example tell us clearly..
So when we run this example with 2 diff input urls we get following outputs..
Using Themes - ThemeResolver:
we can customise theme (overall look and feel of the UI - style sheets, images etc..) to use themes we need to implement "ThemeSource" interface. However WebApplicationContext implements this but it delegates the role of its responsibility to "ResourceBundleThemeResource" (RBTS) implementation that loads properties from class path properties (its like we loaded messages above example). To use a custom theme source implementation that loads properties from class path or to configure the base name prefix of RBTS, you need to register a bean in appl context xml with the reserved name called "themeSource" (just like "messageSource" in above example). A sample file to be used when using RBTS is as below:styleSheet=/themes/cool/style.css background=/themes/cool/img/coolBg.jpgand it can be referred in JSP as below: <spring:theme code='background'/>. By default, the RBTS uses empty base name so property files are loaded form classpath. The ResourceBundleThemeSource uses the standard Java resource bundle loading concept for i18n themes, say if we have /WEB-INF/classes/cool_fr.properties that references a special background image with French locale/text on it. Just like locale resolvers we have following ThemeResolvers so that DS will looks for a bean named "themeResolver" to find out which resolver to be used. Following example explains it all in one go.. Above examples tell us how screen text colour changes
Uploading Files
Handling Exceptions
Spring's exception handling slightly looks like servlet's <error-page> concept but its much powerful than that. In this section we learn about how to handle runtime exceptions occurs in controllers. Like we saw ThemeResolvers, LocaleResolvers, Implementations of HandlerExceptionResolver provides which handler was executing when exception was thrown. Implementing the HandlerExceptionResolver interface i.e. implementing theresolveException(Exception, Handler) method and returning a ModelAndView where we can pass model/exception related information along with custom view. You may also use the providedSimpleMappingExceptionResolver or create @ExceptionHandler methods.The SimpleMappingExceptionResolver enables you to take the class name of any exception that might be thrown and map it to a view name. This is functionally equivalent to the exception mapping feature from the Servlet API, but it is also possible to implement more finely grained mappings of exceptions from different handlers. We in this section look at @ExceptionHandler annotation which we write on methods will be invoked when any exception marked occurs. We usually write these methods in controller class. Incase if you want to it to be invoked globally (not just from this controller) then consider keeping it as a method in @ControllerAdviceclass (for which you need spring-web in pom.xml. When methods in controllers are marked with @ExceptionHandler, then any exception raised by methods marked with @RequestMapping will be handled by these exceptionhandler method. The @ControllerAdviceannotation is a component annotation, which can be used with classpath scanning. It is enabled when using the MVC namespace is used. Just like @RequestMapping method arguments, methods of methods with @ExceptionHandler are flexible too.
and the output would be like above..
No comments:
Post a Comment