Friday 15 March 2013

Create Multiple Layouts with Apache Tiles 2 and Spring 3


Create Multiple Layouts with Apache Tiles-2 and Spring-3

Step (1): Create Dynamic web project and add following libraries



Step (2):  Add the following files



MyController.java

   package com.anand.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.anand.spring.model.Contact;

@Controller
@SessionAttributes
public class MyController {
                                      @RequestMapping(value = "/addContact", method = RequestMethod.POST)
                                      public String addContact(@ModelAttribute("contact") Contact contact,
                                                                BindingResult result) {
                                                System.out.println("FirstName: " + contact.getFirstname());
                                                return "redirect:contacts.html";
                                      }
                                      @RequestMapping("/contacts")
                                      public ModelAndView viewContacts() {
                                                return new ModelAndView("contact", "command", new Contact());
                                      }
                                     
                                      @RequestMapping("/secondh")
                                      public ModelAndView viewSecondHome() {
                                                return new ModelAndView("mysecondhome", "command", new Contact());
                                      }
}


Contact.java

package com.anand.spring.model;

public class Contact {

       private String firstname;
       private String lastname;
       private String email;
       private String telephone;

       public String getFirstname() {
              return firstname;
       }

       public void setFirstname(String firstname) {
              this.firstname = firstname;
       }

       public String getLastname() {
              return lastname;
       }

       public void setLastname(String lastname) {
              this.lastname = lastname;
       }

       public String getEmail() {
              return email;
       }

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

       public String getTelephone() {
              return telephone;
       }

       public void setTelephone(String telephone) {
              this.telephone = telephone;
       }

}



contact.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="sf"%>
<!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>
       <h2>Contact Form</h2>
       <sf:form method="post" action="addContact.html">
              <table>
                     <tr>
                           <td><sf:label path="firstname">First Name</sf:label></td>
                           <td><sf:input path="firstname" /></td>
                     </tr>
                     <tr>
                           <td><sf:label path="lastname">Last Name</sf:label></td>
                           <td><sf:input path="lastname" /></td>
                     </tr>
                     <tr>
                           <td><sf:label path="lastname">Email</sf:label></td>
                           <td><sf:input path="email" /></td>
                     </tr>
                     <tr>
                           <td><sf:label path="lastname">Telephone</sf:label></td>
                           <td><sf:input path="telephone" /></td>
                     </tr>
                     <tr>
                           <td colspan="2"><input type="submit" value="Submit" /></td>
                     </tr>
              </table>
       </sf:form>


</body>
</html>




footer.jsp

<h1>This is footer</h1>
header.jsp

<h1>This is header</h1>

layout.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!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><tiles:insertAttribute name="title" ignore="true" /></title>
</head>
<body>
       <table border="1" cellpadding="2" cellspacing="2" align="center">
              <tr>
                     <td height="30" colspan="2"><tiles:insertAttribute name="header" />
                     </td>
              </tr>
              <tr>
                     <td height="250"><tiles:insertAttribute name="menu" /></td>
                     <td width="350"><tiles:insertAttribute name="body" /></td>
              </tr>
              <tr>
                     <td height="30" colspan="2"><tiles:insertAttribute name="footer" />
                     </td>
              </tr>
       </table>
</body>
</html>

menu.jsp
<h1>This is menu</h1>
spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

       <context:component-scan base-package="com.anand.spring.controller" />

       <bean id="viewResolver"
              class="org.springframework.web.servlet.view.UrlBasedViewResolver">
              <property name="viewClass"
                     value="org.springframework.web.servlet.view.tiles2.TilesView" />
       </bean>

       <bean id="tilesConfigurer"
              class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
              <property name="definitions">
                     <list>
                           <value>/WEB-INF/tiles.xml</value>
                     </list>
              </property>
       </bean>

</beans>


tiles.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
       <definition name="base.definition" template="/WEB-INF/jsp/layout.jsp">
              <put-attribute name="title" value="" />
              <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
              <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
              <put-attribute name="body" value="" />
              <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
       </definition>
       <definition name="contact" extends="base.definition">
              <put-attribute name="title" value="Title of the Tiles" />
              <put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />
       </definition>

       <definition name="mysecondhome" extends="base.definition">
              <put-attribute name="title" value="Hi This is second title" />
              <put-attribute name="body" value="Hi This is second Layout" />
       </definition>

</tiles-definitions>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       id="WebApp_ID" version="3.0">
       <display-name>springform</display-name>
       <welcome-file-list>
              <welcome-file>index.jsp</welcome-file>
       </welcome-file-list>

       <servlet>
              <servlet-name>spring</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
       </servlet>

       <servlet-mapping>
              <servlet-name>spring</servlet-name>
              <url-pattern>*.html</url-pattern>
       </servlet-mapping>

</web-app>




Index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
<!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>

       <a href="contacts.html">Click here</a>
      
       <a href="secondh.html">Second Layout</a>


</body>
</html>