Externalizing text messages

So far, in all our View files we hardcoded text values for all the labels. As an example, take our addProduct.jsp file—for the productId input tag, we have a label tag with the hardcoded text value as Product id:

<label class="control-label col-lg-2 col-lg-2" for="productId">Product Id</label> 

Externalizing these texts from a View file into a properties file will help us to have a single centralized control for all label messages. Moreover, it will help us to make our web pages ready for internationalization. We will talk more about internationalization in Chapter 6, Internalize Your Store with Interceptor, but, in order to perform internalization, we need to externalize the label messages first. So now you are going to see how to externalize locale-sensitive text messages from a web page to a property file.

Time for action - externalizing messages

Let's externalize the labels texts in our addProduct.jsp:

  1. Open our addProduct.jsp file and add the following tag lib reference at the top:
          <%@ taglib prefix="spring"       
          uri="http://www.springframework.org/tags" %> 
    
  2. Change the productId's <label> tag value Product Id to <spring:message code="addProduct.form.productId.label"/>. After changing your productId's <label> tag value, it should look as follows:
          <label class="control-label col-lg-2 col-lg-2" 
          for="productId"> <spring:message 
          code="addProduct.form.productId.label"/> </label> 
    
  3. Create a file called messages.properties under /src/main/resources in your project and add the following line to it:
          addProduct.form.productId.label = New Product ID 
    
  4. Now open our web application context configuration file WebApplicationContextConfig.java and add the following bean definition to it:
          @Bean 
          public MessageSource messageSource() {  
             ResourceBundleMessageSource resource = new        
             ResourceBundleMessageSource(); 
             resource.setBasename("messages"); 
             return resource;     
          } 
    
  5. Now run our application again and enter the URL http://localhost:8080/webstore/market/products/add. You will be able to see the added product page with the product ID label showing as New Product ID.

What just happened?

Spring MVC has a special a tag called <spring:message> to externalize texts from JSP files. In order to use this tag, we need to add a reference to a Spring tag library; that's what we did in step 1. We just added a reference to the Spring tag library in our addProduct.jsp file:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 

In step 2, we just used that tag to externalize the label text of the product ID input tag:

<label class="control-label col-lg-2 col-lg-2" for="productId"> <spring:message code="addProduct.form.productId.label"/> </label> 

Here, an important thing you need to remember is the code attribute of the <spring:message> tag; we have assigned the value addProduct.form.productId.label as the code for this <spring:message> tag. This code attribute is a kind of key; at runtime Spring will try to read the corresponding value for the given key (code) from a message source property file.

We said that Spring will read the message's value from a message source property file, so we need to create that property file. That's what we did in step 3. We just created a property file with the name messages.properties under the resource directory. Inside that file, we just assigned the label text value to the message tag code:

addProduct.form.productId.label = New Product ID 

Remember, for demonstration purposes I just externalized a single label, but a typical web application will have externalized messages for almost all tags; in that case messages.properties file will have many code-value pair entries.

Okay, we created a message source property file and added the <spring:message> tag in our JSP file, but to connect these two we need to create one more Spring bean in our web application context for the org.springframework.context.support.ResourceBundleMessageSource class with the name messageSource—we did that in step 4:

@Bean 
public MessageSource messageSource() {  
   ResourceBundleMessageSource resource = new ResourceBundleMessageSource(); 
   resource.setBasename("messages"); 
   return resource;     
} 

One important property you need to notice here is the basename property; we assigned the value messages for that property. If you remember, this is the name of the property file that we created in step 3.

That is all we did to enable the externalizing of messages in a JSP file. Now if we run the application and open up the Add products page, you can see that the product ID label will have the same text as we assigned to the addProdcut.form.productId.label code in the messages.properties file.

Have a go hero - externalizing all the labels from all the pages

I just showed you how to externalize the message for a single label; you can now do that for every single label available in all the pages.

..................Content has been hidden....................

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