Java tapestry4.1.2 getting started tutorial page 1/2

Source: Internet
Author: User

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: CopyCode The 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.
Instance 1: The simplest tapestryProgram
Upload home.html under webrootCopy 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
http: // localhost: 8088/[context path]/APP
.
example
instance 2: Add simple dynamic content
change home.html to copy Code the code is as follows:



insert title here


the first tapestry Program
The current time is:

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 pageCopy 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 code The 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 Code the 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 );
}< BR >}

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 code The 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>

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.