We will see how the bean tag works using a currency converter example. In this example we will convert dollars to rupees. We do this using the CurrencyConverter JavaBeans class.
The CurrencyConverter class.
01.package vaannila;
02.
03.public class CurrencyConverter {
04.
05. private float rupees;
06. private float dollars;
07.
08. public float getRupees() {
09. return dollars * 50;
10. }
11. public void setRupees(float rupees) {
12. this.rupees = rupees;
13. }
14. public float getDollars() {
15. return rupees/50 ;
16. }
17. public void setDollars(float dollars) {
18. this.dollars = dollars;
19. }
20.
21.}
The next step is to create an instance of the CurrencyConverter bean in the jsp page using the bean tag. We can either use the bean tag to push the value onto the ValueStack or we can set a top-level reference to it in the ActionContext. Let's see one by one.
First we will see how we can do this by pushing the value onto the ValueStack. The index.jsp page contains the following code.
01.<html>
02.<head> !--判断阅读权限-->!--判断是否已经扣点--> |