Summary
1. Tapestry has two main types of requests: action requests and render requests.
Render requests are easy, the URL includes just the page name, and that page is rendered out.
Action requests are more complicated; the URL will include the name of the page and the id of the component within the page, and perhaps the type of event.
2.The code source about High/Low Guessing Game
Start.html is as follows:
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<head>
<title>tutorial1 Start Page</title>
</head>
<body>
<h1>High/Low Guess</h1>
<p>I'm thinking of a number between one and ten ... </p>
<p>
<t:actionlink>Start guessing</t:actionlink>
</p>
</body>
</html>
Start.java is as follows :
import java.util.Random;
import org.apache.tapestry.annotations.InjectPage;
public class Start
{
private final Random _random = new Random();
@InjectPage
private Guess _guess;
Object onAction()
{
int target = _random.nextInt(10) + 1;
_guess.setup(target);
return _guess;
}
}
Note: In the Start.java , the method onAction does not includes the target link(URL) , but how to get the next page ? the key is the @InjectPage annotation I think,when theMethod return the instance of @InjectPage , tapestry could get(render) the next page and send it to the clinet.
Here is the Guess.html:
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<head>
<title>Guess A Number</title>
</head>
<body>
<p>Make a guess between one and ten:</p>
<p>${message}</p>
<t:loop source="1..10" value="guess">
<t:actionlink t:id="link" context="guess">${guess}</t:actionlink>
</t:loop>
</body>
</html>
Guess.java is as follows:
package org.apache.tapestry.tutorial.pages;
public class Guess
{
@Persist
private int _target;
@Persist
private String _message;
@Persist
private int _count;
@InjectPage
private GameOver _gameOver;
private int _guess;
void setup(int target)
{
_target = target;
_count = 0;
}
public int getTarget()
{
return _target;
}
public int getGuess()
{
return _guess;
}
public void setGuess(int guess)
{
_guess = guess;
}
public String getMessage()
{
return _message;
}
String onActionFromLink(int guess)
{
_count++;
if (guess == _target){
_gameOver.setup(_count);
return _gameOver;
}
if (guess < _target)
_message = String.format("%d is too low.", guess);
else
_message = String.format("%d is too high.", guess);
return null;
}
}
Note: the Start.html transit a parameter to the Guess.html ,the parameter named _target (the underscores is NOt requirement),
in the Guess.java the method uses the name setup() instead of setTarget(), because there's more setup than just initializing
the _target instance variable. it is easy to see that pages as classes that store internal state and communicate with each other
Now,look at the GameOver.html:
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<head>
<title>Game Over!</title>
</head>
<body>
<h1>Game Over</h1>
<p> You guessed the secret number in ${count} guesses! </p>
</body>
</html>
and the GameOver.java is as follows:
import org.apache.tapestry.annotations.Persist;
public class GameOver
{
@Persist
private int _count;
public int getCount()
{
return _count;
}
void setup(int count)
{
_count = count;
}
}