Monday, 29.04.2024, 11:22
Welcome Guest | RSS

Tapestry learning

Site menu
Statistics

First project

Select File → New → Project and the list of available templates - Maven 2 Project → Maven 2 Project Creation Wizard.  
In the resulting window Maven2 Project Location, type the name of the project TapestryForum. 
Click Next. In the Maven Archetypes, select Use a custom archetype. Enter in the form fields values shown in the figure. Click Finish. 
In the Package Explorer tree on the left will be a new site - TapestryForum. This presentation of our project with a standard directory structure, as well as classes and configuration files that make up the simplest Tapestry-application. 
Open the form Servers, right-click on the server and select Add and remove projects ... In the left list vyberiteTapestryForum, click Add and Finish. 
Right-click on the server and select Start. Now open your browser's address http://localhost:8080/TapestryForum/. You should see front page template Tapestry quickstart, showing the current time, which can be updated by clicking on refresh. 
Thus, the development environment is ready, the template for our application is created. In the next article we will consider the application directory structure, purpose and content of the auto-generated file and make the first changes in the application code to make him do something more interesting.

1 Structure of the project 
Let see the Package Explorer. The first two node, src/test/java and src/main/java - a directory of sources, containing, respectively, the source code and test the application itself. PLACEHOLDER file in the directory src/test/java will be removed as soon as they are created the first test. 
In the directory src/main/java Maven puts packages. Tapestry introduces an additional concept - app package, or root application package. In our case it is called TapestryForum (I called it so only for brevity and simplicity - in real projects still worth pursuing agreements). All are in it subpackage must follow a strict naming system, which allows Tapestry independently find and configure application components, saving you from having to write a kilometer XML-files.Agreement is more important than the configuration (convention over configuration) - one of the fundamental principles of Tapestry, and we have not time to remember. 
Incidentally, if the "one-level" representation of the structure of packages you're not satisfied, you can change it to "hierarchical" - click on the triangle in the upper right corner of the Package Explorer and select Package Presentation → Hierarchical. 
So far we see only two subpackage inside the package TapestryForum. Package pages contains the page class - be it a simple Java-objects (POJO, Plain Old Java Objects), not by the need to inherit or implement any interface. Class Index - a class behind the page, which we saw last time, when doing a test run the application. 
Package services includes services - classes that perform support functions. Class AppModule in it - so-called module, the main configuration class of applications, but to Him we shall return. 
The next node in the tree Package Explorer - Maven is a library for our project. Library (library) - the concept of environment Eclipse, which means a set of jar-files, which can be connected to the project. Plugin Q4E, we set the last time, automatically builds a library from the Maven repository in accordance with the dependencies specified in the pom.xml - the configuration file Maven. 
In addition to the jar-files Tapestry, this library includes means journaling (SLF4J, Log4J) and testing (jUnit, TestNG), as well as internal depending Tapestry. 
Followed by several more folders (normal, not source folders), containing the application resources. The folder src/main/resources designed to accommodate the configuration files that are in the assembly must be copied into the folder WEB-INF. Now in this folder, single file - log4j.properties, the configuration file system journaling is configured for registration messages Tapestry in the console. It also contains instructions for configuring log4j for more information about the internal processes Tapestry. 
The folder src / main / webapp corresponds to the root folder of the expanded or packed in a war-file Web applications. 
Files tml - page templates Web application, so far there is only Index.tml - template main page of the application associated with a class from the package Index pages. 
META-INF - folder with the manifesto (manifest.mf) Web applications; 
WEB-INF - configuration of web applications. It contains the deployment descriptor web.xml file and app.properties - a global message catalog. The mechanism of communications and related internationalization, we must consider in future articles. 
The folder src / test / resources intended for the resources used in testing the application classes from src / test / java.
 
2 Elements of the application 
The deployment descriptor web.xml 
This is a standard element of any Web application, its main configuration file. First look at setting the context tapestry.app-package, which defines the root application package. In fact, it is - a single configuration parameter for Tapestry, which is set in the xml-configuration. The rest, as I said, based on naming conventions packages and classes. 
<context-param> 
    
<param-name> tapestry.app-package </ param-name> 
    
<param-value> TapestryForum </ param-value> 
</ Context-param> 
This is followed by the filter configuration Tapestry. In earlier versions of the framework used by a special servlet, but the filter - a more flexible solution. As can be seen from the configuration, the filter Tapestry intercepts requests to all URL, beginning with the root URL of our application, ie in our case - with addresses http://localhost:8080/TapestryForum/. 
<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> 
Filter name app specifies the name of the configuration module applications t.e.klassa AppModule, which I have already mentioned, and which is placed in the package <tapestry.app-package>. Services. For example, if we replace the root package of applications com.example.forum, and the name of the filter on the main, then when you start Tapestry will search for class com.example.forum.services.MainModule. 
In addition, the name of the filter determines the file name of the global catalog. In the above example, the file should be called not app.properties, as in our case, as main.properties. 
Class AppModule 
This class is designed for configuration IoC-container and to expand Tapestry. It does not implement any special interface or inherit from the abstract class. His methods also are based on naming conventions. This is a static public methods that do not return values (public static void). 
For example, the method is designed to bind configuration IoC-container, you can specify the binding interfaces of beans to a specific implementation. Methods whose names begin with contribute, are used as extension points Tapestry. 
Method contributeApplicationDefaults sets application settings by default. 
  
public static void contributeApplicationDefaults (MappedConfiguration <String, String> configuration) {
    
configuration.add (SymbolConstants.SUPPORTED_LOCALES, "en"); 
    
configuration.add (SymbolConstants.PRODUCTION_MODE, "false"); 
}

Originally it defined a list of supported locales SUPPORTED_LOCALES (he is, we need to internationalize). There is also activated application development (PRODUCTION_MODE = "false"), in which disabled some means of optimization, in particular, the caching page templates, so for each request to the server templates are read again. This mode is very useful in the design, as it allows you to change templates without restarting the application server and see the results of changes after the upgrade page in the browser. When you deploy applications in a production environment this line, you can simply delete as default Tapestry adjusted precisely to operating conditions (production mode). 
The method contributeRequestHandler intended for registration request processing filters. This kind of alternative filter chain of Servlet API, declared in the deployment descriptor, only integrated with Tapestry. As an example, using the method of registration is given a simple filter-profiler logs the request processing time. 
Actually, a lot of information about the possibilities of class AppModule can be obtained by examining the methods of the class org.apache.tapestry5.services.TapestryModule - Internal configuration module Tapestry, located in the archives of tapestry-core-5.0.18.jar. It is built on similar agreements, and all the methods defined in it can be set and in our module AppModule. 

3 creates the first page 
Before proceeding to examine and edit the file page Index.tml, let's setup Eclipse to edit the file with that extension by editing the XML. 
Open the Preferences window environment (Window → Pereferences). Select a tree on the left General → Content Types. In the resulting tree content types, expand Text and highlight the node XML. 
Click Add ... next to the list of associations and enter *.tml. 
Now the files tml will default XML editor opens with backlight and automatic closing tags. 
Open the file Index.tml. We will not parse the code by default, but better to create your own.Remove all content between the tags <body> ...</ body>. Enter the following text: 
<h1> Welcome to the forum! </ h1> 
<h2> Enter your username and password </ h2> 
<t:form t:id="loginForm"> 
    
<table> 
        
<tr> 
            
<td> <t: label 
                
t: for = "loginField "/>:</ td> 
            
<td> <t: textfield 
                
t: id = "loginField" 
                
t: value = "login" 
                
t: label = "Login" /> 
            
</ Td> 
        
</ Tr> 
        
<tr> 
            
<td> <t: label 
                
t: for = "passwordField "/>:</ td> 
            
<td> <t: passwordfield 
                
t: id = "passwordField" 
                
t: label = "Password" 
                
t: value = "password" /> 
            
</ Td> 
        
</ Tr> 
        
<tr> 
            
<td> <t: submit 
                
t: value = "Login" /> </ td> 
        
</ Tr> 
    
</ Table> 
</ T: form> 
Tags starting with t: - a component of Tapestry. Their names in the template pages are not case sensitive. Here we see four components: TextField, PasswordField, Label, Submit, and Form. 
For those who understand JSP and JSF, it would not be anything complicated. Form - a component, framing the rest in html-form. His ID t: id = "loginForm" we need it too. 
TextField - this is a text entry field. Its attributes also include the identifier t: id, text label t: label and field grade Index, with whom he associated, in our case, this field login. Attribute t: value is not case sensitive, you can specify and LOGIN, and Login. 
Component Label indicates the place in which to insert the label associated components.Connected components is determined by the ID attribute in t: for. In our case, the components of t: label is determined that the labels of input fields will be inserted into a table cell to the left of input fields - that was still looking sharp and beautiful. 
Component PasswordField similar component TextField, but he hides the input characters for the dots or asterisks. 
Finally, the components Submit - this is a normal button to submit the form, its attribute t: value defines the text that appears on it. 
Save the file and run Index.tml server. Go to the address http://localhost:8080/TapestryForum/ 

The error message says that the class is no Index property of the login. This is a property referred to by our component loginField. Here is a list of available properties class. Please note that Tapestry has determined that there currentTime properties on a single method-getter getCurrentTime (). 
In addition, the page contains the exception stack trace and a very detailed list of all the conditions under which the exception occurred, including a fragment of a template, which caused the error. 
Open Class Index and remove from the method getCurrentTime. Add a private-field login and password, providing them with the annotation @ Property (org.apache.tapestry5.annotations.Property): 

@ Property 
private String login; 
@ Property 
private String password;
 
When the Java language will develop the characteristics (if any appear) - is still unknown.Meanwhile, the @ Property annotation frees you from having to write getters and setters for the fields of the class page. 
It is important that the page class contains a field with only access modifiers private. Otherwise, you risk getting an error message. 
Wait a few seconds until the reboot Tapestry context (this process can be seen in the console).Reload the page. That's another matter. 
However, when you click "Enter", nothing happens. Not surprisingly, we did not specify an event handler Submit. 
To begin to determine where the user will get when successful or unsuccessful authentication.Create a directory src / main / webapp two very simple pages Welcome.tml and Error.tml: 

Welcome.tml 

<h1> Welcome! </ h1> 
Error.tml 
<h1> Authorization Error! </ h1> 
Create two empty classes of the same name in the package pages, Welcome and Error. 
  
package TapestryForum.pages; 
public class Welcome {
}

package TapestryForum.pages; 
public class Error { 
}

Now add in class Index two fields such as Welcome and Error and supplied them with annotations @ InjectPage:
 
@ InjectPage 
private Error error; 
@ InjectPage 
private Welcome welcome; 

This abstract indicates IoC-container, the objects of the classes must be injected into the class Index. 
Now back to the handler, the button Submit. If you remember, a component of Form in the page Index.tml had ID loginForm. Method handler clicking on the button, quite logically, should be called onSubmitFromLoginForm: 

public Object onSubmitFromLoginForm () {
    
if ("foo". equals (login) & & "bar". equals (password)) {
        
return welcome; 
    }

    
return error; 
}
Reload the page. Try typing in the login field foo, but in the password field - bar. Go back to using the back button your browser to the login page. Enter the incorrect values, and again click "Login". 
Notice how the page name appears in the URL. Tapestry is built in accordance with the principles of REST.

Search
Entries archive

Copyright ZmeyStudio © 2024
Hosted by uCoz