JSTL and EL expressions

2010-03-27  来源:本站原创  分类:Java  人气:370 

Session of the values obtained

<c:out value="${sessionScope.user.userId}"> </ c: out> <br>

<c:out value="${user.userLoginName}"> </ c: out> <br>

<s:property value="#session.user.userId"/> <br>

$ (Session.user.userId) <br>

$ (SessionScope.user.userId) <br>

Basic syntax

1, EL Introduction
1. Grammatical structure
$ (Expression)
2. [] And. Operator
EL provide. And [] of two operators to access the data.
When you want to access the property name contains special characters, such as. Or? Such as letters or numbers are not symbols, they have to use []. For example:
$ (User.My-Name) should be replaced by $ (user ["My-Name"])
If you want to when the dynamic value, you can use [] to do, but. Can not be dynamic values. For example:
$ (SessionScope.user [data]) is a variable in the data
3. Variable
EL access the variable data is simple, for example: $ (username). It means that out of a range variable named username.
Because we did not specify which of a range of username, so it will sequence from the Page, Request, Session, Application range search.
If the way to find the username, the direct return, not to look down, but if all of the range are found, would return null.
Properties range in the name of the EL
Page PageScope
Request RequestScope
Session SessionScope
Application ApplicationScope

2, EL implicit objects
1. And scope of the hidden object and scope of the EL implicit object contains the following four: pageScope, requestScope, sessionScope and applicationScope;
They are basically on and JSP's pageContext, request, session and application the same;
In the EL in the four implicit object attribute values can only be used to get range, that getAttribute (String name), they can not get other related information.

For example: we are to make session username stored in the value of a property, you can use the following methods:
session.getAttribute ("username") to obtain the value of username,
Used in the EL in the following way
$ (SessionScope.username)

2. And enter the hidden object and enter the hidden object has two: param and paramValues, they are the more special EL implicit object.

For example, we have to get the user's request parameters, you can use the following methods:
request.getParameter (String name)
request.getParameterValues (String name)
You can use in the EL in both param and paramValues to obtain the data.
$ (Param.name)
$ (ParamValues.name)

3. Other implicit objects

JSTL and set the cookie does not provide the action,
Example: To obtain the cookie has a name for userCountry value set, you can use $ (cookie.userCountry) to get it.

header and headerValues
header store the user's browser and server to communicate with the data Example: To get the user's browser version, you can use $ (header ["User-Agent"]).
Also in very few opportunities, there may be the same header name has a different value, this time must be used instead headerValues to get these values.

initParam web site to obtain environmental parameters set (Context)
Example: a general method of String userid = (String) application.getInitParameter ("userid");
You can use $ (initParam.userid) to get the name for the userid

pageContext to obtain user requirements or other relevant page for more information.
$ (PageContext.request.queryString) parameter string to obtain the requested
$ (PageContext.request.requestURL) to obtain the requested URL, but does not include the request parameters string
$ (PageContext.request.contextPath) The name of the web application services
$ (PageContext.request.method) HTTP access method (GET, POST)
$ (PageContext.request.protocol) made use of the protocol (HTTP/1.1, HTTP/1.0)
$ (PageContext.request.remoteUser) to obtain the user name
$ (PageContext.request.remoteAddr) to obtain the user's IP address
$ (PageContext.session.new) determine whether the new session
$ (PageContext.session.id) to obtain the ID session
$ (PageContext.servletContext.serverInfo) to obtain information on the host side of the service

3, EL operator
1. Arithmetic operators have five :+,-,* or $, / or div,% or mod
2. There are six relational operators: == or eq,! = Or ne, <or lt,> or gt, <= or le,> = or ge
3. There are three logical operators: & & or and, | | or or,! Or not
4. Other operators have three: Empty operator, the conditional operator, () operator cases: $ (empty param.name), $ (A? B: C), $ (A * (B + C))

4, EL functions (functions).
Syntax: ns: function (arg1, arg2, arg3 .... ArgN)
Ns for the front of which the name (prefix), it must be the name of a taglib directive home front



<% @ Taglib prefix = "c" uri = "http://java.sun.com/jstl/core_rt"%>


<C: forEach items = "$ (messages)"
var = "item"
begin = "0"
end = "9"
step = "1"
varStatus = "var">
... ...
</ C: forEach>


<c:out value="${logininfo.username}"/>
c: out> the value of the contents of the output to the current location, where the object is to logininfo
username property value output to the page here.
$ (... ...) Is JSP2.0 the Expression Language (EL) syntax. It defines an expression,
Which can be a constant expression (as above), it can be a concrete expression of statements (such as forEach loop in the case). Typical cases are as follows:
? $ (Logininfo.username)
This indicates that the object reference logininfo username property. We can "." Operator property of the object reference, you can also use the "[]" reference object properties, such as $ (logininfo [username])
And $ (logininfo.username) achieved the same effect.
"[]" References the way, is that if the property name appears in the special characters, such as "." Or "-"
At this point we must use the "[]" syntax for property values in order to avoid a conflict (the system should try to avoid the development of this phenomenon).
With equivalent JSP Script as follows:
LoginInfo logininfo =
(LoginInfo) session.getAttribute ("logininfo");
String username = logininfo.getUsername ();
Can be seen, EL significant savings in coding capacity.
Leads to another question here is, EL logininfo from where to find the object, for
$ (Logininfo.username) this expression, the first page of search from the current definition of a variable before it logininfo, if not found then turn to the Request, Session,
Application range of search, but until then. If still not found until the last match of the variables, it returns null.
If we need to specify the variable radius can be specified in the EL expression search:
$ (PageScope.logininfo.username)
$ (RequestScope.logininfo.username)
$ (SessionScope.logininfo.username)
$ (ApplicationScope.logininfo.username)
In the Spring, all the logic processing unit returned results data will be placed as an Attribute to the HttpServletRequest object returns (realization can be found in the Spring source
exposeModelAsRequestAttributes method implementation code), that is Spring
MVC, the results of data objects by default are requestScope. Therefore, in the Spring MVC in
Addressing the following method should be used with caution:
$ (SessionScope.logininfo.username)
$ (ApplicationScope.logininfo.username)
? $ (1 +2)
The results for the expression results, the integer value 3.
? $ (I> 1)
If the variable value i> 1, then will return bool type true. Compared with the previous cases, we find that EL will be automatically calculated according to the expression to return different data types.
Java code written expression and written expression in much the same way.


<c:if test="${var.index% 2 == 0}">
</ C: if>
Determine the general conditions for a EL expression.
<c:if> else clause does not provide, for use when there may be some inconvenience, then we can <c:choose>
tag to achieve a similar purpose:
<c:when test="${var.index% 2 == 0}">
</ C: when>
</ C: otherwise>
</ C: choose>
Similar to Java's switch statement, <c:choose> determined to provide the complex under the conditions of the simplified approach. Clause similar to the case in which <c:when> clause, can occur many times. The above code, the output in the odd rows with "*",
The even rows output "!."


1 EL expression with $ () that can be used in all the HTML and JSP tags in JSP pages to replace the role of the complex JAVA code.

2 EL expressions operational variables and constants implicit object. The most commonly used implicit object has $ (param) and $ (paramValues). $ (Param) request parameters that returns a single string value. $ (ParamValues) said Return the request parameters of a set of values. pageScope indicate that the page scope variable. requestScope said the request object's variables. sessionScope that session variables within. applicationScope that the variable range of applications.

3 <% @ page isELIgnored = "true"%> that are disabled for EL language, TRUE is prohibited. FALSE said they were not prohibited. JSP2.0 EL is enabled in the default language.

4 EL language to display the logical expression such as $ (true and false) The result is false relational expression such as $ (5> 6) The result is false arithmetic expressions such as $ (5) The result is 10 +5

5 EL variables search is: page request session application point operator (.) And "[]" are said to obtain the variable value. Difference is that [] to display non-variable parts of speech

  • JSTL and EL expressions What is the relationship between 2010-12-07

    EL concepts JSTL with EL expressions are generally used together to achieve does not appear in the jsp java code. So let's learn EL expression EL mainly used to find the scope of the data, and then perform simple operations on them; it is not a progr

  • JSTL and EL expressions 2010-03-27

    Session of the values obtained <c:out value="${sessionScope.user.userId}"> </ c: out> <br> <c:out value="${user.userLoginName}"> </ c: out> <br> <s:property value="#session.user.userId"/

  • jstl does not support EL expressions solution 2010-08-03

    Today, old newspaper with <c:out> label does not support the expression of error, Mangliaobantian finally found the problem when building Dynamic web project item selected Dynamic web module version 2.5, the default jsp page is not allowed to use EL

  • EL expressions formatted date 2009-04-09

    In the EL expression to show "yyyy-MM-dd" format Date: Use formatting tags <fmt:> An import on the page <% @ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt"%> 2 Format Date <fmt:formatDate value=

  • EL expressions on the issue of failure 2009-12-07

    Today started a small system, used in EL, to find the previous project, copy over, actually found $ (xxx) failure The server is using tomcat6.0. google one afternoon, summing up the reasons for problems and solutions. First of all now JSP2.0 already

  • EL expressions in JSP question of invalidity 2010-04-28

    Determined using Servlet / JSP version, see the beginning of the web.xml, if there is the following, that is a Servlet 2.4 / JSP 2.0. 1. <Web-app version = "2.4" 2. Xmlns = "http://java.sun.com/xml/ns/j2ee" 3. Xmlns: xsi = "htt

  • (1) jstl test el expression 2010-06-10

    1. Look at the value jstl and general What is the difference? jstl $ () are used to value the way the. First build a JstlElServlet: package com.jxust.jstl; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.ser

  • EL expressions use (transfer) 2010-06-10

    EL expression is always enclosed with braces, and in front of a dollar sign ($) prefix: $ (expression). Representation of the first named variable or type an implicit object, either a scope (page scope, request scope, session scope or application sco

  • zk EL expressions and custom functions for use 2010-07-21

    Can use the function list will be listed last, first, we use an example to learn how to use the EL function with a custom EL function Here is an international example of the beginning of a paragraph in the code Code title = "$ (c: l ('zkway.title'))&

  • JSP EL expressions can not be used 2011-05-01

    Can not use the JSTL is a very tangled problem. Especially for the EL expressions are used to people who no doubt is a painful thing more eggs Entangled for a long time today, finally solved the problem. For the low version of MyEclipse to add EL exp

  • JSP EL expressions invalidity of the page 2010-05-04

    Previously done with the WSAD wizard, can resolve in the JSP page to the EL expression, of course, the premise is JSP2.0 circumstances. Today encountered a strange problem. Just download Eclipse3.3 + MyEclipse6.0 experience the process, they encounte

  • A basic introduction to JSTL and EL 2010-05-27

    Introduction JSTL and EL can not help but tell us about the standard JSP action element. JSP standard action elements: <jsp:useBean> elements using javabean <jsp:getProperty> a JavaBean component made from a property value, and its response to

  • Custom creation and use of EL expressions 2010-07-18

    EL expressions are often used in J2EE programming things, so today, according to books written specifically again, hope has been useful to others, above all, the definition of JAVA method Class name is public, the method is defined as public static p

  • JAVA EE5 (J2EE5) support for EL expressions 2010-12-27

    Let JAVA EE5 (J2EE5) support EL expressions collection JAVA EE5 default does not support EL expressions. One way: In the top of each jsp file add the following code: <% @ Page language = "java" import = "java.util .*" pageEncoding =

  • jstl el expressions to judge whether the collection is empty and the loop through output 2010-07-25

    <table width="100%" border="0" align="center"> <tr> <td width="3%" height="26"> <input type="checkbox" name="selectall"/> </ td> <td width="18%&quo

  • EL expressions use the 2009-12-30

    The WEN_INFO a folder under the new tlds, into which the fn.tld EL expression value to the space: The introduction of: <% @ taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "fn"%> To achieve $ (fn: trim (success))

  • JSP EL expressions in 2010-07-11

    1, JSP EL language definition EL (Expression Language) Objective: To make JSP easier to write. Expression language inspired by ECMAScript and the XPath expression language, which provides simplified expressions in the JSP in the method. It is a simpl

  • "Zhuanzhai> EL expressions error attribute items does not accept any expressions 2010-03-24

    There are 2 solutions 1, the <% @ taglib prefix = "c" uri = "http://java.sun.com/jstl/core"%> Change Java code <% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%> <% @ taglib prefix =

  • EL expressions - custom function 2010-03-28

    Expression language can be used in addition to the basic operators, but also can use a custom function. By using a custom function, increased expression language functions. EL expression function, the main function is to complete the modification of

  • EL expressions can not resolve the problem 2010-04-01

    Long time did not move the items, today launched a sudden there are analytical expressions can not, then check friends online information, do not trouble to find better friends, in the JSP page add the following sentence on the line: <% @ Page isELIg