JSP[8]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 lemonade 的 blog

<link rel=stylesheet href="my-style-sheet.css" type="text/css"> </head> <body> <center> <table border=5> <tr><th class="title"> reusing javabeans in jsp</table> </center> <p> <jsp:usebean id="test" class="hall.simplebean" /> <jsp:setproperty name="test" property="message" value="hello www" /> <h1>message: <i> <jsp:getproperty name="test" property="message" /> </i></h1> </body> </html>

simplebean.java

here's the source code for the bean used in the beantest jsp page. you can also download the source.
package hall;

public class simplebean {
  private string message = "no message specified";

  public string getmessage() {
    return(message);
  }

  public void setmessage(string message) {
    this.message = message;
  }
}
here's a typical result:
beantest output

8.3 more jsp:usebean details

the simplest way to use a bean is to use
   <jsp:usebean id="name" class="package.class" />
to load the bean, then use jsp:setproperty and jsp:getproperty to modify and retrieve bean properties. however, there are two other options. first, you can use the container format, namely
  <jsp:usebean ...>
    body
  </jsp:usebean>

to indicate that the body portion should be executed only when the bean is first instantiated, not when an existing bean is found and used. as discussed below, beans can be shared, so not all jsp:usebean statements result in a new bean being instantiated. second, in addition to id and class, there are three other attributes that you can use: scope, type, and beanname. these attributes are summarized in the following table.

atribute usage
id gives a name to the variable that will reference the bean. a previous bean object is used instead of instantiating a new one if one can be found with the same id and scope.
class designates the full package name of the bean.
scope indicates the context in which the bean should be made available. there are four possible values: page, request, session, and application. the default, page, indicates that the bean is only available on the current page (stored in the pagecontext of the current page). a value of request indicates that the bean is only available for the current client request (stored in the servletrequest object). a value of session indicates that the object is available to all pages during the life of the current httpsession. finally, a value of application indicates that it is available to all pages that share the same servletcontext. the reason that the scope matters is that a jsp:usebean entry will only result in a new object being instantiated if there is no previous object with the same id and scope. otherwise the previously existing object is used, and any jsp:setparameter elements or other entries between the jsp:usebean start and end tags will be ignored.
type specifies the type of the variable that will refer to the object. this must match the classname or be a superclass or an interface that the class implements. remember that the name of the variable is designated via the id attribute.
beanname gives the name of the bean, as you would supply it to the instantiate method of beans. it is permissible to supply a type and a beanname, and omit the class attribute.

本文关键:JSP
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top