List of JSP notes and cases (the second half of JSP)

(一)JavaBean

1. Features of Javabeande:

1. This Java class must have a no-argument constructor

2. Properties must be privatized

3. Private properties must be exposed to other programs through methods of public type, and the method is named

Certain naming conventions must also be followed.

Function: It is usually used to encapsulate data. For JavaBean components that follow the above writing method, other programs can pass

The reflection technology instantiates the JavaBean object, and by reflecting the methods that comply with the naming convention, it learns the properties of the JavaBean, and then calls other properties to save the data.

Second, the properties of JavaBean:

1. The properties of a JavaBean can be of any type, and a JavaBean can have multiple properties. Each property usually needs to have a corresponding setter and getter method, the setter method becomes the property modifier, and the getter method becomes the property accessor.

 

2. The attribute modifier must start with a lowercase set prefix, followed by the attribute name, and the first letter of the attribute name should be changed to uppercase,

For example, the modifier name of the name attribute is getName, and the modifier name of the password attribute is getPassword.

3. Property accessors usually start with a lowercase get prefix, followed by the property name, and the first letter of the property name should also be changed to uppercase,

For example, the accessor name of the name attribute is getName, and the accessor name of the password attribute is getPassword.

 

Add small knowledge points: modifiers and accessors:

Modifier: Modifier means a program that can modify a program or file. The English translation of the modifier is called: "Trainer" or "Hack" Generally speaking, trainer is often used to refer to " cheating device ", such as "xx game xx item attribute modifier", which is targeted and can only be used for a certain game or a version of this game. Modifier is a tool whose main function is to find the required memory address through technical means, and then modify it.

The purpose of the modifier is to find the address and then modify it.

The general process is: search for a certain value for the first time, modify the value in the game, then search for the new value, and so on, until you find the address and change it to the value you want.

If you don't know the specific number, you need "fuzzy search": first search for the desired address, then modify the number in the game, then search for the number you want to increase or decrease or remain unchanged, and then change the value in the game, and so on. , find the address.

If a certain value is small, such as an integer below 10, you will find a lot, you can use the combination number to search, such as hp is 100, mp is 150, just search 100, 150. But the value type must be selected correctly. If you don't know, just search the type once (select "??")

After finding the value, there is often related data nearby, which can be modified by the way. For example, the address in front of the item number is often the item type, you can change the best item out of thin air, and the experience is often hp, mp, str, agi, int, luk, etc.

Principles for judging the quality of modifiers: 1. Can find the address 2. Fast speed 3. Rich functions

Accessor: When declaring a class, member variables are usually declared private to prevent malicious operations caused by direct access to member variables. However, it's not that access is disallowed, it's indirectly accessible through the public interface. The so-called public interface means that the programmer defines the public methods related to each private member variable in the class to improve the security level. Traditionally, member variables with private access rights are called attributes, and the corresponding public methods are called accessors. Accessors are divided into read accessors (getters) and write accessors (setters) according to their functions.

4. A certain property of a JavaBean can also have only set method or get method. Such properties are usually called write-only and read-only properties.

Three: Using JavaBeans in JSP

The three major JavaBean tags are JSP tags:

1. <jsp:useBean> tag: used to find or instantiate a JavaBean component in a JSP page.

Can get a bean very quickly

Small addition: <%= %> output function;

2. <jsp:setProperty> tag: used to set the properties of a JavaBean component in the JSP page.

Set properties for the created javab tag.

3.<jsp:getProperty>标签:用于在JSP页面中获取一个JavaBean组件的属性。

来获取属性。代替输出函数来获取值(属性)

JavaBean的代码如下:

 

<%@page import="day09_day010.Preson"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>JSP标签setproperty</title>

 

</head>

<body>

<jsp:useBean id="person" class="day09_day010.Preson"> </jsp:useBean>
<jsp:setProperty property="name" name="person" value="xxxxx"/>
<%=person.getName() %><br/>
<!-- 用请求参数给Bean复制 -->
<jsp:setProperty property="name" name="person" param="name"/>
<%=person.getName() %><br/>
<jsp:setProperty property="age" name="person" param="age" />
<!-- 用请求参数支持八中基本数据类型的转换 (把客户机提交的数据)-->
<%=person.getAge() %><br/>

 

<jsp:setProperty property="date" name="person" value="<%=new Date() %>"/>
<%=person.getDate() %>
<br/>----------------------------------------------<br/>
<!-- 用所有的请求参数为Bean赋值 -->
<jsp:setProperty property="*" name="person"/>

 

<%=person.getAge() %><br/>
<%=person.getName() %>
-------------------------------<br/>
<jsp:getProperty property="name" name="person"/>
<jsp:getProperty property="age" name="person"/>
<jsp:getProperty property="date" name="person"/>

 


</body>
</html>

 

Guess you like

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