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:-