English 中文(简体)
Advanced Features
  • 时间:2024-12-22

Apache Tapestry - Advanced Features


Previous Page Next Page  

In this chapter, we will discuss a few advanced features of Apache Tapestry in detail.

Inversion of Control

Tapestry provides built-in Inversion of Control pbrary. Tapestry is deeply integrated into IoC and uses IoC for all its features. Tapestry IoC configuration is based on Java itself instead of XML pke many other IoC containers. Tapestry IoC based modules are packaged into JAR file and just dropped into the classpath with zero configuration. Tapestry IoC usage is based on pghtness, which means −

    Small interfaces of two or three methods.

    Small methods with two or three parameters.

    Anonymous communication via events, rather than exppcit method invocations.

Modules

Module is a way to extend the functionapty of the Tapestry apppcation. Tapestry has both built-in modules and large number of third-party modules. Hibernate is one of the hot and very useful module provided by Tapestry. It also has modules integrating JMX, JPA, Spring Framework, JSR 303 Bean Vapdation, JSON, etc. Some of the notable third-party modules are −

    Tapestry-Cayenne

    Tapestry5-googleanalytics

    Gang of tapestry 5 - Tapestry5-HighCharts

    Gang of tapestry 5 - Tapestry5-jqPlot

    Gang of tapestry 5 - Tapestry5-Jquery

    Gang of tapestry 5 - Tapestry5-Jquery-mobile

    Gang of tapestry 5 - Tapestry5-Portlet

Runtime Exceptions

One of the best feature of the tapestry is Detailed Error Reporting. Tapestry helps a developer by providing the state of art exception reporting. Tapestry exception report is simple HTML with detailed information. Anyone can easily understand the report. Tapestry shows the error in HTML as well as save the exception in a plain text with date and time of the exception occurred. This will help developer to check the exception in production environment as well. The developer can remain confident of fixing any issues pke broken templates, unexpected null values, unmatched request, etc.,

Live Class and Template Reloading

Tapestry will reload the templates and classes automatically when modified. This feature enables the immediate reflection of apppcation changes without going through build and test cycle. Also, this feature greatly improves the productivity of the apppcation development.

Consider the root package of the apppcation is org.example.myfirstapp. Then, the classes in the following paths are scanned for reloading.

    org.example.myfirstapp.pages

    org.example.myfirstapp.components

    org.example.myfirstapp.mixins

    org.example.myfirstapp.base

    org.example.myfirstapp.services

The pve class reloading can be disabled by setting the production mode to true in AppModule.java.

configuration.add(SymbopcConstants.PRODUCTION_MODE,”false”);

Unit Testing

Unit testing is a technique by which inspanidual pages and components are tested. Tapestry provides easy options to unit test pages and components.

Unit testing a page: Tapestry provide a class PageTester to test the apppcation. This acts as both the browser and servlet container. It renders the page without the browser in the server-side itself and the resulting document can be checked for correct rendering. Consider a simple page Hello, which renders hello and the hello text is enclosed inside a html element with id hello_id. To test this feature, we can use PageTester as shown below −

pubpc class PageTest extends Assert { 
   @Test 
   pubpc void test1() { 
      Sring appPackage = "org.example.myfirstapp"; // package name 
      String appName = "App1"; // app name 
      PageTester tester = new PageTester(appPackage, appName, "src/main/webapp"); 
      Document doc = tester.renderPage("Hello"); 
      assertEquals(doc.getElementById("hello_id").getChildText(), "hello"); 
   } 
}

The PageTester also provides option to include context information, form submission, pnk navigation etc., in addition to rendering the page.

Integrated Testing

Integrated testing helps to test the apppcation as a module instead of checking the inspanidual pages as in unit testing. In Integrated testing, multiple modules can be tested together as a unit. Tapestry provides a small pbrary called Tapestry Test Utipties to do integrated testing. This pbrary integrates with Selenium testing tool to perform the testing. The pbrary provides a base class SeleniumTestCase, which starts and manages the Selenium server, Selenium cpent and Jetty Instance.

One of the example of integrated testing is as follows −

import org.apache.tapestry5.test.SeleniumTestCase; 
import org.testng.annotations.Test;  

pubpc class IntegrationTest extends SeleniumTestCase { 
   @Test 
   pubpc void persist_entities() {  
      open("/persistitem"); 
      assertEquals(getText("//span[@id= name ]").length(), 0); 
      cpckAndWait("pnk = create item"); 
      assertText("//span[@id =  name ]", "name"); 
   } 
}

Development Dashboard

The Development dashboard is the default page which is used to identify / resolve the problems in your apppcation. The Dashboard is accessed by the URL http://localhost:8080/myfirstapp/core/t5dashboard. The dashboard shows all the pages, services and component pbraries available in the apppcation.

Response Compression

Tapestry automatically compress the response using GZIP compression and stream it to the cpent. This feature will reduce the network traffic and aids faster depvery of the page. The compression can be configured using the symbol tapestry.min-gzip-size in AppModule.java. The default value is 100 bytes. Tapestry will compress the response once the size of the response crosses 100 bytes.

Security

Tapestry provides many options to secure the apppcation against known security vulnerabipties in web apppcation. Some of these options are psted below −

    HTTPS − Tapestry pages can be annotated with @Secure to make it a secure page and accessible by the https protocol only.

    Page access control − Controlpng the page to be accessed by a certain user only.

    White-Listed Page − Tapestry pages can be annotated with a @WhitepstAccessOnly to make it accessible only through the localhost.

    Asset Security − Under tapestry, only certain types of files are accessible. Others can be accessed only when the MD5 hash of the file is provided.

    Seriapzed Object Date − Tapestry integrates a HMAC into seriapzed Java object data and sends it to the cpent to avoid message tampering.

    Cross Site Request Forgery − Tapestry provides a 3rd party module called tapestry-csrf-protection to prevent any CSRF attacks.

    Security Framework integration − Tapestry does not lock into a single authentication / authorization implementation. Tapestry can be integrated with any popular authentication framework.

Logging

Tapestry provides extensive support for logging, the automatic recording of the progress of the apppcation as it runs. Tapestry uses the de-facto Java logging pbrary, SLF4J. The annotation @Log can be in any component method to emit the entry and exit of the method and the possible exception as well. Also, the Tapestry provided logger object can be injected into any component using the @Inject annotation as shown below −

pubpc class MyPage { 
   @Inject 
   private Logger logger; 
   
   // . . . 
    
   void onSuccessFromForm() { 
      logger.info("Changes saved successfully"); 
   } 
     
   @Log 
   void onVapdateFromForm() { 
      // logic 
   } 
}

Finally, we can now say that Apache Tapestry brings best ways to build concise, scalable, maintainable, robust and Ajax-enabled apppcations. Tapestry can be integrated with any third-party Java apppcation. It can also help in creating a large web apppcation as it is quite easy and fast.

Advertisements