Original post address: http://linuxboy.javaeye.com/blog/57682
The following shows how to perform page navigation and use the input box with T: value:
Start.html (messages is used for localization to display Chinese characters ):
HTML code
- <HTML xmlns: t = "http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- <Title> Hilo-$ {message: page-title} <! -- </Span --> title>
- <! -- </Span --> head>
- <Body>
- <Div align = "center"> <H3 >$ {message: Body-title} <! -- </Span --> h3> <! -- </Span --> div>
- <Div align = "center">
- <Form T: TYPE = "form">
- <Table cellpadding = "3">
- <Tr> <TD colspan = "2" align = "center" bgcolor = "cc00aa" >$ {message: Body-title} <! -- </Span --> TD> <! -- </Span --> tr>
- <Tr> <TD >$ {message: Name-label} <! -- </Span --> TD> <input T: TYPE = "textfield" T: Id = "username" T: value = "user. name "type =" text "/> <! -- </Span --> TD> <! -- </Span --> tr>
- <Tr> <TD >$ {message: Pass-label} <! -- </Span --> TD> <input T: TYPE = "passwordfield" T: Id = "password" T: value = "user. pass "type =" password "/> <! -- </Span --> TD> <! -- </Span --> tr>
- <Tr> <TD colspan = "2" align = "center"> <input type = "Submit" value = "login"/> <input type = "reset" value =" reset "/> <! -- </Span --> TD> <! -- </Span --> tr>
- <! -- </Span --> table>
- <! -- </Span --> form>
- <! -- </Span --> div>
- <! -- </Span --> body>
- <! -- </Span --> HTML>
Start. Java: Java code
- Package org. Examples. pages;
- Import org. Apache. tapestry. Annotations. injectpage;
- Import org. Examples. Data. user;
- Public class start {
- Private user _ user;
- @ Injectpage
- Private test _ test;
- Void onprepare (){
- If (_ User = NULL)
- _ User = new user ();
- }
- String onsuccess (){
- _ Test. setuser (_ User );
- System. Out. println ("the current login user is:" + getuser (). getname ());
- Return "test ";
- }
- Public user getuser (){
- Return _ user;
- }
- Public void setuser (User user ){
- _ User = user;
- }
- }
Start. properties:
Code:
- Page-Title = logon page
- Body-Title = User Logon
- Name-label = User Name:
- Pass-label = password:
User. Java: Java code
- Package org. Examples. Data;
- Public class user {
- Private string _ name;
- Private string _ pass;
- Public String getname (){
- Return _ name;
- }
- Public String getpass (){
- Return _ pass;
- }
- Public void setname (string name ){
- _ Name = Name;
- }
- Public void setpass (string pass ){
- _ Pass = pass;
- }
- }
Test.html: XML Code
- <HTML xmlns: t = "http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- <Title> HiLo Login here <! -- </Span --> title>
- <! -- </Span --> head>
- <Body>
- Welcome, $ {user. name }!
- <! -- </Span --> body>
- <! -- </Span --> HTML>
Test. Java: Java code
- Package org. Examples. pages;
- Import org. Apache. tapestry. Annotations. persist;
- Import org. Examples. Data. user;
- Public class test {
- @ Persist
- Private user _ user;
- String onactivate (){
- If (_ User = NULL | _ User. getname () = "")
- Return "start ";
- Else return NULL;
- }
- Public void setuser (User user ){
- _ User = user;
- }
- Public user getuser (){
- Return _ user;
- }
- }
Summary:
1. Use the onactivate () method for page navigation (page navigation), especially page protection;
2. Use an image
- <Input T: TYPE = "textfield" T: Id = "name" T: value = "user. Name"/>
During template initialization, the user object must be initialized, otherwise the page will not run properly (throwing an exception ). The onprepare () method is used for object initialization.
3. beaneditform can be used for logon, creation, and update pages, which is more convenient and convenient. The template is
- <T: comp type = "beaneditform" Object = "user" submitlabel = "login"/>
In the page class, you only need to create the getter and setter of the user object. onprepare () is no longer required to initialize the user object.