Basic Application of form in T5 Learning

Source: Internet
Author: User

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
  1. <HTML xmlns: t = "http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
  2. <Head>
  3. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  4. <Title> Hilo-$ {message: page-title} <! -- </Span --> title>
  5. <! -- </Span --> head>
  6. <Body>
  7. <Div align = "center"> <H3 >$ {message: Body-title} <! -- </Span --> h3> <! -- </Span --> div>
  8. <Div align = "center">
  9. <Form T: TYPE = "form">
  10. <Table cellpadding = "3">
  11. <Tr> <TD colspan = "2" align = "center" bgcolor = "cc00aa" >$ {message: Body-title} <! -- </Span --> TD> <! -- </Span --> tr>
  12. <Tr> <TD >$ {message: Name-label} <! -- </Span --> TD> <input T: TYPE = "textfield" T: Id = "username" T: value = "user. name "type =" text "/> <! -- </Span --> TD> <! -- </Span --> tr>
  13. <Tr> <TD >$ {message: Pass-label} <! -- </Span --> TD> <input T: TYPE = "passwordfield" T: Id = "password" T: value = "user. pass "type =" password "/> <! -- </Span --> TD> <! -- </Span --> tr>
  14. <Tr> <TD colspan = "2" align = "center"> <input type = "Submit" value = "login"/> <input type = "reset" value =" reset "/> <! -- </Span --> TD> <! -- </Span --> tr>
  15. <! -- </Span --> table>
  16. <! -- </Span --> form>
  17. <! -- </Span --> div>
  18. <! -- </Span --> body>
  19. <! -- </Span --> HTML>

Start. Java: Java code

  1. Package org. Examples. pages;
  2. Import org. Apache. tapestry. Annotations. injectpage;
  3. Import org. Examples. Data. user;
  4. Public class start {
  5. Private user _ user;
  6. @ Injectpage
  7. Private test _ test;
  8. Void onprepare (){
  9. If (_ User = NULL)
  10. _ User = new user ();
  11. }
  12. String onsuccess (){
  13. _ Test. setuser (_ User );
  14. System. Out. println ("the current login user is:" + getuser (). getname ());
  15. Return "test ";
  16. }
  17. Public user getuser (){
  18. Return _ user;
  19. }
  20. Public void setuser (User user ){
  21. _ User = user;
  22. }
  23. }

Start. properties:

Code:
  1. Page-Title = logon page
  2. Body-Title = User Logon
  3. Name-label = User Name:
  4. Pass-label = password:

User. Java: Java code

  1. Package org. Examples. Data;
  2. Public class user {
  3. Private string _ name;
  4. Private string _ pass;
  5. Public String getname (){
  6. Return _ name;
  7. }
  8. Public String getpass (){
  9. Return _ pass;
  10. }
  11. Public void setname (string name ){
  12. _ Name = Name;
  13. }
  14. Public void setpass (string pass ){
  15. _ Pass = pass;
  16. }
  17. }

Test.html: XML Code

  1. <HTML xmlns: t = "http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
  2. <Head>
  3. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  4. <Title> HiLo Login here <! -- </Span --> title>
  5. <! -- </Span --> head>
  6. <Body>
  7. Welcome, $ {user. name }!
  8. <! -- </Span --> body>
  9. <! -- </Span --> HTML>

Test. Java: Java code

  1. Package org. Examples. pages;
  2. Import org. Apache. tapestry. Annotations. persist;
  3. Import org. Examples. Data. user;
  4. Public class test {
  5. @ Persist
  6. Private user _ user;
  7. String onactivate (){
  8. If (_ User = NULL | _ User. getname () = "")
  9. Return "start ";
  10. Else return NULL;
  11. }
  12. Public void setuser (User user ){
  13. _ User = user;
  14. }
  15. Public user getuser (){
  16. Return _ user;
  17. }
  18. }

Summary:
1. Use the onactivate () method for page navigation (page navigation), especially page protection;
2. Use an image

  1. <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

  1. <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.

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.