Convention Over Configuration

Convention Over Configuration ”; Previous Next Apache Tapestry follows Convention over Configuration in every aspect of programming. Every feature of the framework does have a sensible default convention. For example, as we learned in the Project Layout chapter, all pages need to be placed in the /src/main/java/«package_path»/pages/ folder to be considered as Tapestry Pages. In another sense, there is no need configure a particular Java Class as Tapestry Pages. It is enough to place the class in a pre-defined location. In some cases, it is odd to follow the default convention of Tapestry. For example, Tapestry component can have a method setupRender which will be fired at the start the rendering phase. A developer may want to use their own opiniated name, say initializeValue. In this situation, Tapestry provides Annotation to override the conventions as shown in the following code block. void setupRender() { // initialize component } @SetupRender void initializeValue() { // initialize component } Both ways of programming are valid in Tapestry. In short, Tapestry”s default configuration is quite minimal. Only the Apache Tapestry Filter (Java Servlet Filter) needs to be configured in the “Web.xml” for the proper working of the application. Tapestry provides one another way to configure application and it is called as the AppModule.java. Print Page Previous Next Advertisements ”;

Apache Tapestry – Quick Start

Apache Tapestry – Quick Start ”; Previous Next After Tapestry installation, let us create a new initial project using Maven as shown below − $ mvn archetype:generate -DarchetypeCatalog=http://tapestry.apache.org You could see the response as shown below − [INFO] Scanning for projects… [INFO] [INFO] ——————————————————————————— [INFO] Building Maven Stub Project (No POM) 1 [INFO] ——————————————————————————— [INFO] [INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generatesources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generatesources @ standalone-pom <<< [INFO] [INFO] — maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom — [INFO] Generating project in Interactive mode [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0) After Maven building all the operations, choose archetype to create Tapestry 5 QuickStart project as follows − Choose archetype − https://tapestry.apache.org → org.apache.tapestry:quickstart (Tapestry 5 Quickstart Project) https://tapestry.apache.org → org.apache.tapestry:tapestry-archetype (Tapestry 4.1.6 Archetype) Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1 Now you will get a response like what is shown below − Choose org.apache.tapestry:quickstart version: 1: 5.0.19 2: 5.1.0.5 3: 5.2.6 4: 5.3.7 5: 5.4.1 Extract the QuickStart version number as follows − Choose a number: 5: 5 Here, the QuickStart project takes the version for the option 5, “5.4.1”. Now, Tapestry archetype asks the following information one by one as follows − 5.1 groupId − Define value for property ”groupId”: : com.example 5.2 artifactId − Define value for property ”artifactId”: : Myapp 5.3 version − Define value for property ”version”: 1.0-SNAPSHOT: : 5.4 package name − Define value for property ”package”: com.example: : com.example.Myapp Now your screen asks confirmation from you − Confirm properties configuration − groupId − com.example artifactId − Myapp version − 1.0-SNAPSHOT package − com.example.Myapp Verify all the properties and confirm the changes using the option shown below − Y: : Y You would see the screen like the one shown below. [INFO] ——————————————————————————— [INFO] Using following parameters for creating project from Archetype: quickstart:5.4.1 [INFO] ——————————————————————————— [INFO] Parameter: groupId, Value: com.example [INFO] Parameter: artifactId, Value: Myapp [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: com.example.Myapp [INFO] Parameter: packageInPathFormat, Value: com/example/Myapp [INFO] Parameter: package, Value: com.example.Myapp [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: groupId, Value: com.example [INFO] Parameter: artifactId, Value: Myapp [WARNING] Don”t override file /Users/workspace/tapestry/Myapp/src/test/java [WARNING] Don”t override file /Users/workspace/tapestry/Myapp/src/main/webapp [WARNING] Don”t override file /Users/workspace/tapestry/Myapp/src/main/resources/com/ example/Myapp [WARNING] Don”t override file /Users/workspace/tapestry/Myapp/src/test/resource [WARNING] Don”t override file /Users/workspace/tapestry/Myapp/src/test/conf [WARNING] Don”t override file /Users/workspace/tapestry/Myapp/src/site [INFO] project created from Archetype in dir: /Users/workspace/tapestry/Myapp [INFO] ——————————————————————————— [INFO] BUILD SUCCESS [INFO] ——————————————————————————— [INFO] Total time: 11:28 min [INFO] Finished at: 2016-09-14T00:47:23+05:30 [INFO] Final Memory: 14M/142M [INFO] ——————————————————————————— Here, you have successfully built the Tapestry Quick Start project. Move to the location of the newly created Myapp directory with the following command and start coding. cd Myapp Run Application To run the skeleton project, use the following command. mvn jetty:run -Dtapestry.execution-mode=development You get a screen like this, [INFO] Scanning for projects… [INFO] [INFO] ——————————————————————————— [INFO] Building Myapp Tapestry 5 Application 1.0-SNAPSHOT [INFO] ——————————————————————————— …….. …….. …….. Application ”app” (version 1.0-SNAPSHOT-DEV) startup time: 346 ms to build IoC Registry, 1,246 ms overall. ______ __ ____ /_ __/__ ____ ___ ___ / /_______ __ / __/ / / / _ `/ _ / -_|_-</ __/ __/ // / /__ /_/ _,_/ .__/__/___/__/_/ _, / /____/ /_/ /___/ 5.4.1 (development mode) [INFO] Started [email protected]:8080 [INFO] Started Jetty Server As of now, we have created a basic Quick Start project in Tapestry. To view the running application in the web browser, just type the following URL in the address bar and press enter − https://localhost:8080/myapp Here, myapp is the name of the application and the default port of the application in development mode is 8080. Using Eclipse In the previous chapter, we discussed about how to create a Tapestry Quick Start application in CLI. This chapter explains about creating a skeleton application in Eclipse IDE. Let us use a Maven archetype to create skeleton application. To configure a new application, you can follow the steps given below. Step 1: Open Eclipse IDE Open your Eclipse and choose File → New → Project… → option as shown in the following screenshot. Now, choose Maven → Maven project option. Note − If Maven is not configured then configure and create a project. After selecting the Maven project, click Next and again click the Next button. After that, you will get a screen where you should choose the configure option. Once it is configured, you will get the following screen. Step 2: Catalog Configuration After the first step is done, you should click on Add Remote Catalog. Then add the following changes as shown in the following screenshot. Now, Apache Tapestry Catalog is added. Then, choose filter option org.apache.tapestry quickstart 5.4.1 as shown below. Then click Next and the following screen will appear. Step 3: Configure GroupId, ArtifactId, version and package Add the following changes to the Tapestry Catalog configuration. Then click Finish button, now we have created the first skeleton application. The first time you use Maven, project creation may take a while as Maven downloads many JAR dependencies for Maven, Jetty and Tapestry. After Maven finishes, you”ll see a new directory, MyFirstApplication in your Package Explorer view. Step 4: Run the application using Jetty server You can use Maven to run Jetty directly. Right-click on the MyFirstApplication project in your Package Explorer view and select Run As → Maven Build… you will the screen shown below. In the configuration dialog box, enter goals option as “jetty:run” then click Run button. Once Jetty is initialized, you”ll see the following screen in your console. Step 5: Run in the web browser Type the following URL to run the application in a web browser – https://loclhost:8080/MyFirstApplication Step 6: Stop the Jetty server To stop the Jetty server, click the red square icon in your console as shown below. Print Page Previous Next Advertisements ”;

Apache Tapestry – Installation

Apache Tapestry – Installation ”; Previous Next In this chapter, we will discuss how to install Tapestry on our machine. Prerequisite Tapestry”s only dependency is Core Java. Tapestry is developed independently without using any third party library / framework. Even the IoC library used by tapestry is developed from the scratch. Web application written in tapestry can be built and deployed from console itself. We can use Maven, Eclipse and Jetty to improve the development experience. Maven provides quick start application templates and options to host application in Jetty, Java”s de-facto development server. Eclipse provides extensive project management features and integrates well with maven. An ideal tapestry application development needs the following − Java 1.6 or later Apache Maven Eclipse IDE Jetty Server Verify Maven Installation Hopefully, you have installed Maven on your machine. To verify the Maven installation, type the command given below − mvn –version You could see the response as shown below − Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-1110T22:11:47+05:30) Maven home: /Users/workspace/maven/apache-maven-3.3.9 Java version: 1.8.0_92, vendor: Oracle Corporation Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home/jre Default locale: en_US, platform encoding: UTF-8 OS name: “mac os x”, version: “10.11.4”, arch: “x86_64”, family: “mac” If Maven is not installed, then download and install the latest version of maven by visiting the Maven website. Download Tapestry The latest version of tapestry is 5.4 and can be downloaded from the Tapestry website. It is enough to download the binary package. If we use the Maven Quick Start Template, then it is not necessary to download Tapestry separately. Maven automatically downloads the necessary Tapestry Jars and configures the application. We will discuss how to create a basic Tapestry Application using Maven in the next chapter. Print Page Previous Next Advertisements ”;

Apache Tapestry – Home

Apache Tapestry Tutorial PDF Version Quick Guide Resources Job Search Discussion Apache Tapestry is an open source, component based web framework written in Java. Tapestry was developed by “Howard Lewis Ship” and later open sourced and included into the Apache Foundation. It became a top-level Apache Project in 2006. Tapestry can work under any application server and easily integrate with all back ends like Spring, Hibernate, etc. This tutorial will explore the Architecture, Setup, Quick Start Guide, Tapestry Components and finally walk through with Simple Applications. Audience This is a tutorial for Java programmers and other people who are aspiring to make a career in Java Web Framework using Tapestry. This tutorial will give you enough understanding on creating Tapestry Web Applications. Prerequisites Before proceeding with this tutorial, you need to have a sound knowledge of core Java, particularly on Annotations, Basic Understanding of Web Application, Basic Client Side Programming (HTML, CSS & JavaScript) and Basic Working Knowledge of Eclipse IDE. Print Page Previous Next Advertisements ”;

Apache Tapestry – Discussion

Discuss Apache Tapestry ”; Previous Next Apache Tapestry is an open source, component based web framework written in Java. Tapestry was developed by “Howard Lewis Ship” and later open sourced and included into the Apache Foundation. It became a top-level Apache Project in 2006. Tapestry can work under any application server and easily integrate with all back ends like Spring, Hibernate, etc. This tutorial will explore the Architecture, Setup, Quick Start Guide, Tapestry Components and finally walk through with Simple Applications. Print Page Previous Next Advertisements ”;

Apache Tapestry – Architecture

Apache Tapestry – Architecture ”; Previous Next Tapestry tries to use the available features of Java as much as possible. For example, all Tapestry pages are simply POJOs. It does not enforce any custom interfaces or base class to write the application. Instead, it uses Annotation (a light weight option to extend the functionality of a Java class) to provide features. It is based on battle-tested Java Servlet API and is implemented as a Servlet Filter. It provides a new dimension to the web application and the programming is quite Simple, Flexible, Understandable and Robust. Workflow Let us discuss the sequence of action taking place when a tapestry page is requested. Step 1 − The Java Servlet receives the page request. This Java Servlet is the configured in such a way that the incoming request will be forwarded to tapestry. The configuration is done in the web.xml as specified in the following program. Filter and Filter Mapping tag redirects all the request to Tapestry Filter. <!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” “http://java.sun.com/dtd/web-app_2_3.dtd”> <web-app> <display-name>My Tapestry Application</display-name> <context-param> <param-name>tapestry.app-package</param-name> <param-value>org.example.myapp</param-value> </context-param> <filter> <filter-name>app</filter-name> <filter-class>org.apache.tapestry5.TapestryFilter</filter-class> </filter> <filter-mapping> <filter-name>app</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> Step 2 − The Tapestry Filter calls the HttpServletRequestHandler Service by its Service() method. Step 3 − HttpServletRequestHandler stores the request and response in RequestGlobals. It also wraps the request and response as a Request and Response object and sends it to the RequestHandler. Step 4 − The RequestHandler is an abstraction on top of HttpServletRequest of Servlet API. Some of the salient feature of the tapestry is done in RequestHandler section. The feature of tapestry can be extended by writing a filter in RequestHandler. RequestHandler provides several build-in filters, which include − CheckForUpdates Filter − Responsible for live class reloading. This filter checks the java classes for changes and update the application as necessary. Localization Filter − Identify the location of the user and provide localization support for the application. StaticFiles Filter − Identify the static request and aborts the process. Once the process is aborted, Java Servlet takes control and process the request. Error Filter − Catches the uncaught exception and presents the exception report page. The RequestHandler also modifies and stores the request and response in the RequestQlobalsand invokes the MasterDispatcher service. Step 5 − The MasterDispatcher is responsible for rendering the page by calling several dispatchers is a specific order. The four-main dispatchers called by MasterDispatcher is as follows − RootPath Dispatcher − It recognizes the root path “/” of the request and render the same as Start page. Asset Dispatcher − It recognized the asset (Java assets) request by checking the url pattern /assets/ and sends the requested assets as byte streams. PageRender Dispatcher − Bulk of the tapestry operations are done in PageRender Dispatcher and the next dispatcher Component Dispatcher. This dispatcher recognizes the particular page of that request and its activation context (extra information). It then renders that particular page and sends it to the client. For example, if the request url is /product/12123434, the dispatcher will check if any class with name product/12123434 is available. If found, it calls product/12123434 class, generate the response and send it to the client. If not, it checks for product class. If found, it calls product class with extra information 121234434, generates the response and sends it to the client. This extra information is called Activation Context. If no class is found, it simply forwards the request to Component Dispatcher. Component Dispatcher − Component Dispatcher matches the URL of the page with the pattern – /<class_name>/<component_id>:<event_type>/<activation_context>. For example, /product/grid:sort/asc represents the product class, grid component, sortevent type and asc activation context. Here, event_type is optional and if none is provided, the default event type action will be triggered. Usually, the response of the component dispatcher is to send a redirect to the client. Mostly, the redirect will match PageRender Dispatcher in the next request and proper response will be send to the client. Print Page Previous Next Advertisements ”;

Apache Tapestry – Templates

Apache Tapestry – Templates ”; Previous Next Let us consider the Tapestry XML Template in this section. XML Template is a well-formed XML document. The presentation (User Interface) layer of a Page is XML Template. An XML Template have normal HTML markup in addition to the items given below − Tapestry Namespace Expansions Elements Components Let us now discuss them in detail. Tapestry Namespace Tapestry Namespaces are nothing but XML Namespaces. Namespaces should be defined in the root element of the template. It is used to include Tapestry Components and component related information in the Template. The most commonly used namespaces are as follows − xmlns:t = “https://tapestry.apache.org/schema/tapestry_5_4.xsd” — It is used to identify Tapestry”s Elements, Components and Attributes. xmlns:p = “tapestry:parameter” — It is used to pass arbitrary chunks of code to components. An example of Tapestry Namespace is as follows − <html xmlns:t = “https://tapestry.apache.org/schema/tapestry_5_3.xsd” xmlns:p = “tapestry:parameter”> <head> <title>Hello World Page</title> </head> <body> <h1>Hello World</h1> <t:eventlink page = “Index”>refresh page</t:eventlink> </body> </html> Expansions Expansion is simple and efficient method to dynamically change the XML Template during rendering phase of the Page. Expansion uses ${<name>} syntax. There are many options to express the expansion in the XML Template. Let us see some of the most commonly used options − Property Expansions It maps the property defined in the corresponding Page class. It follows the Java Bean Specification for property definition in a Java class. It goes one step further by ignoring the cases for property name. Let us change the “Hello World” example using property expansion. The following code block is the modified Page class. package com.example.MyFirstApplication.pages; public class HelloWorld { // Java Bean Property public String getName { return “World!”; } } Then, change the corresponding XML Template as shown below. <html xmlns:t = “http://tapestry.apache.org/schema/tapestry_5_4.xsd”> <head> <title>Hello World Page</title> </head> <body> <!– expansion –> <h1>Hello ${name}</h1> </body> </html> Here, we have defined name as Java Bean Property in the Page class and dynamically processed it in XML Template using expansion ${name}. Message Expansion Each Page class may or may not have an associated Property file – «page_name».properties in the resources folder. The property files are plain text files having a single key / value pair (message) per line. Let us create a property file for HelloWorld Page at – “/src/main/resources/com/example/MyFirstApplication/pages/helloworld.properties” and add a “Greeting” message. Greeting = Hello The Greeting message can be used in the XML Template as ${message:greeting} <html xmlns:t = “http://tapestry.apache.org/schema/tapestry_5_4.xsd”> <head> <title>Hello World Page</title> </head> <body> <!– expansion –> <h1>${message:greeting} ${name}</h1> </body> </html> Elements Tapestry has a small set of elements to be used in XML Templates. Elements are predefined tags defined under the Tapestry namespace − https://tapestry.apache.org/schema/tapestry_5_4.xsd Each element is created for a specific purpose. The available tapestry elements are as follows − <t:body> When two components are nested, the parent component”s template may have to wrap the child component”s template. The <t:body> element is useful in this situation. One of the uses of <t:body> is in the Template Layout. In general, the User Interface of a web application will have a Common Header, Footer, Menu, etc. These common items are defined in an XML Template and it is called Template Layout or Layout Component. In Tapestry, it needs to be created by an application developer. A Layout Component is just another component and is placed under the components folder, which has the following path – src/main/«java|resources»/«package_name»/components. Let us create a simple layout component called MyCustomLayout. The code for MyCustomLayout is as follows − <!DOCTYPE html> <html xmlns:t = “http://tapestry.apache.org/schema/tapestry_5_4.xsd”> <head> <meta charset = “UTF-8” /> <title>${title}</title> </head> <body> <div>Sample Web Application</div> <h1>${title}</h1> <t:body/> <div>(C) 2016 TutorialsPoint.</div> </body> </html> package com.example.MyFirstApplication.components; import org.apache.tapestry5.*; import org.apache.tapestry5.annotations.*; import org.apache.tapestry5.BindingConstants; public class MyCustomLayout { @Property @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL) private String title; } In the MyCustomLayout component class, we declared a title field and by using annotation, we have made it mandatory. Now, change HelloWorld.html template to use our custom layout as shown in the code block below. <html> t:type = “mycustomlayout” title = “Hello World Test page” xmlns:t = “http://tapestry.apache.org/schema/tapestry_5_4.xsd”> <h1>${message:greeting} ${name}</h1> </html> We can see here that the XML Template does not have head and body tags. Tapestry will collect these details from the layout component and the <t:body> of the layout component will be replaced by the HelloWorld Template. Once everything is done, Tapestry will emit similar markup as specified below − <!DOCTYPE html> <html> <head> <meta charset = “UTF-8” /> <title>Hello World Test Page</title> </head> <body> <div>Sample Web Application</div> <h1>Hello World Test Page</h1> <h1>Hello World!</h1> <div>(C) 2016 TutorialsPoint.</div> </body> </html> Layouts can be nested. For example, we may extend our custom layout by including administration functionality and use it for admin section as specified below. <html t:type = “MyCommonLayout” xmlns:t = “http://tapestry.apache.org/schema/tapestry_5_4.xsd”> <div><!– Admin related items –><div> <t:body/> </html> <t:container> The <t:container> is a top-level element and includes a tapestry namespace. This is used to specify the dynamic section of a component. For example, a grid component may need a template to identify how to render its rows – tr (and column td) within a HTML table. <t:container xmlns:t = “http://tapestry.apache.org/schema/tapestry_5_4.xsd”> <td>${name}</td> <td>${age}</td> </t:container> <t:block> The <t:block> is a placeholder for a dynamic section in the template. Generally, block element does not render. Only, components defined in the template uses block element. Components will inject data dynamically into the block element and render it. One of the popular use case is AJAX. The block element provides the exact position and markup for the dynamic data to be rendered. Every block element should have a corresponding Java Property. Only then it can be dynamically rendered. The id of the block element should follow Java variable identifier rules. The partial sample is provided below. @Inject private Block block; <html t:type = “mycustomlayout” title = “block example” xmlns:t = “https://tapestry.apache.org/schema/tapestry_5_4.xsd” xmlns:p = “tapestry:parameter”> <h1>${title}</h1> <!– … … –> <t:block t:id = “block”> <h2>Highly dynamic section</h2> I”v been updated through AJAX call The current time is: <strong>${currentTime}</strong> </t:block> <!–

Apache Tapestry – Quick Guide

Apache Tapestry – Quick Guide ”; Previous Next Apache Tapestry – Overview Apache Tapestry is an open source web framework written in Java. It is a component based web framework. Tapestry components are Java Classes. They are neither inherited from a framework specific base class nor implementation of an interface and they are just plain POJOs (Plain old Java Objects). The important feature of the Java used by tapestry is Annotation. Tapestry web pages are constructed by using one or more components, each having a XML based template and component class decorated with a lot of Tapestry”s Annotations. Tapestry can create anything ranging from a tiny, single-page web application to a massive one consisting of hundreds of pages. Benefits of Tapestry Some of the benefits provided by tapestry are − Highly scalable web applications. Adaptive API. Fast and mature framework. Persistent state storage management. Build-in Inversion of Control. Features of Tapestry Tapestry has the following features − Live class reloading Clear and detailed exception reporting Static structure, dynamic behaviors. Extensive use of Plain Old Java Objects (POJOs) Code less, deliver more. Why Tapestry? Already Java has a lot of web frameworks like JSP, Struts, etc., Then, why do we need another framework? Most of the today”s Java Web Frameworks are complex and have a steep learning curve. They are old fashioned and requires compile, test and deploy cycle for every update. On the other hand, Tapestry provides a modern approach to web application programming by providing live class reloading. While other frameworks are introducing lots of interfaces, abstract & base classes, Tapestry just introduces a small set of annotations and still provides the ability to write large application with rich AJAX support. Apache Tapestry – Architecture Tapestry tries to use the available features of Java as much as possible. For example, all Tapestry pages are simply POJOs. It does not enforce any custom interfaces or base class to write the application. Instead, it uses Annotation (a light weight option to extend the functionality of a Java class) to provide features. It is based on battle-tested Java Servlet API and is implemented as a Servlet Filter. It provides a new dimension to the web application and the programming is quite Simple, Flexible, Understandable and Robust. Workflow Let us discuss the sequence of action taking place when a tapestry page is requested. Step 1 − The Java Servlet receives the page request. This Java Servlet is the configured in such a way that the incoming request will be forwarded to tapestry. The configuration is done in the web.xml as specified in the following program. Filter and Filter Mapping tag redirects all the request to Tapestry Filter. <!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” “http://java.sun.com/dtd/web-app_2_3.dtd”> <web-app> <display-name>My Tapestry Application</display-name> <context-param> <param-name>tapestry.app-package</param-name> <param-value>org.example.myapp</param-value> </context-param> <filter> <filter-name>app</filter-name> <filter-class>org.apache.tapestry5.TapestryFilter</filter-class> </filter> <filter-mapping> <filter-name>app</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> Step 2 − The Tapestry Filter calls the HttpServletRequestHandler Service by its Service() method. Step 3 − HttpServletRequestHandler stores the request and response in RequestGlobals. It also wraps the request and response as a Request and Response object and sends it to the RequestHandler. Step 4 − The RequestHandler is an abstraction on top of HttpServletRequest of Servlet API. Some of the salient feature of the tapestry is done in RequestHandler section. The feature of tapestry can be extended by writing a filter in RequestHandler. RequestHandler provides several build-in filters, which include − CheckForUpdates Filter − Responsible for live class reloading. This filter checks the java classes for changes and update the application as necessary. Localization Filter − Identify the location of the user and provide localization support for the application. StaticFiles Filter − Identify the static request and aborts the process. Once the process is aborted, Java Servlet takes control and process the request. Error Filter − Catches the uncaught exception and presents the exception report page. The RequestHandler also modifies and stores the request and response in the RequestQlobalsand invokes the MasterDispatcher service. Step 5 − The MasterDispatcher is responsible for rendering the page by calling several dispatchers is a specific order. The four-main dispatchers called by MasterDispatcher is as follows − RootPath Dispatcher − It recognizes the root path “/” of the request and render the same as Start page. Asset Dispatcher − It recognized the asset (Java assets) request by checking the url pattern /assets/ and sends the requested assets as byte streams. PageRender Dispatcher − Bulk of the tapestry operations are done in PageRender Dispatcher and the next dispatcher Component Dispatcher. This dispatcher recognizes the particular page of that request and its activation context (extra information). It then renders that particular page and sends it to the client. For example, if the request url is /product/12123434, the dispatcher will check if any class with name product/12123434 is available. If found, it calls product/12123434 class, generate the response and send it to the client. If not, it checks for product class. If found, it calls product class with extra information 121234434, generates the response and sends it to the client. This extra information is called Activation Context. If no class is found, it simply forwards the request to Component Dispatcher. Component Dispatcher − Component Dispatcher matches the URL of the page with the pattern – /<class_name>/<component_id>:<event_type>/<activation_context>. For example, /product/grid:sort/asc represents the product class, grid component, sortevent type and asc activation context. Here, event_type is optional and if none is provided, the default event type action will be triggered. Usually, the response of the component dispatcher is to send a redirect to the client. Mostly, the redirect will match PageRender Dispatcher in the next request and proper response will be send to the client. Apache Tapestry – Installation In this chapter, we will discuss how to install Tapestry on our machine. Prerequisite Tapestry”s only dependency is Core Java. Tapestry is developed independently without using any third party library / framework. Even the IoC library used by tapestry is developed from the scratch. Web application written in tapestry can be built and deployed

Apache Tapestry – Project Layout

Apache Tapestry – Project Layout ”; Previous Next Here is the layout of the source code created by Maven Quickstart CLI. Also, this is the suggested layout of a standard Tapestry Application. ├── build.gradle ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ └── example │ │ │ └── MyFirstApplication │ │ │ ├── components │ │ │ ├── data │ │ │ ├── entities │ │ │ ├── pages │ │ │ └── services │ │ ├── resources │ │ │ ├── com │ │ │ │ └── example │ │ │ │ └── MyFirstApplication │ │ │ │ ├── components │ │ │ │ ├── logback.xml │ │ │ │ └── pages │ │ │ │ └── Index.properties │ │ │ ├── hibernate.cfg.xml │ │ │ └── log4j.properties │ │ └── webapp │ │ ├── favicon.ico │ │ ├── images │ │ │ └── tapestry.png │ │ ├── mybootstrap │ │ │ ├── css │ │ │ │ ├── bootstrap.css │ │ │ │ └── bootstrap-theme.css │ │ │ ├── fonts │ ├── glyphicons-halflings-regular.eot │ │ │ │ ├── glyphicons-halflings-regular.svg │ │ │ │ ├── glyphicons-halflings-regular.ttf │ │ │ │ ├── glyphicons-halflings-regular.woff │ │ │ │ └── glyphicons-halflings-regular.woff2 │ │ │ └── js │ │ └── WEB-INF │ │ ├── app.properties │ │ └── web.xml │ ├── site │ │ ├── apt │ │ │ └── index.apt │ │ └── site.xml │ └── test │ ├── conf │ │ ├── testng.xml │ │ └── webdefault.xml │ ├── java │ │ └── PLACEHOLDER │ └── resources │ └── PLACEHOLDER └── target ├── classes │ ├── com │ │ └── example │ │ └── MyFirstApplication │ │ ├── components │ │ ├── data │ │ ├── entities │ │ ├── logback.xml │ │ ├── pages │ │ │ └── Index.properties │ │ └── services │ ├── hibernate.cfg.xml │ └── log4j.properties ├── m2e-wtp │ └── web-resources │ └── META-INF │ ├── MANIFEST.MF │ └── maven │ └── com.example │ └──MyFirstApplication │ ├── pom.properties │ └── pom.xml ├── test-classes │ └── PLACEHOLDER └── work ├── jsp ├── sampleapp.properties └── sampleapp.script The default layout is arranged like the WAR Internal File Format. Using WAR format helps to run the application without packaging and deploying. This layout is just a suggestion, but the application can be arranged in any format, if it is packaged into a proper WAR format while deploying. The source code can be divided into the following four main sections. Java Code − All java source codes are placed under /src/main/java folder. Tapestry page classes are placed under the “Pages” folder and Tapestry component classes are placed under components folder. Tapestry service classes are placed under services folder. ClassPath Resources − In Tapestry, most of the classes have associated resources (XML Template, JavaScript files, etc.). These resources are placed under the /src/main/resources folder. Tapestry Page Classes have its associated resources under the “Pages” folder and Tapestry components classes have its associated resources under the Components folder. These resources are packaged into the WEB-INF/classes folder of the WAR. Context Resources − They are static resources of a web application like Images, Style Sheet and JavaScript Library / Modules. They are usually placed under the /src/main/webapp folder and they are called Context Resources. Also, the web application description file (of Java Servlet), web.xml is placed under the WEB-INF folder of context resources. Testing Code − These are optional files used to test the application and placed under the src/test/java and src/test/Resources Folders. They are not packaged into WAR. Print Page Previous Next Advertisements ”;

Apache Tapestry – Storage

Apache Tapestry – Storage ”; Previous Next Every web application should have some way to store certain user data like user object, user preferences, etc. For example, in a shopping cart application, the user”s selected items / products should be saved in a temporary bucket (cart) until the user prefers to buy the products. We can save the items in a database, but it will be too expensive since all users are not going to buy the selected items. So, we need a temporary arrangement to store / persist the items. Apache Tapestry Provides two ways to persist the data and they are − Persistence page data Session Storage Both has its own advantages and limitations. We will check it in the following sections. Persistence Page Data The Persistence Page Data is a simple concept to persist data in a single page between requests and it is also called as Page Level Persistence. It can be done using the @Persist annotation. @Persist public int age; Once a field is annotated with @Persist, the field”s value will be persisted across request and if the value is changed during request, it will be reflected when it is accessed next time. Apache Tapestry provides five types of strategy to implement the @Persist concept. They are as follows − Session Strategy − The data is persisted using the Session and it is a default strategy. Flash Strategy − The data is persisted using Session as well, but it is a very short lived one. The data will be available in only one subsequent request. @Persist(PersistenceConstants.FLASH) private int age; Client Strategy − The data is persisted in the client side such as URL query string, hidden field in the form, etc. @Persist(PersistenceConstants.FLASH) private int age; Hibernate Entity Strategy − The data is persisted using the Hibernate module as Entity. The entity will be stored in Hibernate and its reference (Java class name and its primary key) will be saved as token in HttpSession. The entity will be restored by using the token available in HttpSession. @Persist(HibernatePersistenceConstants.ENTITY) private Category category; JPA Entity Strategy − The data is persisted using a JPA module. It will only able to store Entity. @Persist(JpaPersistenceConstants.ENTITY) private User user; Session Storage Session storage is an advanced concept used to store data which needs to be available across pages like data in multiple page wizard, logged in user details, etc. The Session Storage provides two options, one to store complex object and another to store simple values Session Store Object − Used to store complex object. Session Attributes − Used to store simple values. Session Store Object (SSO) An SSO can be created using @SessionStore annotation. The SSO will store the object using type of the object. For example, the Cart Object will be stored using a Cart class name as token. So, any complex object can be stored once in an application (one per user). public class MySSOPage { @SessionState private ShoppingCart cart; } An SSO is a specialized store and should be used to store only complex / special object. Simple data types can also be stored using an SSO, but storing simple data types like String makes it only store one “String” value in the application. Using a single “String” value in the application is simply not possible. You can use simple data types as Apache Tapestry provides Session Attributes. Session Attributes Session Attributes enable the data to be stored by name instead of its type. public class MyPage { @SessionAttribute private String loggedInUsername; } By default, Session Attributes uses the field name to refer the data in session. We can change the reference name by annotation parameter as shown below − public class MyPage { @SessionAttribute(“loggedInUserName”) private String userName; } One of the main issues in using name as session reference is that we may accidentally use the same name in more than one class / page. In this case, the data stored maybe changed unexpectedly. To fix this issue, it will be better to use the name along with class / page name and package name like com.myapp.pages.register.email, where com.myapp.pages is the package name, register is the page / class name and finally email is variable (to be stored) name. Print Page Previous Next Advertisements ”;