Java Tapestry4.1.2 Introductory Instructions tutorial 1th/2 page _java

Source: Internet
Author: User
Tags abstract java web
Simple Introduction
Don't care about links! Don't care where the request is (HTTP request)! Don't care about the response (HTTP response) where to turn! Tapestry is built on the underlying request-resonse model, based on the servlet technology, to abstract the component-oriented development model. Tapestry are concerned with: pages, components, events, objects, methods, properties!
Installing JAR packs
1,
Copy the jar packages from the Lib directory in the Tapestry decompression directory to the Web-inf/lib directory.
and delete the duplicate package (Commons-logging.jar/javassist.jar/ognl-2.6.11.jar)
2,
Add in Web.xml:
Copy Code code as follows:

<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app</url-pattern>
</servlet-mapping>

This is the central controller of the tapestry. Tapestry the display of the page, all requests will be sent to this servlet.
Example 1: The simplest tapestry program
Add home.html under Webroot
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>
First Tapestry Program
</body>

and visit the Web site:
Http://localhost:8088/[context Path]/app
The results page displays the contents of the home.html.
This is because tapestry always contains a page named "Home", which by default is a home.html under the root path.
Example 2: Add simple dynamic content
Home.html to read:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>
First Tapestry Program
Now time is: <span jwcid= "@Insert" value= "Ognl:new java.util.Date ()" > </span>
</body>

Re-access URL: Http://localhost:8088/[context path]/app
On this page, the Tapestry Insert Component (Component) is used. It takes a parameter and is passed through a OGNL expression.
Add in the startup parameters of the application server:-dorg.apache.tapestry.disable-caching=true to avoid restarting the server every time the page template is modified.
JWC = Java Web Component
Example 3: Create a link, point to a page
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>
First Tapestry Program
Now time is: <span jwcid= "@Insert" value= "Ognl:new java.util.Date ()" ></span>
<p>
<a href= "#" jwcid= "@PageLink" page= "Home" > Refresh </a>
</body>

This time, with a pagelink component that points to the home page, the Pagelink component automatically generates a link to the home page (we don't have to care about the link!). )。
Example 4: Simple counters
Until now, we have not touched Java class, but has let Tapestry successfully run! It's time to write some Java code. We want to create a counter, and whenever the user clicks on "Counter 1", we add this counter 1 and then display the result on the page.
In the traditional request-response model, we need to think about this problem by submitting a request, creating a corresponding action to receive the request, maintaining the value of the counter, and then deciding which page to turn to successfully, displaying the results on the page.
In tapestry, what we need to consider is: which page handles the event, and how does the result appear on the page?
Here's home.html.
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>
First Tapestry Program
Now time is: <span jwcid= "@Insert" value= "Ognl:new java.util.Date ()" ></span>
<p>
<a href= "#" jwcid= "@PageLink" page= "Home" > Refresh </a>
<p>
The current value of the counter is: <span jwcid= "@Insert" value= "Ognl:counter" ></span>
<a href= "#" jwcid= "@DirectLink" listener= "Listener:doclick" > Counter increased 1</a>
</body>

The listener specified by DirectLink is: DoClick, which triggers an event that is actually going to invoke the DoClick () method.
Where will the DoClick () method be written? Because the current page, its dynamic data can not be easily obtained, so, you must create a corresponding class for the present page, our method will be created in this class.
Copy Code code as follows:

Package com.bjsxt.crm.web.tapestry.test;
Import org.apache.tapestry.annotations.Persist;
Import Org.apache.tapestry.html.BasePage;
Public abstract class Home extends BasePage {
@Persist
public abstract int Getcounter ();
public abstract void Setcounter (int count);
public void DoClick () {
int counter = Getcounter ();
Counter = counter + 1;
Setcounter (counter);
}
}

Note: In this example, the class name needs to be defined as Home;listener's name is DoClick, not DoClick ()
Additionally, you need to add a profile under the Web-inf directory: app.application
Copy Code code as follows:

<?xml version= "1.0"?>
<! DOCTYPE Application Public
"-//apache Software foundation//tapestry specification 4.0//en"
"Http://tapestry.apache.org/dtd/Tapestry_4_0.dtd" >
<application>
<meta key= "Org.apache.tapestry.page-class-packages" value= "Com.bjsxt.crm.web.tapestry.test"/>
</application>

The primary purpose of the configuration is to let tapestry understand how to associate pages and page classes.
Example 5: A counter with one parameter
Home.html to read:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>
First Tapestry Program
Now time is: <span jwcid= "@Insert" value= "Ognl:new java.util.Date ()" ></span>
<p>
<a href= "#" jwcid= "@PageLink" page= "Home" > Refresh </a>
<p>
The current value of the counter is: <span jwcid= "@Insert" value= "Ognl:counter" ></span>
<a href= "#" jwcid= "@DirectLink" listener= "Listener:doclick" parameters= "ognl:1" > Counter increased 1</a> <br>
<a href= "#" jwcid= "@DirectLink" listener= "Listener:doclick" parameters= "Ognl:5" > Counter increased 5</a> <br>
<a href= "#" jwcid= "@DirectLink" listener= "Listener:doclick" parameters= "ognl:10" > Counter increased 10</a> <br>
<a href= "#" jwcid= "@DirectLink" listener= "Listener:clearcounter" > Empty counters </a>
</body>

Current 1/2 page 12 Next read the full text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.