[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 人气:339
blog comments powered by Disqus


  • Calculate the greatest common factor of two numbers 2010-09-15

    Given two positive integers m and n, seeking their greatest common factor, that can also divides m and n, the largest positive integer. E1. [] To seek balance and to make r m n addition to the remainder as income. (We will have 0 <= r <n) E2. [Remai

  • int type java byte code based acquaintance 2010-12-27

    The Internet to see a similar post, a little summary of learning it. Detailed bytecode package com.test; public class Test1 { public void test1() { int a = 1; int b = 2; int c = -1; int d = -2; } public void test2() { int a = 5; int b = 6; int c = -5

  • [Reprinted] JAVA developers to the 25 most common English website 2010-03-29

    JAVA developer to the 25 most common English website http://www.infoq.com/ - Info IT News http://www.apache.org/ - Apache Foundation http://www.springsource.org/ - Majority of Java developers love Spring http://www.hibernate.org/ - Open-source ORM fr

  • JAVA developer to the 20 most common English website 2010-03-29

    JAVA developer to the 20 most common English website 1. [ http://www.javaalmanac.com ] - Java developers to the online version of the Yearbook of the book. To quickly find the usage of certain techniques, and sample Java code, this is a good place to

  • flex and java how communication with the public session webService 2010-12-26

    flex and java how communication with the public session webService This is very troublesome, because you have to be very understanding of what session which in the end (as to flex there, not so strict over there in java) The correct approach is, java

  • On ozzzzz brother of "java is a language or a platform." 2007-02-27

    Quote java will die? This problem is very wonderful. If the race as a debate topic is very appropriate. java representative of the concept of what exactly is a language or a platform, or on behalf of the above two. Yes or no, this is a problem. If it

  • Some rights java modifiers, private / friendly / protected / public 2010-05-29

    Begin testing the following: A Test class, as was access to the class, private not say, can access only its own, public is able to access, this is mainly to distinguish between friendly and protected New two classes, Test1 and Test2 to visit Test, Te

  • Teacher-Java Web development platform based on actual classic papers (JSP.Servlet.Struts.Ajax) 2010-08-26

    Teacher-Java Web development platform based on actual classic articles (JSP, Servlet, Struts, Ajax) Basic Information Of: Li Xinghua Wang Yueqing Publisher: Tsinghua University Press ISBN: 9787302231585 Added time :2010-8-25 Publication Date: Aug. 20

  • Java Persistence on pooling of resources related to: Java Persistence API 2010-11-12

    BEA's first to a webinar (Webinar): EJB3 Java Persistence API: Good, the Bad and the general function of attachment is the PPT data. Java Persistence API: The Good, the Bad, and the Ugly Java Persistence API: Good, the Bad and the general features JP

  • Java程序员最常用的8个Java日志框架 2015-02-05

    作为一名Java程序员,我们开发了很多Java应用程序,包括桌面应用.WEB应用以及移动应用.然而日志系统是一个成熟Java应用所必不可少的,在开发和调试阶段,日志可以帮助我们更好更快地定位bug:在运行维护阶段,日志系统又可以帮我们记录大部分的异常信息,从而帮助我们更好的完善系统.本文要来分享一些Java程序员最常用的Java日志框架组件. 1.Log4j – 最受欢迎的Java日志组件 Log4j是一款基于Java的开源日志组件,Log4j功能非常强大,我们可以将日志信息输出到控制台.文件.

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.633 (s). 10 q(s)