Saturday, March 14, 2009

Array Binding In Spring

Sending data from JSP to controller in easy no matter whether it is string, integer or an array. Problem occurs in displaying JSP page if the data in present in command class and it is an array.
Following is sample JSP code to bind an array of names to the command class in spring. The code will both send and display the data.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="core" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>Test</title>
</head>
<body>
<form:form commandName="test" method="post" action="welcome.do" >
    <core:set var="flag" value="false" />
<!-- Create the n number of name fields if data is present -->
    <core:forEach items="${test.name}" var="n" varStatus="status">
        <form:input path="name[${status.index}]"  />
        <core:set var="flag" value="true" />
    </core:forEach>
<!-- If data is not present then create 2(default) name fields -->
    <core:if test="${flag eq false}">
            <form:input path="name"  />
            <form:input path="name"  />
    </core:if>
    <input type="submit" value="submit" />
</form:form>
</body>
</html>


In the above example String[] name; is used in the command class.
That All !!! :)

Saturday, February 28, 2009

XML Parsing

There is a high rise in information exchange between application and XML has become the standard. Here is an simple basic XML parsing example which gives an basic idea of fetching information from an XML file.

The sample XML file:
<?xml version="1.0"?>
<company name="my comapany">
<employee id="1">
<name>Mike</name>
</employee>
<employee id="2">
<name>Mark</name>
</employee> </company>
Java code to read the XML file:

package pkg;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;import org.w3c.dom.Element;
import org.w3c.dom.NodeList;import org.xml.sax.SAXException;

public class XMLReader {
public void readXML(String fileName) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document document = documentBuilder.parse(new File(fileName));
//GET THE ROOT ELEMENT
Element root = document.getDocumentElement();
System.out.println("Root: " + root.getNodeName());
System.out.println("Company Name: " + root.getAttribute("name"));
//GET THE LIST OF CHILD ELEMENTS UNDER ROOT
NodeList employeeList = root.getElementsByTagName("employee");
System.out.println("Total employees: " + employeeList.getLength());
Element employee;
String name = null, id = null;
for(int i=0;i<employeeList.getLength();i++) {
employee = (Element)employeeList.item(i);
//FETCH ID OF EMPLOYEE
if(employee.hasAttributes()) {
id = employee.getAttributes().getNamedItem("id").getNodeValue();
}
//GET NAME OF THE EMPLOYEE name = employee.getElementsByTagName("name").item(0).getTextContent();
System.out.println("Id: " + id + "\tName: " + name);
}
}
}
Happy parsing !!! :)

Wednesday, November 12, 2008

Awesome Goa Trip

Goa Trip !!! It was amazing !!!

This was the first time I went for a long trip and it was great.
We were three Soumak, Sunny and me. It was friday, and we all left our offices in hurry to catch train. Thank god the train was late by 20 minutes. We met at the station and we were in highly excited state and a big smile but also there was a little bit of concern that we haven't book the hotel in Goa. But after sometime we forgot that thing and we boarded the train. Here are some pics that I took while traveling.





I never took out camera in train but I really wanted to click some shots in and of the train and I took some shots, after all train my favorite. I just love to travel by train. As journey begins we also get in form of our masti, raste bhar masti karte gaye, and finally next morning we reach Madgaon at 6.30 in the morning.

We decided to stay near beach, so we directly went to Calangute beach by changing couple of busses. And guess what the very first person we met at Calangute solved our all issues that mean we got a decent hotel with a very decent price and bikes to ride. After getting refresh at hotel that too in just couple of hours we were ready to ride to our first destination i.e. Chapora Fort(DCH fort). And we were three and it was perfect. The scenic beauty was amazing, and then I and my camera came into action. he he.

After this fort we went to Auguda fort. It was also nice. We spent some time at this fort did some masti and came back to Baga beach. We reach there just in time to watch an awesome sunset in the Arabian Sea. It was great. We then had a long walk on the beach. We were so tired that we had our dinner and we came back to hotel. We had a plan for the disc but couldn't execute it.

But the very next morning we were again ready for the ride. This time we decided to cover South Goa. And we started it all with Baslica Of Bom Jeasus chruch. It was sunday so it was bit crowded. In front of the chruch there was St. Cathaderal Chruch. We just roam in the premises of the chruch and then we went to Shanta Durga temple. It was also nice. That's it enough of temples and churches, now we want to have some fun, so lets go to the Colva beach. Now we saw the parachute up in the air. So me and Soumak went for it. by god hawa me udne ka bhaut mann tha, to socha khwaish puri kar he leni chaheye. We went for it and enjoyed the glide. Soumak and Sunny enjoyed the bath in sea and in the mean while I clicked some amazing shots at Colva beach. That's it now we decided to go to Dona Paula. We heard that it's a good point to see sunset. Thats it we were again on the run to reach Dona Paula before sunset as it was already 1630 Hrs. The sunset from the view point was spectacular. We spent an approximately an hour at the This is not it. We had a plan for the Cruise also :). So went to the cruise. They have arrangement for 3 regional dances and disc. It was great. That's all for the Sunday evening. We came back and slept.
Next morning we had to leave by 1200 Hrs so in the morning we spent some time on Anjuna, Vagator and Calangute Beach.

That's all. We just roamed and roamed in goa for 2.5 days and enjoyed a lot.