Tutorials to .com

Tutorials to .com » Jsp » Tapestry-hivemind » 3 Cart tapestry to buy fruit

3 Cart tapestry to buy fruit

Print View , by: iSee ,Total views: 5 ,Word Count: 3587 ,Date: Thu, 16 Apr 2009 Time: 4:53 AM

In order to purchase fruit, we need to Shop page for the name of each fruit to add a link, once the user clicks on the fruit, we purchase fruit Jump to a page that displays detailed information about the fruit and can be purchased to allow the user to enter.

Shop for fruit and the name of each page to add links:

  Modify Shop.java:    com.tapestry4; package com.tapestry4;    java.util.List; import java.util.List;    org.apache.tapestry.IPage; import org.apache.tapestry.IPage;    org.apache.tapestry.annotations.InjectPage; import org.apache.tapestry.annotations.InjectPage;    org.apache.tapestry.html.BasePage; import org.apache.tapestry.html.BasePage;    class Shop extends  BasePage { public abstract class Shop extends BasePage (           ) @ InjectPage ( "BuyFruit")            BuyFruit getBuyFruitPage(); public abstract BuyFruit getBuyFruitPage ();            List getFruitList() { public List getFruitList () (                    Products.getProductList(); return Products.getProductList ();           )            IPage buyFruit( Integer  ig) { public IPage buyFruit (Integer ig) (                   .getBuyFruitPage(); BuyFruit page = this. GetBuyFruitPage ();                   page.setFruitId (ig);                    page; return page;           )   ) 
  Modify Shop.page:   <page-specification>       /> <property name= "fruit" />        type= "Foreach" > <component id= "fruitListLoop" type= "Foreach">            value= "fruitList" /> <binding name= "source" value= "fruitList" />            value= "fruit" /> <binding name= "value" value= "fruit" />            value= "literal:tr" /> <binding name= "element" value= "literal:tr" />       </ component>        type= "Insert" > <component id= "fruitName" type= "Insert">            value= "fruit.name" /> <binding name= "value" value= "fruit.name" />       </ component>        type= "Insert" > <component id= "fruitPrice" type= "Insert">            value= "fruit.price" /> <binding name= "value" value= "fruit.price" />       </ component>        type= "DirectLink" > <component id= "buyFruitLink" type= "DirectLink">            value= "listener:buyFruit" /> <binding name= "listener" value= "listener:buyFruit" />            value= "fruit.id" /> <binding name= "parameters" value= "fruit.id" />       </ component>   </ page-specification> 
  Modify Shop.html:   <html>   <body>    border= "1" > <table width= "419" border= "1">     > <tr align= "center">       ></td> <td width= "240"> fruit name </ td>       ></td> <td width= "139"> Price </ td>     </ tr>      align= "center" > <tr jwcid= "fruitListLoop" align= "center">        jwcid= "buyFruitLink" ><span jwcid= "fruitName" ></span></a></td> <td> <a href= "#" jwcid= "buyFruitLink"> <span jwcid= "fruitName"> name </ span> </ a> </ td>       ></span></td> <td> <span jwcid= "fruitPrice"> Price </ span> element </ td>     </ tr>   </ table>   </ body>   </ html> 


Changes from above can be seen, we added a DirectLink component, this component is in the Tapestry, one of two forms of DirectServcie, is another user-submitted forms. DirectLink component HTML <a> for a performance element, used to provide a URL, when the user clicks, the trigger of a particular page of java monitor methods. tapestry framework is based on the event mechanism, so you can swing the image to associate the event. We know from the statement of the components will find:

   value= "listener:buyFruit" /> <binding name= "listener" value= "listener:buyFruit" /> 

Shop.java the component will trigger the buyFruit methods and pass parameters:

   value= "fruit.id" /> <binding name= "parameters" value= "fruit.id" /> 

Familiar with the T3 will find friends, T4 in the use of monitoring methods is very different, and now we see another way to monitor usage. DirectLink transmitted directly to the input parameters as part of its monitoring method parameters. Once the trigger monitor DirectLink, we can monitor directly on the way to get the input parameters are passed back to fruit.id. Of course, methods to monitor the input parameter is the type of the corresponding fruit.id.

   IPage buyFruit( Integer  ig) { public IPage buyFruit (Integer ig) (           .getBuyFruitPage(); BuyFruit page = this. GetBuyFruitPage ();           page.setFruitId (ig);            page; return page;   ) 

In this monitoring method, we obtain the DirectLink transmission parameters, and we pass down a page of the ID value of fruits, the next page, we will be in accordance with the ID value that corresponds to the fruit to find more information.

Fruit detailed information page:

  BuyFruitPage.java    com.tapestry4; package com.tapestry4;    java.util.ArrayList; import java.util.ArrayList;    java.util.List; import java.util.List;    org.apache.tapestry.html.BasePage; import org.apache.tapestry.html.BasePage;    class BuyFruit extends  BasePage { public class BuyFruit extends BasePage (           Integer  fruitId; private Integer fruitId;           int  buyMount; private int buyMount;           @ Override            void initialize() { protected void initialize () (                   ; fruitId = null;                   buyMount = 0;           )           public  Fruit getFruitDetail() { / / Obtain detailed information of fruit public Fruit getFruitDetail () (                    Products.findFruitById( this .getFruitId()); return Products.findFruitById (this. getFruitId ());           )                    @SuppressWarnings( "deprecation" ) / / The purchase of fruits @ SuppressWarnings ( "deprecation")           String  buyFruitSubmit() { public String buyFruitSubmit () (                   .out.println( ":"  + this .getBuyMount()); System. Out.println ( "the purchase of the number of fruits:" + this. GetBuyMount ());                   .setFruitId( this .getFruitId()); this. setFruitId (this. getFruitId ());                   ; return this;           )           public String  backShopSubmit() { / / Re-select the fruit public String backShopSubmit () (                   "Shop" ; return "Shop";           )           Integer  getFruitId() { public Integer getFruitId () (                    fruitId; return fruitId;           )            void setFruitId( Integer  fruitId) { public void setFruitId (Integer fruitId) (                   .fruitId = fruitId; this. fruitId = fruitId;           )           int  getBuyMount() { public int getBuyMount () (                    buyMount; return buyMount;           )            void setBuyMount( int  buyMount) { public void setBuyMount (int buyMount) (                   .buyMount = buyMount; this. buyMount = buyMount;           )   ) 
  BuyFruitPage.page   <page-specification>        type= "Insert" > <component id= "fruitName" type= "Insert">            value= "fruitDetail.name" /> <binding name= "value" value= "fruitDetail.name" />       </ component>        type= "Insert" > <component id= "fruitPrice" type= "Insert">            value= "fruitDetail.price" /> <binding name= "value" value= "fruitDetail.price" />       </ component>        type= "Insert" > <component id= "fruitMount" type= "Insert">            value= "fruitDetail.mount" /> <binding name= "value" value= "fruitDetail.mount" />       </ component>        type= "Form" > <component id= "buyFruitForm" type= "Form">            value= " true " /> <binding name= "clientValidationEnabled" value= "true" />       </ component>        type= "Hidden" > <component id= "fruitIdHidden" type= "Hidden">            value= "fruitId" /> <binding name= "value" value= "fruitId" />       </ component>        type= "TextField" > <component id= "fruitBuyMount" type= "TextField">            value= "buyMount" /> <binding name= "value" value= "buyMount" />            value= "translator:number" /> <binding name= "translator" value= "translator:number" />            value= "literal:" /> <binding name= "displayName" value= "literal:" />       </ component>        type= "Submit" > <component id= "buyFruitSubmit" type= "Submit">            value= "listener:buyFruitSubmit" /> <binding name= "listener" value= "listener:buyFruitSubmit" />       </ component>        type= "Submit" > <component id= "backShopSubmit" type= "Submit">            value= "listener:backShopSubmit" /> <binding name= "listener" value= "listener:backShopSubmit" />       </ component>   </ page-specification> 
  BuyFruitPage.html   <html>   > <body jwcid= "@Body">   > <form jwcid= "buyFruitForm">   /> <span jwcid= "fruitIdHidden" />    border= "1" > <table width= "242" border= "1">     > <tr align= "center">       ></td> <td width= "121"> fruit name </ td>       ><span jwcid= "fruitName" ></span></td> <td width= "105"> <span jwcid= "fruitName"> name </ span> </ td>     </ tr>     > <tr align= "center">       <td> Price </ td>       >1.00</span></td> <td> <span jwcid= "fruitPrice"> 1.00 </ span> element </ td>     </ tr>     > <tr align= "center">       <td> stock </ td>       >20</span></td> <td> <span jwcid= "fruitMount"> 20 </ span> jin </ td>     </ tr>     > <tr align= "center">       the number of <td> purchase </ td>        jwcid= "fruitBuyMount"  size= "3"  length= "3" /></td> <td> <input type= "text" jwcid= "fruitBuyMount" size= "3" length= "3" /> jin </ td>     </ tr>     > <tr align= "center">        jwcid= "backShopSubmit"  value= "" /></td> <td> <input type= "submit" jwcid= "backShopSubmit" value= "" /> </ td>        jwcid= "buyFruitSubmit"  value= "" /></td> <td> <input type= "submit" jwcid= "buyFruitSubmit" value= "" /> </ td>     </ tr>   </ table>   </ form>   </ body>   </ html> 


In order to form submitted, we can access to fruitId, set up a Hidden components:

   type= "Hidden" > <component id= "fruitIdHidden" type= "Hidden">            value= "fruitId" /> <binding name= "value" value= "fruitId" />       </ component> 

When the operation of the component will be input to the page <input type="hidden" value=""/> label, its role and type = "hidden", like the input tag.

When the choice of fruit in the Shop page, we DirectLink to this page through a fruit delivery ID. Then, in the following way, we received a fruit detailed information:

  public  Fruit getFruitDetail() { / / Obtain detailed information of fruit public Fruit getFruitDetail () (          Products.findFruitById( this .getFruitId()); return Products.findFruitById (this. getFruitId ());   ) 

This method returns the object is a Fruit, and we directly through the Insert component, will be the fruit of information output to the page:

   type= "Insert" > <component id= "fruitName" type= "Insert">            value= "fruitDetail.name" /> <binding name= "value" value= "fruitDetail.name" />       </ component>        type= "Insert" > <component id= "fruitPrice" type= "Insert">            value= "fruitDetail.price" /> <binding name= "value" value= "fruitDetail.price" />       </ component>        type= "Insert" > <component id= "fruitMount" type= "Insert">            value= "fruitDetail.mount" /> <binding name= "value" value= "fruitDetail.mount" />       </ component> 

Next, we want the user to input the number of fruit to buy, or to submit the purchase, or return to the previous page to re-select fruits Shop.
Therefore, we need a form, and use the TextField components:

   type= "Form" > <component id= "buyFruitForm" type= "Form">            value= " true " /> <binding name= "clientValidationEnabled" value= "true" />       </ component>        type= "TextField" > <component id= "fruitBuyMount" type= "TextField">            value= "buyMount" /> <binding name= "value" value= "buyMount" />            value= "translator:number" /> <binding name= "translator" value= "translator:number" />            value= "literal:" /> <binding name= "displayName" value= "literal:" />       </ component> 

TextField component here is used and in front of the same, but we found an interesting parameter translator. As a text input box, and ultimately the performance of the client are the String. If we use the String type buyMount to load the number of users to buy fruit, then eventually when we have statistics, but also will be converted to String we are used to calculate the int type. It seems there are troubles. However, T4 is now provided us with a convenient solution in BuyFruit.java a closer look at the type of variable buyMount members, we directly use the int. TextField components through translator parameters, T4 will be for our type conversion automatically. At the same time, if the user typed in the text of non-digital data, TextField can be automatically carried out for our well-tested, easy, right?

translator currently supports three types: date, number, String.

If the user does not want to buy this fruit, and wish to re-choose fruits, you can click on the "re-select" and submit:

   type= "Submit" > <component id= "backShopSubmit" type= "Submit">            value= "listener:backShopSubmit" /> <binding name= "listener" value= "listener:backShopSubmit" />       </ component> 

Submit component will trigger the monitor backShopSubmit methods:

  public String  backShopSubmit() { / / Re-select the fruit public String backShopSubmit () (                   "Shop" ; return "Shop";           ) 

Since we do not need to pass any parameters, only the Jump page, then the method returns to monitor the page name, and will jump to the page.


tapestry & hivemind Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.