JavaBeans

Introduction to Javabeans:

Javabeans are Java classes  that conform to a specific specification . The advantage of using Javabeans is to solve code duplication, reduce code redundancy, clearly distinguish functions, and improve code maintainability.

Javabean design principles:

  (1) Public class

  (2) Public constructor without parameters

  (3) Attribute private

  (4) getter and setter methods

 1. What is Jsp action?

  JSP action elements (action elements), action elements provide information for the request processing stage. Action elements follow the syntax of XML elements, have an opening tag containing the element name, can have attributes, optional content, and an closing tag that matches the opening tag.

  The first category is related to accessing JavaBeans, including:

    <jsp:useBean> <jsp:setProperty> <jsp:getProperty>

  The second category is the basic elements that JSP1.2 began to have, including 6 action elements

    <jsp:include> <jsp:forward><jsp:param><jsp:plugin><jsp:params><jsp:fallback>

  The third category is the newly added elements of JSP2.0, mainly related to JSP Document, including six elements

    <jsp:root> <jsp:declaration> <jsp:scriptlet><jsp:expression><jsp:text><jsp:output>

  The fourth category is the new action element of JSP2.0, which is mainly used to dynamically generate the value of the XML element tag, including 3 actions

    <jsp:attribute><jsp:body><jsp:element>

  The fifth category is the new action element of JSP2.0, mainly used in Tag File, there are 2 elements

    <jsp:invoke><jsp:dobody>

 

2. How to use Javabeans in Jsp pages?

  (1) Create a javabean instance like a normal java class.

  (2) The jsp action tag is usually used in the Jsp page to use javabean.

      a. useBeans action

      b. setProperty action

      c. getProperty action

    grammar:

     ①<jsp:useBeans>

      Role: instantiate in the jsp page or use javabean in the specified scope:

      <jsp:useBean id = "identifier" class = "java class name" scope = "scope" />

      

      ②<jsp:setProperty>

      Function: Assign values ​​to the properties of the instantiated Javabean objects, there are four forms.

      <jsp:setProperty name = "JavaBean instance" property = "*" /> (associated with the form)

      <jsp:setProperty name = "JavaBean instance name" property = "JavaBean property name" /> (associated with the form)

      <jsp:setProperty name="JavaBean instance name" property="JavaBean property name" value = "BeanValue" /> (manually set)

      <jsp:setProperty name = "JavaBean instance name" property = "propertyName" param = "parameter name in the request object" /> (associated with the request parameter)

       ③<jsp:getProperty>

       Role: Get the property value of the specified Javabean object.

      <jsp:getProperty name = "JavaBean instance name" property = "Property name" />

 

 3. Four scopes of Javabean:

  Description: Use the scope attribute of useBeans to specify the scope of the javabean.

  ①page //Only valid on the current page

  ②request //The JavaB object can be obtained through the HttpRequest.getAttribute() method.

  ③session //The JavaBean object can be obtained through the HttpSession.getAttribute() method.

  ④application //The JavaBean object can be obtained through the application.getAttribute() method.

 

4. Introduction to Model1:

  ① Before the Model1 model appeared, the entire Web application: almost all consisted of JSP pages. The JSP pages received and processed client requests, and responded directly to the requests after they were processed.

  ② Disadvantages: The interface layer is filled with a large number of business logic codes and data access layer codes, and the scalability and maintainability of Web programs are very poor.

  ③The appearance of JavaBean can make the data encapsulated by Javabean in the jsp page or call the business logic code of Javabean, which greatly improves the maintainability of the program.

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325260699&siteId=291194637