Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Monday, 1 July 2013

Integrating Struts 2.0 and tiles

I am currently evaluating some web frameworks for a pet project and was trying to implement Struts 2 with tiles. Neither the Sturts 2 website, nor the tiles website gave an easy way to integrated Struts 2 and tiles. It took me a while to get them to work together. This post describes a way I figured out how to integrate Struts 2 with tiles. Struts 2 provides a plugin for integrating tiles 2. This plugin is included in the complete bundle (struts-2.x.x.x-all.zip). The following are are the steps needed to integrate struts2 with tiles.
Skip to Sample Code
  1. Download the struts complete bundle from the struts 2 website
  2. Download tiles 2 from tiles 2 website
  3. Download the tiles dependencies from the jakarta commons site
    • Commons BeanUtils 1.7.0 or above
    • Commons Digester 1.8 or above
    • Commons Logging 1.1 or above
  4. Create the layout page, and related files (except the layout, all the other files are basic jsps )
    <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
    <table width="100%" height="100%">
    <tr height="20%">
    <td colspan="2" align="center" bgcolor="skyblue">
    <tiles:insertAttribute name="header" /></td>
    </tr>
    <tr>
    <td bgcolor="cyan" width="75%"><tiles:insertAttribute name="body" /></td>
    </tr>
    <tr height="20%">
    <td colspan="2" align="center" bgcolor="skyblue"><tiles:insertAttribute name="footer" /></td>
    </tr>
    </table>
    </body>
    </html>
  5. Create the HelloWorld Action class
    package example;

    import com.opensymphony.xwork2.ActionSupport;

    public class HelloWorld extends ActionSupport {

    public String execute() throws Exception {
    System.out.println("Hello World");
    return SUCCESS;
    }
    }
  6. Configure the Web Deployment descriptor by adding a tiles listener to the web.xml file of your web application.
    <?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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>tilesTest</display-name>
    <listener>
    <listener-class>
    org.apache.struts2.tiles.StrutsTilesListener
    </listener-class>
    </listener>
    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    </web-app>
  7. Configure struts to work with tiles, this can be done by either
    1. Extending the sturts package from "tiles-default"
      <package name="tilesTest" extends="tiles-default">
    2. OR
    3. Declaring a new "result-type", tiles, that will map to "org.apache.struts2.views.tiles.TilesResult"
      <result-types>
      <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
      </result-types>
  8. Set the type of the results in the package to "tiles"
    <result name="success" type="tiles">helloworld.home</result>

    struts.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>
    <package name="tilesTest" extends="struts-default">
    <result-types>
    <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>
    <action name="helloWorld" class="example.HelloWorld">
    <result name="success" type="tiles">helloworld.home</result>
    </action>
    </package>
    </struts>
    Note that the result "helloword.home" must match the definition name in tiles.xml file.

  9. Create definitions for tiles in WEB-INF/tiles.xml file.
    <!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="helloworld.home" template="/layouts/layout.jsp">
    <put-attribute name="header" value="/layouts/header.jsp" />
    <put-attribute name="body" value="/index.html" />
    <put-attribute name="footer" value="/layouts/footer.jsp" />
    </definition>
    </tiles-definitions>
  10. The following is a list of jar files used for this example (copied to the WEB-INF/lib directory)
    • commons-beanutils.jar
    • commons-digester-1.8.jar
    • commons-logging-1.1.1.jar
    • freemarker-2.3.8.jar
    • ognl-2.6.11.jar
    • struts2-core-2.0.11.1.jar
    • struts2-tiles-plugin-2.0.11.1.jar
    • tiles-api-2.0.5.jar
    • tiles-core-2.0.5.jar
    • tiles-jsp-2.0.5.jar
    • xwork-2.0.4.jar
  11. This example was implemented on tomcat 6.0.16, with Java 5 update 11

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...