Study notes 6: JSP and JSTL (in)

EL expression

Before use, add a header file <%@ page isELIgnored="false" %> to enable EL expressions. The high version of Web.XML defaults to false which means it is enabled, and the low version defaults to true to be manually added.

EL expression syntax

EL expression is to make it easier to write JSP. It provides a way to simplify expressions in JSP, making JSP code more simplified.

语法结构非常简单:${表达式}

EL expressions operate on data in domain objects, and cannot operate on local variables.

When you need to specify to find data from a specific domain object, you can use the corresponding spatial objects corresponding to the four domain objects, which are pageScope, requestScope, sessionScope, and applicationScope.

${pageScope.属性名}
${requestScope.属性名}
${sessionScope.属性名}
${applicationScope.属性名}

If you do not specify a domain object, the default search method of EL is to search from small to large, until it is found. When the domain object is searched and not found yet, an empty string "" is returned.

Get project path through EL expression

​ Usually through $(pageContext.request.contextPath) to dynamically get the project name path similar to request.getContextPath();

Get List collection

<%
    数据源
    List<String> list = new ArrayList<String>();
    list.add("aaa");
    list.add("bbb");
    request.setAttribute("list",list);
%>
<%--
    用EL表达式获取list中指定下标的数据
    
    ${
    
    list[下标]}

    获取集合的长度
    ${
    
    list.size()}

    注意:这里list代表的是域对象中的变量名,也就是集合名
--%>

Get Map

<%
	数据源
	
    HashMap<String, String> map = new HashMap<>();
    map.put("name","张三");
    map.put("name2","李四");
    request.setAttribute("map",map);
%>
<%--
    通过EL表达式获取Map中指定值
    
    ${
    
    map["key"]}  或 ${
    
    map.key}
    
    注:这里map代表的是域对象中的变量名
--%>

Get javaBean object

The attribute field in javaBean must have a get method to get it! , Note: When obtaining the value, use the domain object directly. The attribute name is enough, and there is no need to call the get method.

	获取对象的方式
	${
    
    对象名} 
	获取对象中的属性
	${
    
    对象名.属性名}

Determine whether the domain object is empty

判断对象为空吗
${
    
    empty 域对象名}
为空则返回true;
不为空则返回false;
如果为String类型的域对象,那么空字符串也返回true
如果为集合类型的域对象,那么长度小于1,也就是没有长度的集合也返回true

Guess you like

Origin blog.csdn.net/qq_40492885/article/details/115289515