Friday, June 29, 2012

JSTL : How to use fmt:message and ResourceBundle

There are two ways you can access values from a property file. Mostly a text value which can change in future are kept in property file and access it using fmt:message and ResourceBundle in JSP and JAVA respectively so that if the text values are changed in future it wont affect our code. We just need to change in property file and rest all will be taken care implicitly.

Lets see how to use fmt:message and ResourceBundle in JSP and JAVA respectively.

1. In JSP

If you need to access the property value in JSP, first lets create a property file lets say prop.properties. Add prop.properties in src folder of your application.

prop.properties

key1=This is value1
key2=This is value2

Add the mapping in web.xml or Spring's dispatcher-servlet.xml as shown below

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="prop" />
</bean>

Note the value="prop" it says to load prop.properties from the src folder. Now through JSP you can access the property by its key as shown below

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<fmt:bundle basename="prop">
<fmt:message key="key1"/>
<fmt:message key="key2"/>
</fmt:bundle>

2. In Java

Also you may need to access these properties in a JAVA program it could be either bean or any controller. You can access using java.util.ResourceBundle as shown below.

String value1 = ResourceBundle.getBundle("prop").getString("key1");
String value2 = ResourceBundle.getBundle("prop").getString("key2");

Note : You may get below error

org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/fmt cannot be resolved in either web.xml or the jar files deployed with this application

To solve above error you need to include jstl-1.2.jar in your application's classpath. It could be possible that your application is not able to detect jstl-1.2.jar file.

Javascript : Encode special characters

In Javascript, you may required to escape special characters from a text at some point of time and later you may need to retrieve the original text. This may be because of some logic or for passing as argument in some function. The basic is very simple we will write a encode function which will encode the special characters and a decode function which will get the original text out of encoded text.

Please use the below functions in your code accordingly.

<script language="Javascript">


   var toEncode = new Array(" ","!","@","#","$","%","^","&","*","(",")","-","_","=","+",":","\;",".","\"","'","\\","/","?","<",">","~","[","]","{","}","`");
 var toDecode = new Array("%20","%21","@","%23","%24","%25","%5E","%26","*","%28","%29","-","_","%3D","+","%3A","%3B",".","%22","%27","%5C","/ /","%3F","%3C","%3E","%7E","%5B","%5D","%7B","%7D","%60");

 function encode(val)
 {
  for (var i = 0; i < toEncode.length; i++)
  {
   val = val.replace(toEncode[i],toDecode[i]);
  }
  
  return val;
 }

 function decode(val)
 {
  for (var i = toDecode.length; i >= 0; i--)
  {
   val = val.replace(toDecode[i],toEncode[i]);
  }
  
  return val;
 }

</script>

Monday, June 18, 2012

DWR Hello World example

DWR - Direct Web Remoting

DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible. In simple words DWR is Easy Ajax for Java. Unlike AJAX you don't have to write codes to get XHTML object or check for state change etc. To know more about DWR, click here.

DWR is supported by almost all browsers like Firefox,Internet Explorer,Opera and Safari. To know more, click here.

About the example

We will create a simple java class (i.e HelloWorld.java) with a method sayHello() which will accept a name and print Hello name. We can call this java fuction with JavaScript in a browser. Same as Ajax call without refreshing the whole page.

Below are the steps to run the example.

1. For running the below example you need to add below mentioned JAR file in lib folder of your web project.

  • dwr.jar
  • commons-logging-1.1.1.jar 

For downloading dwr.jar, click here
commons-logging-1.1.1.jar, click here.


2. Add the DWR servlet definition and mapping to your application's web.xml. This is just like defining a normal servlet.


<servlet>
  <display-name>DWR Servlet</display-name>
  <servlet-name>dwr-invoker</servlet-name> 
  <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  <init-param>
     <param-name>debug</param-name>
     <param-value>true</param-value>
  </init-param>
</servlet>
 
<servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>