JSP EL expressions using the syntax and the implicit objects

concept

EL expression: replace and easier to write java jsp page code
jsp page tacit support EL expressions

Let jsp page does not resolve the EL expression displayed as:
isELIgnored = "to true" ignore all the pages of EL expressions
or
the $ before adding an escape character \, which represents ignored after the symbol \ single EL expression
cases:\${4>3}

Syntax: $ {expression}

use

1, computing

✧, arithmetic operators
+
-
*
/或div
%或mod

Example:

${1+2}
${4 div 2}
${4/3}
${3%4}
${3 mod 4}
✧, comparison operators
>
<
>=
<=
==
!=

Example:

${2 == 2}
✧, logical operators
&&或and 
||或or
!或not

Example:

${1>2 && 2>1}
${1>2 and 2>1}
✧, air transport operators

empty
if the object is determined for a set of strings of array objects "" or null or length is 0

Example:

${empty s1}
${empty s2}
${empty s3}

Invert:! Before adding empty or not
a string array object collection object is not "" or null and the length is greater than 0


2, get the value

EL expression can only be obtained from the value of the object field

✧, to obtain a corresponding key-value:
Use way: $ {domain name key name.}

Gets the specified key from the specified field value
if the acquisition is less than the corresponding value is displayed empty string ( "") and does not display null and
therefore does not complain and will not disrupt the page layout displays
the domain name
pageScope: from pageContext domain get the value
requestScope: Get values from field Request
sessionScope: Get values from field Session
applicationScope: Get values from the ServiceContext / application domain

Example:

<%
request.setAttribute("name","陈涛");
session.setAttribute("age","18");
%>

<%--获取数据--%>
${requestScope.get("name")}
${sessionScope.get("age")}
Use way: Key $ {name}

The domain name is omitted from the representation in order to find the smallest domain name if there is a value corresponding to the key until you find the date

Automatic start page inside to find
no words to request to find
another session did not go there to find
no full application inside look


✧, get the object's value
Use:
${域名称.键名称.属性名}

or

${键名称.属性名}

To get through the properties of the object

Property Name Specification: set or get method set or get canceled and then the remaining string first letter lowercase
because the method name lookup is not the member variable name

Find whether there is a specified key name in the specified field / variable domain if the get method corresponding attribute name is called to obtain the data
if the attribute name (in fact, get the name of the corresponding method attribute) is not present will be the reported anomaly 500

Separate acquisition section Date:

${域名称.键名称.属性名.日期的部分}

or

${键名称.属性名.日期的部分}

Parts of the date: it refers to the year month day or the like has a corresponding the getYear () attribute getMonth () getDay () method
if the portion of the date (in fact, a portion corresponding date get method) filling does not exist is 500 will be reported abnormal

The default date format is displayed in English
can be translated into Chinese date format
simply add the key names in the corresponding class in a get method in this method previously transformed date
after use ${域名称.键名称.属性名}or ${键名称.属性名}to order a lowercase first letter of the name as a method to get the property name acquired after the date of conversion

Example:

public String getBirthdayStr()
{
	if (birthday!=null)
	{
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simpleDateFormat.format(birthday);
	}
	else
	{
		return "";
	}
}

This method is the logical view
logical view: the method does not correspond to a member variable method simply displays the corresponding page in the format defined


✧, get the value of the collection
①, get the value of the collection List

Get a common data type

${ 域名称.键名称[索引下标] }

or

${ 键名称[索引下标] }

Example:

<%
	List list=new ArrayList();
	list.add("AAA");
	list.add("BBB");
	request.setAttribute("list",list);
%>

${requestScope.list}
${requestScope.list[0]}
${requestScope.list[1]}

Get Object

${ 域名称.键名称[索引下标].属性名 }

or

${ 键名称[索引下标].属性名 }

Example:

<%
        User user=new User();
        user.setName("张涛");
        user.setAge(18);
        user.setBirthday(new Date());
        List list=new ArrayList();
        list.add(user);
%>

${requestScope.list[0].name}

And does not complain when lower bounds
EL expression made internal optimization will display an empty string

②, get the value of the collection Map

Get a common data type

${ 域名称.键名称.Map集合中的键名称 }
${ 域名称.键名称[ "Map集合中的键名称" ] }

or

${ 键名称.Map集合中的键名称 }
${ 键名称[ "Map集合中的键名称" ] }

Example:

<%
       Map map=new HashMap();
        map.put("username","王涛");
        map.put("gender","男");
        request.setAttribute("map",map);
%>

${requestScope.map.username}
${requestScope.map["username"]}

Similarly Get Object


Implicit Objects

pageContext: get jsp eight other objects

  • pageContext.request
    dynamic access to the virtual directory ContextPath jsp pages
    dynamically obtain : the directory is not easy to post-maintenance-coded
    syntax: pageContext.request.contextPath
    acquisition is the path to the tomcat configuration
    if the path is "/" then get to the empty string
    Here Insert Picture Description
  • pageContext.session
  • pageContext.servletContext
  • pageContext.response
  • pageContext.page
  • pageContext.out
  • pageContext.exception

Scope related objects

The built-in objects like jsp

  • pageScope
  • requestScope
  • sessionScope
  • applicationScope

Related objects request header

  • header
  • headerValues

Request parameters related objects

  • param
  • paramValues

cookie

Global initialization parameters

  • Initfrm

Published 174 original articles · won praise 5 · Views 240,000 +

Guess you like

Origin blog.csdn.net/Piconjo/article/details/104917464