EL&JSTL of Web core

Posted by aaronlzw_21 on Wed, 26 Feb 2020 14:18:14 +0100

primary coverage

1. JSP:
	1. instruction
	2. notes
	3. Built in objects

2. MVC development mode
 3. EL expression
 4. JSTL label
 5. Three tier architecture

JSP:

1. instruction
	*Function: used to configure JSP pages and import resource files
	* format:
		<% @ instruction name property name 1 = property value 1 property name 2 = property value 2...% >
	* classification:
		1. page: configure the
			*contentType: equivalent to response.setContentType()
				1. Set mime type and character set of response body
				2. Set the encoding of the current jsp page (only advanced IDE can take effect. If you use low-level tools, you need to set the pageEncoding property to set the character set of the current page)
			*Import: import package
			*errorPage: when an exception occurs to the current page, it will automatically jump to the specified error page
			*isErrorPage: identifies whether the current page is also an error page.
				*true: Yes, you can use the built-in object exception
				*false: No. Default value. Built in object exception is not allowed

		2. include: included in the page. Import the resource file of the page (usage: include can be used to import multiple resources of the same page)
			* <%@include file="top.jsp"%>
		3. taglib: import resources
			* <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
				*Prefix: prefix, custom
 2. note:
	1. html comment:
		<! ---- >: only html snippets can be annotated
	2. jsp comment: Recommended
		[% ---%]: all can be annotated

3. Built in object (written examination point)
	*Objects that do not need to be created and used directly in jsp pages
	*There are nine:
			Variable name real type action
		*PageContext pagecontext the current page shares data, and you can get eight other built-in objects
		*Request HttpServletRequest multiple resources (forwarding) accessed by one request
		*Session HttpSession multiple requests in a session
		*application ServletContext data sharing among all users
		*Response HttpServletResponse response object
		*Page Object the Object of the current page (Servlet)
		*out JspWriter output object, data output to page
		*config ServletConfig Servlet configuration object
		*Exception Throwable exception object
		
		The first four are four domain objects, ranging from small to large

MVC: development mode

1. jsp evolution history
	1. In the early days, there were only servlet s and only response could be used to output tag data, which was very troublesome
	2. Later, jsp simplifies the development of Servlet. If you overuse jsp, you can write a lot of java code in jsp, including html tables, which makes it difficult to maintain and work together
	3. Later, java's web development, drawing lessons from mvc development mode, makes the program design more reasonable

2. MVC: 
	1. M: Model. JavaBean
		*Complete specific business operations, such as querying databases and encapsulating objects
	2. V: View, View. JSP
		*Show data
	3. C: Controller. Servlet
		*Get user input
		*Call model
		*Give the data to the view for display

	*Advantages and disadvantages:
		1. advantages:
			1. Low coupling, easy to maintain, and conducive to division of labor and cooperation
			2. High reusability
		

		2. disadvantages:
			1. Make the project architecture complex and require high developers

EL expression

1. Concept: Expression Language
 2. Function: replace and simplify the writing of java code in jsp page
 3. Syntax: ${expression}
4. note:
	*jsp supports el expression by default. If you want to ignore el expressions
		1. Set: isELIgnored="true" in the page instruction of jsp to ignore all el expressions in the current jsp page
		2. \ ${expression}: ignore the current el expression

5. use:
	1. operations:
		*Operator:
			1. Arithmetic operator: + - * / (DIV)% (MOD)
			2. Comparison operator: >=
			3. Logical operator: & & (and) | (or)! (not)
			4. Air transport operator: empty
				*Function: used to judge whether the string, collection, array object is null or the length is 0
				*${empty list}: judge whether the string, collection, array object is null or the length is 0
				*${not empty str}: indicates whether the judgment string, collection, array object is not null and the length is > 0
	2. get value
		1. el expression can only get value from domain object
		2. syntax:
			1. ${domain name. Key name}: get the value of the specified key from the specified domain
				*Domain name:
					1. pageScope		--> pageContext
					2. requestScope 	--> request
					3. sessionScope 	--> session
					4. applicationScope --> application(ServletContext)
				*For example: name = Zhang San is stored in the request domain
				*Get: ${requestScope.name}

			2. ${key name}: indicates whether there is a value corresponding to the key from the smallest field until it is found.
			3. el expression. If there is no corresponding key value in the field, null will not be displayed, and an empty string '' will be returned.

			

			3. Get the value of object, List set and Map set
				1. Object: ${domain name. Key name. Attribute name} attribute name: remove get, initial lowercase (for example, getname - > name - > name)
					*In essence, it will call the getter method of the object

				2. List set: ${domain name. Key name [index]}

				3. Map set:
					*${domain name. key name. key name}
					*${domain name. Key name ["key name"]}

	3. Implicit object:
		*11 implicit objects in el expression
		* pageScope
		*Requestscope! = request object in JSP
		* sessionScope
		* applicationScope
		*pageContext = = pageContext built-in object in JSP
        	*Get eight other jsp built-in objects
				*${pageContext.request.contextPath}: get virtual directory dynamically

JSTL

1. Concept: JavaServer Pages Tag Library  JSP Standard label Library
	* It is from Apache Open source free jsp Label		<Label>

2. Role: for simplification and replacement jsp On the page java Code		

3. Use steps:
	1. Import jstl Relevant jar Package, imported jar The bag must be in the WEB-IF Directory, and the package name must be lib
	2. Import label Library: taglib Directive:  <%@ taglib %>
	3. Use labels

4. Frequently-used JSTL Label
	1. if:Amount to java Code if Sentence
		1. Properties:
            * test Required property, accept boolean Expression
                * If the expression is true,Display if Label body content, if false,The label body content is not displayed
                * Normally, test Property values will be combined el Use with expressions
   		 2. Be careful:
       		 * c:if No label else Situation, want else In case, you can define a c:if Label
	2. choose:Amount to java Code switch Sentence
		1. Use choose Label declaration         			Amount to switch statement
        2. Use when Tag to judge         			Amount to case
        3. Use otherwise Label makes other statements    	Amount to default

	3. foreach:Amount to java Code for Sentence
        1. Complete repeated operations instead of normal for
            for(int i = 0; i < 10; i ++){

            }
            * Properties:
                begin: Start value
                end: End value
                var: Temporary variable
                step: step
                varStatus:Cycle state object [understand]---Mainly used to traverse containers
                    index:Index of the element in the container, starting at 0---and step Of,ordinary for loop index Consistent with values
                    	step=1;index=0,1,2,3..
                    	step=2;index=0,2,4,8
                    count:Number of cycles, starting at 1
        2. Traversal container,Replacement enhancement for
            List<User> list;
            for(User user : list){

            }

            * Properties:
                items:Container object
                var:Temporary variables for elements in the container
                varStatus:Loop state object
                    index:Index of the element in the container, starting at 0
                    count:Number of cycles, starting at 1



5. Practice:
	* Demand: in request There is an entity in the domain User Object List Set. Need to use jstl+el take list Gather data to show jsp Table for page table in
	
<%@ page import="User.User" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.ArrayList" %>
<%--
  Created by IntelliJ IDEA.
  User: HuDeYu
  Date: 2020/2/26
  Time: 19:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>test</title>
</head>
<body>
<%
User user1=new User("zhangsan",22,"male",new Date());
User user2=new User("lisi",28,"male",new Date());
User user3=new User("wangwu",26,"male",new Date());
   ArrayList list = new ArrayList<User>();
   list.add(user1);
   list.add(user2);
   list.add(user3);

   request.setAttribute("list",list);
%>

<table border="1px" width="500px" align="center" cellspacing="0px" cellpadding="0px">
    <tr>
        <th>Full name</th>
        <th>Age</th>
        <th>Gender</th>
        <th>Date of birth</th>
    </tr>
    <c:forEach items="${list}" var="user" varStatus="s">
        <c:if test="${s.count%2==0}">
            <tr bgcolor="red">
                <td> ${user.name}</td>
                <td> ${user.age}</td>
                <td> ${user.gender}</td>
                <td> ${user.birStr}</td>

            </tr>
        </c:if>

        <c:if test="${s.count%2!=0}">
            <tr bgcolor="yellow">
                <td> ${user.name}</td>
                <td> ${user.age}</td>
                <td> ${user.gender}</td>
                <td> ${user.birStr}</td>
            </tr>
        </c:if>

    </c:forEach>

</table>

</body>
</html>

Three layer architecture: software design architecture

1. Interface layer (presentation layer): the user can see the interface. Users can interact with the server through the components on the interface
servlet/jsp
	Functions: 1. Accept the request data; 2. Call the service layer; 3. Submit the data to jsp for display
	Package: company name. Project name. web
 2. Business logic layer: it deals with business logic.
	service
	Function: complete some complex logic processing; call data access layer
	Package: company name. Project name. service
 3. Data access layer: operate data storage files.
	dao
	Function: access the database, and complete the processing of adding, deleting, modifying and checking
	Package: company name. Project name. dao

Case: user information list display

Case study:

1. Requirement: add, delete, modify and query user information
2. Design:
	1. Technology selection: Servlet+JSP+MySQL+JDBCTempleat+Duird+BeanUtilS+tomcat
	2. Database design:
		create database day17; -- Create database
		use day17; 			   -- Use database
		create table user(   -- Create table
			id int primary key auto_increment,
			name varchar(20) not null,
			gender varchar(5),
			age int,
			address varchar(32),
			qq	varchar(20),
			email varchar(50)
		);

3. Development:
	1. Environment building
		1. Create database environment
		2. Create project, import required jar package

	2. Code

4. test
5. Deployment operation and maintenance
Published 20 original articles, won praise 11, visited 389
Private letter follow

Topics: JSP Java Database Session