web development (6) EL expressions

I saw a good article on the Internet, written in detail.

The following is a reference to that blog post. Reprinted in "http://www.cnblogs.com/whgk/p/6432044.html", it is here for reference only.

 

1. EL expression

    EL expressions can be used in JSP. EL expressions are scripts enclosed in "${}", which are used to read objects more easily. EL expressions are written in the HTML code of JSP, not in "<% %>", now let's see how EL expressions replace JSP scripts or JSP behaviors.

    EL expressions have 11 built-in objects.

       pageScope、requestScope、sessionScope、applicationScope、pageContext、param、paramValues、header、headerValues、cookie、initParam

    pageScope, requestScope, sessionScope, and applicationScope represent four scope objects

    pageContext represents the built-in object pageContext in JSP, which can obtain eight built-in objects of other JSPs such as request  

    param represents a request parameter ${param.username} is equivalent to request.getParameter("username");

    paramValues ​​represents a set of request parameters ${paramValues.loves} is equivalent to request.getParameterValues("loves"); submit a favorite checkbox    

    header represents a request header ${header.referer} is equivalent to request.getHeader("referer");

    headerValues ​​represents a set of request headers ${header.cookie} is equivalent to request.getHeaders("cookie"); the content in the obtained request header parameter is a set of content, for example, a cookie can be passed together by multiple cookies

    cookie get the cookie object

    initPatam web project initialization parameters, servletContext.getInitParameter("xxx");

    

    There is also a special usage that directly obtains object variables,

      ${user.username}

       User is an instance object of User and is stored in the page scope. The above code means that the user object is searched from the page, request, session, and application scopes in turn, until it is found. The bottom layer uses pageContext.findAttribute (); is the same effect.

 

    Example explanation:

        

      

    Summarize:

      EL expressions are relatively simple. The purpose of using them is to reduce JSP scripts. Try not to embed Java code in HTML, which is very confusing. Using EL expressions in HTML is better to obtain various objects in JSP. Get the value in the four major scopes,

      If you want to get the data in the four major scopes, you can use ${}, ${pageScope.xxx} 

      If you want to get request parameters, use param or paramValues

      If you want to get some information in the request header, get some objects of the Servlet, such as request, session, etc., you can use pageContext to get the request object, and then get the required information, or use the header object to get the header directly information

      If you want to get web initialization parameters, use initPatam

 

      The operation of the el expression will not be explained, it is very simple, and basically everything that can be used can be used.

  

      The main thing is to remember which 11 built-in objects are in EL, and knowing them you know what information EL can get.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325125843&siteId=291194637