java_ first year _JavaWeb (9)

JavaBean is a written follow a particular Java class , has the following characteristics:

  • It must have a no-argument constructor
  • Attributes Required privatization
  • Privatization attributes required by the public exposed to the other type of process programs, which also has some of the named specification

example:

package lzj.learn;
public
class Person{ private String name; private String age;//私有化的属性 public Person(){ } // no constructor parameters; public String getName () { return name; } public void setName(String name){ this.name = name; } pubilc int getAge(){ return age; } public void setAge(int age){ this.age = age; } // add method of privatization by public property and queries; its method name is fixed set, then later get the attribute name and the attribute name of the first letter capitalized; }

Use JavaBean in JSP

JSP provides three action tabs on the JavaBean components

  • <Jsp: useBean> tag: show find or instantiate a JavaBean components in JSP pages;

grammar:

<jsp:useBean id = "xxx" class = "package.class" scope = "page|request|session|application" />

Id attribute indicating the instance where the JavaBean object name, class property which indicates the full class name, the name of the package with the necessary, scope attribute field indicating the range stored instantiate objects, default Page;

  • <Jsp: setProperty>: property of a component tag JabaBean

grammar:

<jsp:setProperty name = "beanName" property = "propertyName" value = "string字符串|<%=expression%>|parameterName" />

Wherein the instantiated JavaBean object name of the name attribute, which is in front <jsp: useBean> tag id attribute set object name; Property name attribute is an attribute to be set, value attributes may comprise a string, or an expression variable parameter values; means that the variable parameter whose value is set by himself through parameterName = "xxx";

  • <Jsp: getProperty> tag: obtain attributes of a JavaBean components

grammar:

<jsp:getProperty name = "beanName" property = "PropertyName" />

Wherein the name, property and the meaning attribute of the attribute of a tag similar to setProperty;

3 combined with large jsp javabean tag related to the Person class above and do a simple JSP page:

<% page language = "java" import = "java.util.*" pageEncoding = "UTF-8" %>
<jsp:useBean id = "person" class="lzj.learn.Person" scope = "page" />
<%
    person.setName("小兆")
%>
<jsp:setPorperty name = "person" property = "age" param = "parameterName" />
<! DOCTYPE HTML>
<html>
    <head>
            <Title> jsp: javabean Use Example </ title>
    <head>
    <body>
        <h2>姓名:<%=peason.getName()%></h2>
        <h2>年龄:<jsp:getPorperty name = "person" property = "age" /></h2>
    </body>
</html>

Since we use <jsp: setPorperty> parameter value is set when a variable parameter param tag, when calling jsp to specify the value similar http://xxx.xxx/xxx/xxx.jsp?parameterName=23 Thus, the value of the property after the age of 23 is set to be displayed;

Guess you like

Origin www.cnblogs.com/lzj-learn/p/11637041.html