Chapter 3 input verification
What do we talk about in this chapter?
In this chapter, you will learn how to verify user input and how to display error information if necessary. In addition, you will learn how to render a component in a loop.
Postage Calculator
Imagine you will develop a calculator to calculate the postage required to send a parcel from somewhere to another place. The user enters the weight of the package in kilograms (SEE ). He can also selectively enter a customer code that identifies him to get a certain discount. Click OK and it will display the postage.
The region is as follows:
<HTML>
<Form into cid = "form">
<Table>
<Tr>
<TD> weight: </TD>
<TD> <input type = "text" cid = "weight"/> </TD>
</Tr>
<Tr>
<TD> patron code: </TD>
<TD> <input type = "text" cid = "patroncode"/> </TD>
</Tr>
<Tr>
<TD> </TD>
<TD> <input type = "Submit"/> </TD>
</Tr>
</Table>
</Form>
</Html>
Define components in home. Page:
<Page-specification class = "com. ttdev. Postage. Home">
<Component id = "form" type = "form">
<Binding name = "listener" value = "listener: onsubmit"/>
</Component>
<Component id = "weight" type = "textfield">
<Binding name = "value" value = "weight"/>
</Component>
<Component id = "patroncode" type = "textfield">
<Binding name = "value" value = "patroncode"/>
</Component>
</Page-Specification>
Home. Java is like this:
Public abstract class home extends basepage {
Private map patroncodetodiscount;
@ Injectpage ("result ")
Public abstract ipage getresult ();
Public abstract string getweight ();
Public abstract string getpatroncode ();
Public home (){
Patroncodetodiscount = new hashmap ();
Patroncodetodiscount. Put ("p1", new INTEGER (90 ));
Patroncodetodiscount. Put ("p2", new INTEGER (95 ));
}
Public ipage onsubmit (){
Int Weight = integer. parseint (getweight ());
Integer discount = (integer) patroncodetodiscount. Get (getpatroncode ());
Int postageperkg = 10;
Int postage = weight * postageperkg;
If (discount! = NULL ){
Postage = postage * discount. intvalue ()/100;
}
Ipage resultpage = getresult ();
Propertyutils. Write (resultpage, "Postage", new INTEGER (postage ));
Return resultpage;
}
}
Create a fruit page. result.html is like this.
<HTML>
The postage is <span partition cid = "@ Insert" value = "ognl: Postage"/>.
</Html>
Result. Page is as follows:
<Page-Specification>
<Property name = "Postage"/>
</Page-Specification>
Because the page class is not specified, the basepage will be used. Tapestry will create its subclass to own attributes.
Next, create a context descriptor postage. XML in C:/tomcat/CONF/Catalina/localhost:
<Context
Docbase = "C:/workspace/postage/context"
Path = "/postage"
Reloadable = "true"/>
Now you can run this program:
Receive integer input
At this moment, the weight attribute of the home page is portable, which is not good. The ideal value is an integer.