Thursday, 28 August 2014

Fetch Whole Database in one Query in hibernate

Hibernate is widely used ORM tool for java. It have lot's of Awesome feature and tricks to manage Entities,
Implicit Inheritance is one of them. You can use a parent to fetch some entity and it will load it's some entities.

Every entity is sub class of Object class. If you create a criteria for Object class you will get all the entities data.

Monday, 10 March 2014

Starting Apache Velocity with struts

What is Velocity?

Ans: Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code.
When Velocity is used for web development, Web designers can work in parallel with Java programmers to develop web sites according to the Model-View-Controller (MVC) model, meaning that web page designers can focus solely on creating a site that looks good, and programmers can focus solely on writing top-notch code. Velocity separates Java code from the web pages, making the web site more maintainable over its lifespan and providing a viable alternative to Java Server Pages (JSPs) or PHP.
Check (http://velocity.apache.org/) for details.

For first App you need jars required for velocity.


  1.       Create a Dynamic web application.
  2.       Put all the required jars in /WEB-INF/lib  directory of your application

antlr-2.7.5.jar
avalon-logkit-2.1.jar
commons-collections-3.2.1.jar
commons-lang-2.4.jar
commons-logging-1.1.jar
jdom-1.0.jar
log4j-1.2.12.jar
maven-ant-tasks-2.0.9.jar
oro-2.0.8.jar
servletapi-2.3.jar
werken-xpath-0.9.4.jar

Note: you can find all these jars in lib folder of downloaded zip of Velocity (Ex. velocity-1.7.zip).

  3.       Map a servlet in your web.xml

<servlet>
    <servlet-name>velocity</servlet-name>
    <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>
  </servlet>
  <servlet-mapping>
       <servlet-name>velocity</servlet-name>
       <url-pattern>*.vm</url-pattern>
  </servlet-mapping>


  4.       In case of Struts 2.x map struts filter

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*</url-pattern> 
</filter-mapping>


  5.  Create a UserAction


package com.ankit.web;

import com.opensymphony.xwork2.ActionSupport;

public class UserLogin extends ActionSupport {

       private String name;
       private String email;
       private int random;
      
       public String getName() {
              return name;
       }

       public void setName(String name) {
              this.name = name;
       }

       public String getEmail() {
              return email;
       }

       public void setEmail(String email) {
              this.email = email;
       }

       public int getRandom() {
              return random;
       }

       public void setRandom(int random) {
              this.random = random;
       }

       public  String login() {
             
              random=(int)( Math.random()*10);
              System.out.println("Name->"+name+" random->"+random);
              return SUCCESS;
       }
}

  6.  Struts.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default" >
       <action name="login" class="com.ankit.web.UserLogin" method="login" >
              <result>index.vm</result>
       </action>
</package>
</struts>

  7.  index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="/struts-tags"  prefix="s" %>
    <%@taglib uri="http://velocity.apache.org/velocity-view"  prefix="velocity" %>
<!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>
<s:form action="login" >
<s:textfield  name="name" label="Name" labelSeparator=":-" ></s:textfield>
<s:textfield  name="Email" label="Email" labelSeparator=":-" ></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>

  8.  index.vm

#set($a="Welcome")
$a
<b>$name</b> <br>
Your Email is <b>$email</b>
<br>
Generated Random Number is in RED
#set($i=10)
#foreach($i in [1..10])
<b
#if($i==$random)
style="color:red;"
#else
style="color:green;"
#end >$i</b>&nbsp;
#set($i=$i+1)
#end
<br>Check if user is  Admin
#if($name=="Admin")
<b>User is Admin</b>
#end

Output:- 

Page1

   

        Page2

 


Download:-



Tuesday, 11 February 2014

Date Converter for Different Timezone and Formats

Convert Date from one timezone to another without hassle. Covert a different format to different format.

Features:-
1. Easy to use.
2. Free of cost.
3. Light weight.
4. Pure Java tool.

If you want to calculate your age or want to know how much younger or older your friend than you, Have a date in different timezone and want to know time in your timezone or whats time in different timezone now. All this is just one download and one click away. May be you don't have internet next time just download it once and use it. Any platform and place just you need Java enabled (Today most of the systems have Java).

Download

Want to explore more looking for source code Download Source

Can't find your desired format write to me.

Thanks.