javaweb -- domain object & EL expression & JSTL Library

1, Four domain objects

Page context: it can only work on the current page, and can not be used for data sharing of forwarding or redirection.
Request domain: it can only be used for the data sharing of the same request, so it can only be used in the forwarding of requests.
session domain: it can only be used to share data in one conversation (one conversation: users open the browser, browse multiple web sites, and then close the browser), forwarding and redirection can be used
context domain (application): can only be used in the same web application. (global)

2, EL expression

EL is a JSP expression language, whose full name is ExpressionLanguage. The purpose of using EL is to simplify the way of accessing variables in JSP and the coupling of simple static HTML and Java code

Basic syntax format
        ${ EL Expression}

1. example code

  ${ "Helloworld" }   // Output string constant
        ${  str  }           // Output the value of the string variable str
        ${ 3 + 2 }           // Output the result of 3 + 2
        ${}          // Output the name attribute of the user object
        ${user["name"] }    // ditto
        ${  sessionScope ["user"].name }  // ditto
        ${}        //Access the getName () method of the user object to get the value of the name member.
        ${list[1]}            //Access the second item of the list object.
        ${map["key"]}         //Access the value of the key specified by the map.

  2.  "." Similarities and differences with "[]"
(1) similarities: all accessible objects have attributes.
(2) differences: when the name of the attribute contains complex symbols such as spaces and dots. Use "." To access the properties of an object, an exception will occur
3. Operators in El expressions
(1) arithmetic operators (+,, *, /,%)
(2) logical operators (& &, ||,! Or and,or,not)
(3) comparison operators (>, > =, <, < =, = =,! = =) can automatically convert data types
(4) empty operator (empty) returns true when the value is null
(a) the arithmetic operators of EL are roughly the same as those in Java and have the same priority.
(b) the '+' operator will not connect strings. It is only used for addition.
      4.EL relational operators have the following six operators
Relational operator description example result
= = or eq | equal to ${5 = = 5} or ${5 eq 5} | true
            != Or ne | is not equal to ${5! = 5} or ${5 ne 5} | false
< or lt | less than ${3 < 5} or ${3 lt 5} | true
> or gt | greater than ${3 > 5} or ${3 gt 5} | false
< = or le | less than or equal to ${3 < = 5} or ${3 le 5} | true
> = or ge | greater than or equal to ${3 > = 5} or ${3 ge 5} | false

3, JSTL Library

A set of standard tags provided by JSP
-- general label: set out remove
-- condition tag: if # if else
-- iteration label: forEach # for loop
How to use JSTL Library
Import ----- taglib instruction

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    <!-- use taglib Instruction introduction JSTL library -->
    <!-- prefix="" Commands for label Libraries -->
    <!-- uri="" Files imported into label Library -->
    <%@ taglib prefix="c" uri="" %>
			1.from JSP A set of label libraries provided internally
			Question: pure JSP Not allowed in page Java Code needs to be used JSTL Provide labels to replace.
			3.Use steps:
				(1)Import dependent Libraries     lib
				(2)In the specified page taglib Instruction to introduce specific tag library dependency files
	 	//Save a value through a JSP script
	 	session.setAttribute("username", "admin");
	 <%=session.getAttribute("username") %>
	 ${sessionScope.username }
	 <!-- adopt JSTL In Library out Output tag value -->
	 <!-- default When value The default result is output when the result of value access does not exist  -->
	 <c:out value="${username }" default="sb"></c:out>
	 <!-- adopt JSTL In Library set Label to set the value -->
	 		var  Stored variable name
	 		value Stored value
	 		scope Scope( pageContext request session application)
	 <c:set var="uname" value="Breezy" scope="session"></c:set>
	 ${uname }
	 <c:out value="${uname }"></c:out>
	 <!-- adopt JSTL Provided in the library if Tag to realize effective judgment of data -->
	 <c:if test="${1==12 }">
	 	<c:out value="Your condition is OK of"></c:out>
	 <!-- adopt JSTL In Library if Tag implements a login case -->
	 	Logged in----Displays the current login name
	 	Not logged in ----Show form for login
	 	String username = request.getParameter("username");
	 		Student student = new Student(username,"male");
	 		session.setAttribute("student", student);
	 <c:if test="${empty student }">
	 	<form action = "demo4.jsp" method = "post">
	 		account <input type = "text" name = "username"/>
	 		password <input type = "password" name = "password"/>
	 		<input type = "submit" value = "Sign in"/>
	 <c:if test="${not empty student }">
	 	<h4>Welcome back! ${student.username }</h4>
	<!-- remove Label removal -->
	<c:set var="bb" value="123456"></c:set>
	Before removal: <c:out value="${bb }"></c:out>
	<!-- remove -->
	<c:remove var="bb" />
	After removal:<c:out value="${bb }"></c:out>
	<!-- foreach Circular label -->
		sb  variable
		begin Starting value
		end  Final value
		step 2 per increase
		varStatus Get the line number corresponding to the data( Y1 stage J2EE  custom JSP (label)
	<c:forEach  var="sb" begin="1" end="100" step="2" varStatus="demo" >
		${sb }----${demo.index }----${demo.count } <br/>
		List<Student> myList = new ArrayList<Student>();
		myList.add(new Student("Zhang san1","male"));
		myList.add(new Student("Zhang San 2","male"));
		myList.add(new Student("Zhang SAN3","male"));
		myList.add(new Student("Zhang San 4","male"));
		myList.add(new Student("Zhang San 5","male"));
		session.setAttribute("myList", myList);
	<c:forEach items="${myList }" var="stu">
		${stu.username }   ${ }

Tags: Web Development

Posted by shutat on Sat, 16 Apr 2022 20:12:33 +0930