[Change] JavaBean introduce

From the Java language's point of view, JavaBean is a purely a Java class, which abide by some rules and naming, for example, it is an open category, with a public setter and getter methods, support for event handling, etc., JavaBean is a reusable components can be used to design the user interface components, or used in JSP pages and data logic processing to carry.

For dynamic pages, if it is also responsible for the generation of Web content, is also responsible for data processing and preservation, the final result often is a part of page rendering with the program logic processing, and handling of data objects mixed in together, on the need to cooperate with each other website designers and programmers alike a kind of troubled, JavaBean One of the roles is to want to program the logic to deal with rendering part of the separation
For the JSP designers, it should make full use of technologies such as JavaBean (that is, there are other technologies) to separate the presentation of programs and web pages.

JavaBean's writing as a general Java program, let's write a simple Java class, its role is to find the greatest common factor of two numbers:

package beantools.math;
public class FindGCD
   private int m, n;
   public void setM(int m)
      this.m = m;
   public void setN(int n)
      this.n = n;
   public int getGCD()
      int r = 0;
      while(n != 0)
         r = m % n;
         m = n;
         n = r;
      return m;

Note that in JSP 2.0, after, JavaBean class must have a package to be managed; above methods in this category are owned by the public,
And using setXXX () and getXXX () approach to naming, will compile this class is complete, you will type *. class placed on the Web application WEB-INF/classes / folder, note that because we set the package to beantools . math, so in the end of your *. class
Should be the path should be WEB-INF/classes/beantools/math/FindGCD.class, then we will be in this category in the JSP pages to use as a JavaBean, we write the JSP page the following programs:

<jsp:useBean />
<jsp:setProperty name="gcd" property="*" />
<%= request.getParameter("m") %>  And  <%= request.getParameter("n") %>
 Greatest common divisor:  <jsp:getProperty name="gcd" property="GCD" />

<jsp:useBean> tag is used to call JavaBean methods, id is used to specify a reference name of the JavaBean instance, it actually will be converted to
Servlet in an object reference to the name, class to specify your JavaBean which will be an instance of a category, in the implementation of the JSP page,
It will check whether an instance of JavaBean already exists, if does not exist, then an instance of a JavaBean.

<jsp:setProperty> tags used to set the JavaBean property values, name used to specify which you want to use a reference to the name a reference to the
JavaBean instance; in property to set when we use the introspection mechanism to set the property "*" that will automatically search for
request that meets the JavaBean in the name of the parameter value setter that if the request parameter name if they meet the setXXX () in the XXX name,
Will request the corresponding value is set to setXXX ().

<jsp:getProperty> is used to access JavaBean property values carried by, name used to specify which you want to use a reference to the name a reference to the JavaBean instance is specified in the property to which an attribute value, that is, if designated as a XXX, using getXXX () method to obtain the value.
We are directly in the address bar to send the following message can obtain the greatest common factor:


The back reads as follows:

100  And 14 of the greatest common divisor: 2

As shown above, using JavaBean, we can move some of the programmable logic JavaBean in JSP pages without having to write code directly in logic, as long as the JSP page to use <jsp:useBean>, <jsp:setProperty> and <jsp:getProperty> such as labels, tags used in HTML pages more intuitive, not with a lot of programmable logic portion can have a "majority of the" view and the logical separation of purpose (alone JavaBean to achieve full separation is still difficult).
It should be noted is that whenever you change the contents of the JavaBean programming and re-compiled, Container may not necessarily be aware of the contents of the JavaBean has changed, there will be no re-loaded after the change a new category, which is one of the shortcomings of using the JavaBean The solution is to re-start
Container, so that changes in Container reloading after JavaBean.


分类:Java 时间:2010-03-29 人气:298
blog comments powered by Disqus


iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.869 (s). 14 q(s)