Saturday, 21 April 2012

Properties file usage in Spring framework

Create a property file with the required messages and keep the file under the project class path. below is a sample property file.

default-message.properties


menu.one=testing property file
report.mainmenu.one=Report one
report.mainmenu.two=Report two
report.mainmenu.three=Report three
report.mainmenu.four=Report four
report.mainmenu.five=Report five
---------------------------------------------------------------------------------


In your web application context xml specify the bean as below



        <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <property name="basename" value="/WEB-INF/classes/default-messages"/>
</bean>

When ever you want to use the property value in the backend you can use the Spring injection and inject the property value to the class. Example as below.

<bean name="/propertyFile.html" class="com.lnt.rg.web.ReportController" >
<property name="template" ref="basicTemplate" />
<property name="messages" ref="messageSource" />
</bean>
---------------------------------------------------------------------------------

Usage of the property value in the class. example as below.

ReportController.java


package com.lnt.rg.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.MessageSource;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.lnt.rg.common.Template;

public class ReportController extends MultiActionController {
protected final Log logger=LogFactory.getLog(getClass());
private Template template;
private MessageSource messages;

    public void setMessages(MessageSource messages) {
        this.messages = messages;
    }

public void setTemplate(Template template) {
this.template = template;
}
public ModelAndView propertyFile(HttpServletRequest request, HttpServletResponse response){
if(messages!=null){
String str=messages.getMessage("menu.one", null, null);
System.out.println("str    ::::"+str);
}else{
System.out.println("messageSourc is null");
}
return new ModelAndView("test");
}
}



As shown above the we can use the property value in backend(in java code).

------------------------------------------------------------------------------------

Getting the value from the property file to the frontend JSP. below is the code.

test.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h1><spring:message code="menu.one" /></h1>
</body>
</html>


No comments:

Post a Comment