How to do it...

Let us add view labels and titles to the previous projects ch02-web-xml and ch02-web-jc, through the following steps:

  1. In ch02-web-xml, create a messages_en_US.properties file in the srcmain esourcesconfig directory.
  2. A configuration file is composed of a code and message pair. The key is called the lookup of the message and also called the code of the message. Open messages_en_US.properties and add the following label to be used later in our title bar:
title=Creating View Titles 
  1. Add another message, but this time it needs arguments at runtime. This resulting label will be used as a content page header:
content_header=Goodbye! {0}. Last of Chapter {1}.
  1. Declare the org.springframework.context.support.ReloadableResourceBundleMessageSource API, which is responsible for loading, reading, and caching messages to the container. This object must be loaded by WebApplicationContext, containing the location of messages_en_US.properties, the encoding type of the messages, the compliance of the key lookup mechanism to its message values, and the number of seconds it loads and caches the bundle of String messages to the container:
<bean id="messageSource" 
   class="org.springframework.context.support 
.ReloadableResourceBundleMessageSource"> 
       <property name="basename" 
         value="classpath:config/messages_en_US"/> 
       <property name="defaultEncoding" value="UTF-8" /> 
       <property name="useCodeAsDefaultMessage" value="true"/> 
      <property name="cacheSeconds" value="1"/> 
</bean> 
  1. Open any view pages, let us say the view_emps.jsp, and add the Spring Taglib directive <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>. We need this Taglib to read all the messages from the properties file.
  2. Using the Spring Taglib, we use the <spring:message> tag to access the keys of all the needed labels and titles from messages_en_US.properties. To retrieve the title message and content header, use the following:
<head> 
<meta http-equiv="Content-Type" content="text/html; 
charset=ISO-8859-1"> 
<title><spring:message code="title"  /></title> 
</head> 
<h1> 
<spring:message code="content_header" arguments="Spring Fanatics,2" /> 
</h1>
  1. In ch02-web-jc, create the same messages_en_US.properties in its own srcmain esourcesconfig directory. The bundle contains the same title and content_header codes with their values. The same Spring Taglib is declared in show_emps.jsp and the same <spring:bind> techniques are used for reading both keys. The only difference is its JavaConfig way of loading the ReloadableResourceBundleMessageSource bean to SpringDispatcherConfig:
@Bean 
public MessageSource messageSource() { 
    ReloadableResourceBundleMessageSource messageSource = 
new ReloadableResourceBundleMessageSource(); 
 messageSource.setBasenames("classpath:config/messages_en_US"); 
    messageSource.setUseCodeAsDefaultMessage(true); 
    messageSource.setDefaultEncoding("UTF"); 
    messageSource.setCacheSeconds(1); 
    return messageSource; 
} 
  1. Build and deploy both projects. Open a browser and run https://localhost:8443/ch02/view_emps.html and https://localhost:8443/ch02jc/view_emps.html one at a time.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.227.82