Name:
Location: Bangalore, India

Saturday, September 22, 2007

Application Resources implementation details

JSP pages:

<bean:message key="tbu.title.mainTitle" bundle="abc"/>

Struts-config.xml

<message-resources null="false" parameter="resources.MessageResources"/>
<message-resources null="false" key="abc" parameter="resources.ApplicationResources"/>

null="false" ==> Why are we giving this.


If a particular key is not defined in properties file, then we get a runtime exception when we launch a page. By specifying null=”false” we nullify the nullpointerexception and user just sees a question mark.

As you can see key attribute of message-resources tag should map to bundle attribute of bean:message tag.

Properties file:

ApplicationResources.properties & MessageResources.properties file should be placed in WEB-INF\Classes\resources\ folder.

Contents of ABCApplicationResources.properties

tbu.title.mainTitle=ABC Create New XYZ
tbu.label. XYZ Name= XYZ Name
request.title.tableTitle=Request Summary
request.btn.clone=Clone

Loading the ApplicationResources.properties

import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class ABCApplicationServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public static Properties appResources = new Properties();

public void init() throws ServletException {
try{
InputStream is = getServletContext().getResourceAsStream("/WEB-INF/classes/resources/ABCApplicationResources.properties");
appResources.load(is);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}

web.xml mapping to load the ApplicationServlet on application start-up

<servlet-name>ABCApplicationServlet</servlet-name>
<servlet-class>com.abc.xyz.applicationName.servlets.ABCApplicationServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>

0 Comments:

Post a Comment

<< Home