Java Tapestry4.1.2 getting started page 1/2, javatapestry4.1.2

Source: Internet
Author: User

Java Tapestry4.1.2 getting started page 1/2, javatapestry4.1.2
Brief Introduction
Don't worry about links! You don't have to worry about the http request! You don't have to worry about where the response (http response) is going! Tapestry is built on the underlying request-resonse mode. Based on Servlet technology, it abstracts the model developed to the component. Tapestry cares about pages, components, events, objects, methods, and attributes!
Install the jar package
1,
Copy the jar package in the lib directory under the Tapestry extract directory to the WEB-INF/lib directory.
And remove the duplicate package (commons-logging.jar/javassist. jar/ognl-2.6.11.jar)
2,
Add the following in web. xml:
Copy codeThe Code is 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 Tapestry. All requests are sent to the Servlet.
Example 1: The simplest Tapestry Program
Upload home.html under webroot
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GB18030">
<Title> Insert title here </title>
</Head>
<Body>
The first Tapestry Program
</Body>
</Html>

Visit the website:
Http: // localhost: 8088/[context path]/app
The page displays contents in home.html.
This is because tapestrywill always include a page named "home‑based", and the home.html
Instance 2: Add Simple Dynamic Content
Changed Home.html:
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GB18030">
<Title> Insert title here </title>
</Head>
<Body>
The first Tapestry Program
The current time is: <span cid = "@ Insert" value = "ognl: new java. util. Date ()"> </span>
</Body>
</Html>

Access the URL again: http: // localhost: 8088/[context path]/app
On this page, the Insert Component (Component) of the Tapestry is used ). It carries a parameter and is passed through an ognl expression.
Add-Dorg. apache. tapestry. disable-caching = true to the startup parameters of the application server to avoid restarting the server every time you modify the page template.
Jwc = Java Web Component
Instance 3: creates a link pointing to a page
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GB18030">
<Title> Insert title here </title>
</Head>
<Body>
The first Tapestry Program
The current time is: <span cid = "@ Insert" value = "ognl: new java. util. Date ()"> </span>
<P>
<A href = "#" cid = "@ PageLink" page = "Home"> refresh </a>
</Body>
</Html>

This time, through a PageLink component pointing to the Home page, the PageLink component will automatically generate a link pointing to the Home page (we don't need to care about this link !).
Example 4: simple counters
Until now, we have not involved java classes, but we have made Tapestry run successfully! It's time to write some java code. We want to create a counter. Every time you click "add COUNTER 1", we add this counter to 1 and then display this result on the page.
In the traditional request-response mode, we need to consider the following: submit a request, create an Action to receive the request, and maintain the counter value, then, the page is determined to be switched and the result is displayed on the page.
In Tapestry, we need to consider the page on which the event is processed and how the result is displayed on the page?
The following is home.html.
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GB18030">
<Title> Insert title here </title>
</Head>
<Body>
The first Tapestry Program
The current time is: <span cid = "@ Insert" value = "ognl: new java. util. Date ()"> </span>
<P>
<A href = "#" cid = "@ PageLink" page = "Home"> refresh </a>
<P>
The current counter value is: <span cid = "@ Insert" value = "ognl: counter"> </span>
<A href = "#" cid = "@ DirectLink" listener = "listener: doClick"> increase the counter by 1 </a>
</Body>
</Html>

The listener specified by DirectLink is doClick, which triggers an event. In fact, the doClick () method will be called.
Where will the doClick () method be written? Because the dynamic data of the current page cannot be obtained simply, you must create a corresponding class for the current page. Our method will be created in this class.
Copy codeThe Code is 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 must be defined as Home; the listener name is doClick instead of doClick ()
In addition, you must add the configuration file app. application in the web-inf directory.
Copy codeThe Code is 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 main purpose of configuration is to let Tapestry know how to associate pages and page classes.
Instance 5: A counter with a parameter
Changed Home.html:
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GB18030">
<Title> Insert title here </title>
</Head>
<Body>
The first Tapestry Program
The current time is: <span cid = "@ Insert" value = "ognl: new java. util. Date ()"> </span>
<P>
<A href = "#" cid = "@ PageLink" page = "Home"> refresh </a>
<P>
The current counter value is: <span cid = "@ Insert" value = "ognl: counter"> </span>
<A href = "#" cid = "@ DirectLink" listener = "listener: doClick" parameters = "ognl: 1"> increase the counter by 1 </a> <br>
<A href = "#" cid = "@ DirectLink" listener = "listener: doClick" parameters = "ognl: 5"> increase the counter by 5 </a> <br>
<A href = "#" cid = "@ DirectLink" listener = "listener: doClick" parameters = "ognl: 10"> 10 more counters </a> <br>
<A href = "#" cid = "@ DirectLink" listener = "listener: clearCounter"> clear the counter </a>
</Body>
</Html>

Current 1/2 page12. Next page

Related Article

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.