SoFunction
Updated on 2025-04-05

Java Tapestry4.1.2 Getting Started Tutorial Page 2/2


Corresponding Home Class:
Copy the codeThe code is as follows:

package ;
import ;
import ;
public abstract class Home extends BasePage {
@Persist
public abstract int getCounter();
public abstract void setCounter(int count);
public void doClick(int increment){
int counter = getCounter();
counter = counter + increment;
setCounter(counter);
}
public void clearCounter(){
setCounter(0);
}
}

Example 6: Calculator with two parameters
 
Copy the 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 jwc value="ognl:new ()"></span>
<p>
<a href="#" jwc page="Home">Refresh</a>
<p>
The current value of the counter is: <span jwc value="ognl:counter"></span>
<a href="#" jwc listener="listener:doClick" parameters="ognl:1">Increase the counter by 1</a> <br>
<a href="#" jwc listener="listener:doClick" parameters="ognl:5">Increase the counter by 5</a> <br>
<a href="#" jwc listener="listener:doClick" parameters="ognl:10">Increase the counter by 10</a> <br>
<a href="#" jwc listener="listener:clearCounter">Clear the counter</a> <br>
<a href="#" jwc listener="listener:plus" parameters="ognl:{3,5}">calculate 3+5,</a>
The calculation result is: <span jwc value="ognl:result"></span>
</body>
</html>

Use ognl:{param1,param2} to pass multiple parameters.
Corresponding Home Class:
Copy the codeThe code is as follows:

package ;
import ;
import ;
public abstract class Home extends BasePage {
@Persist
public abstract int getCounter();
public abstract void setCounter(int count);
public abstract int getResult();
public abstract void setResult(int result);
public void doClick(int increment){
int counter = getCounter();
counter = counter + increment;
setCounter(counter);
}
public void clearCounter(){
setCounter(0);
}
public void plus(int a,int b){
setResult(a + b);
}
}

Example 7: Tapestry form and submission
Suppose you want to submit a Person object now:
Copy the codeThe code is as follows:

package ;
import ;
public class Person {
//Name
private String name;
//age
private int age;
//Date of Birth
private Date birthday;
public int getAge() {
return age;
}
public void setAge(int age) {
= age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
= birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
}

We add a link to the Home page to point to the AddPerson

Copy the 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 jwc value="ognl:new ()"></span>
<p>
<a href="#" jwc page="Home">Refresh</a>
<p>
The current value of the counter is: <span jwc value="ognl:counter"></span>
<a href="#" jwc listener="listener:doClick" parameters="ognl:1">Increase the counter by 1</a> <br>
<a href="#" jwc listener="listener:doClick" parameters="ognl:5">Increase the counter by 5</a> <br>
<a href="#" jwc listener="listener:doClick" parameters="ognl:10">Increase the counter by 10</a> <br>
<a href="#" jwc listener="listener:clearCounter">Clear the counter</a> <br>
<a href="#" jwc listener="listener:plus" parameters="ognl:{3,5}">calculate 3+5,</a>
The calculation result is: <span jwc value="ognl:result"></span>
<p>
<a href="#" jwc page="AddPerson">Add Personnel Information</a>
</body>
</html>

 
Copy the codeThe code is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html jwc title="Add Personnel Information">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body jwc>
<form jwc success="listener:save">
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td width="17%">Name</td>
<td width="20%"><input type="text" name="textfield" jwc value="ognl:"/></td>
<td width="10%">Age</td>
<td width="19%"><input type="text" name="textfield2" jwc value="ognl:"/></td>
<td width="10%">Date of Birth</td>
<td width="24%"><input type="text" name="textfield22" jwc translator="translator:date,pattern=yyyy-MM-dd" value="ognl:"/></td>
</tr>
<tr>
<td colspan="6"><div align="center">
<input type="submit" name="Submit" value="submit"/>
</div></td>
</tr>
</table>
</form>
</body>
</html>

Note: In the tag of the Tapestry component (using jwcid tag) in the page, there must be a corresponding close tag. Pay special attention to the <input/> tag, which must have a close tag.
 
Copy the codeThe code is as follows:

package ;
import ;
import ;
import ;
public abstract class AddPerson extends BasePage implements PageBeginRenderListener{
public abstract Person getPerson();
public abstract void setPerson(Person person);
public void pageBeginRender(PageEvent event) {
setPerson(new Person());
}
public void save(){
Person person = getPerson();
("name="+getPerson().getName());
("age="+getPerson().getAge());
("birthday="+getPerson().getBirthday());
}
}

Note that you need to define a PageBeginRenderListener to assign an initialization value to Person. Otherwise, an exception will occur when OGNL interprets its (Person object) attributes!
How to submit Chinese
We must add the following configuration in:
Copy the codeThe code is as follows:

<?xml version="1.0"?>
<!DOCTYPE application PUBLIC
"-//Apache Software Foundation//Tapestry Specification 4.0//EN"
"/dtd/Tapestry_4_0.dtd">
<application>
<meta key="-encoding" value="GBK"/>
<meta key="-encoding" value="GBK"/>
<meta key="-class-packages" value=""/>
</application>

How to turn to other pages
Use the @InjectPage annotation to inject the page directly. like:
Copy the codeThe code is as follows:

@InjectPage("ShowProject")public abstract ShowProject getShowProject(); public IPage doSubmit() { ShowProject showProject = getShowProject(); (getProject()); return showProject; }

Tapestry summary
In Tapestry, there is no need to write Action, but listener method. Instead of building the URL address, you use the DirectLink component and configure it to call the listener method.
Tapestry is component-centric. The so-called component-centered, that is:
l Tapestry application consists of a series of pages (pages)
l Pages are composed of smaller components (Components)
l The component itself may also be composed of other components (without limiting the depth of the combination)
l Each page will have a unique, non-repeat name
l The identifier of components in each page cannot be repeated (by default, Tapestry will automatically generate an ID identifier for each component in the page)
Tapestry and Spring integration
Integration requires a third-party class library, you can download: /downloads/tapestry-javaforge/tapestry-spring-1.0.
After decompressing it, copy the jar package into the classpath.
In our page class, you can use @InjectObject("spring:userManager") and other methods to access the userManager object.
like:
@InjectObject("spring:userManager")
public abstract UserManager getUserManager();
Tapestry component development
Previous page12Read the full text