Mu class network "JAVA met HTML - JSP Chapter V JavaBeans" study notes

Mu class network course site: https://www.imooc.com/learn/166 .

5-1 Chapter Introduction

(1) Javabean profile (2) Javabean design principles (3) Jsp how to use Javabean (4) <jsp: useBeans> (5) <jsp: getProperty> (6) <jsp: setProperty> (7) Javabean four scope range (8) Model1 Introduction (9) projects

5-2 JavaBean Introduction and Design Principles

JavaBeans Java classes that meet certain specific specification. The benefits of using JavaBeans is to solve write code duplication, reduce code redundancy, functional distinction clear, improve maintenance of the code.

JavaBeans design principles: (1) public class, (2) public constructor with no arguments, (3) private property, (4) getter and setter methods.

example:

// class design students

public class Students{

  private String name;

  private int age;

  public Students(){ }

  public void setName( String name ) { this.name = name; }

  public String getName() { return this.name; }

  public void setAge( int age ) { this.age = age; }

  public int getAge() { return this.age; }

}

 

5-4 Using common way to create JavaBean

How to use Jsp page JavaBeans:

1, like a normal Java class, create a JavaBean instance.

 

 Right-click in eclipse, source / Generate Getters and Setters.

 

 

 

 5-5 useBean action elements

2, is generally used in Jsp page jsp action tag using JavaBean

(1) useBeans operation (2) setProperty operation (3) getProperty operation

<jsp:useBeans>

Action: instantiating jsp pages or JavaBean used within the specified range:

<Jsp: useBean id = "prompt," class = "java class name" scope = "scope" />

<Jsp: Jsp action that it is a label. Write action after his name useBean, indicating that it is a useBean tag. The class should be the full name (package name plus the class name) java class.

  

 

 

 

 

 

 It is null because there is no assignment to object these two properties.

 5-6 setProperty

<jsp:setProperty>

Role: to Javabean property has been instantiated object assignment, a total of four forms.

<Jsp: setProperty name = "JavaBean instance name" property = "*" /> (associated with Form)

<Jsp: setProperty name = "JavaBean instance name" property = "JavaBean attribute name" /> (associated with Form)

<Jsp: setProperty name = "JavaBean instance name" property = "JavaBean attribute name" value = "BeanValue" /> (manual setting)

<Jsp: setProperty name = "JavaBean instance name" property = "propertyName" param = "parameter name request object" /> (more parameters associated request)

Examples of exercises:

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

 

 

 (2) <jsp: setProperty name = "JavaBean instance name" property = "JavaBean attribute name" /> (associated with Form)

 A partial match.

 

 

 

 

 

 

 

 

 (3) <jsp: setProperty name = "JavaBean instance name" property = "JavaBean attribute name" value = "BeanValue" /> (manual setting)

This has nothing to do with the form.

 

 

 

 

 

 

 

 

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

 

 

 

 

 

 

 

 

 Still with zhangsan, 123456 login.

 

 

 Forms can be reused on the way to set up a user name:

 

 

 

 

 

 5-7 getProperty

Action Tags <jsp: getProperty>

Role: Get the property value of the specified JavaBean objects.

<Jsp: getProperty name = "JavaBean instance name" property = "attribute name" />

 

 

 

 

 

 

 

 

5-8 JavaBean four of scope

Note: Use useBeans scope attribute can be used to specify the scope of JavaBean.

page // valid only in the current page.

request // JavaBean objects can be achieved by HttpRequest.getAttribute () method.

session // JavaBean objects can be achieved by HttpSession.getAttribute () method.

appalication // can () method to obtain JavaBean objects through application.getAttribute.

(1) the range of application object

 

 

 

 

 

 

 

 

 

 

Or use built-in objects JSP application:

 

 

 

 

 

 

(2) the range of the session object

 

 

 

 

 

 

 

 

Range (3) of the request object:

 

 

 

 

 

 

 

 

The reason: doLogin.jsp in the <a href="testScope.jsp"> </a> hyperlink, the request is redirected, the equivalent of a new request, so testScope.jsp in the request object where there is no myUsers objects a.

Solution: We look forward with a request:

 

 

 

 

 

 

 

 

 The request is forwarded directly jump to testScope page, the address bar The address has not changed. The same request, myUsers in the object request still.

Therefore, the scope of the request object, JavaBean works.

(4) the range of target page:

 

 

 

 

 

 

 The change:

 

 

 

 

 

 

 page range is the smallest, only valid in the current page. In testScope.jsp in, page objects, there is no "myUsers" of the object named.

5-10 Model1 Profile

Development JavaWeb applications can be divided into Model1 and Model2.

Model1 former model appeared, the entire Web application: composed almost entirely of JSP page, JSP page receives a client request, the request for direct treatment accordingly.

Drawbacks: the interface layer (that is, our JSP pages) filled with a lot of the code business logic code and data access layers, scalability and maintainability of Web applications very poor.

JavaBean specification may make use of data JavaBean JSP page or call the package JavaBean business logic code, thus greatly enhance the maintainability.

 

 Model1 Simply put, JSP + JavaBeans development model.

 As shown, hierarchical thinking, this is a simple three-tier structure. The interface layer is our JSP pages, business logic is our JavaBeans, the third layer is our database layer (JavaBeans with access to our database).

5-12 stages of the project

Use JSP + JavaBean complete user login functionality.

(1) according to the hierarchical idea com.po package build, built Users class; com.dao package construction, built UsersDAO class.

 

 

 

 

 

 (2) Review doLogin.jsp:

 

 The figure is before the project doLogin.jsp:

 

 effect:

 

 If the wrong password, 123456:

 

 If the username and password are to:

 

 At this point, the end of Chapter 5.

 

 

Guess you like

Origin www.cnblogs.com/pingfanliliang/p/11795442.html