Chapter 3 input verification -- enjoying web developmenenjoying

Source: Internet
Author: User

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.

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.