Summary of front-end and back-end technologies in Web development-EL expressions, JSTL, eval () functions, jstleval

Source: Internet
Author: User

Summary of front-end and back-end technologies in Web development-EL expressions, JSTL, eval () functions, jstleval

In our daily Web development, we often use EL expressions, JSTL labels, and eval () in JavaScript (). The following is a novel about technologies and functions.

Expression Language (EL) is the full name Expression Language. Since it is a development Language, it is used to write statements like Java and other languages. It is usually used on JSP pages, to obtain the attribute values of the built-in objects provided by JSP,

We usually use EL expressions. For example, a common requirement in development is to obtain the project root directory. If the project name value changes, when we specify a path, if the root directory of the project is represented by a variable

We need to modify our code, so we usually use a common expression $ {pageContext. request. contextPath}. The basic explanation of this expression is to get the request of pageContext, a built-in JSP object.

The value of the contextPath attribute of the object. Here we can take a small example to see how to use $ {pageContext. request. contextPath:

For example, our project structure is as follows:

-- Demo

-- Src

-- WebRoot

WEB-INF

-- Css

-- Js

-- Img

-- Font

-- Page

The core code is as follows:

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>

<! DOCTYPE html>
<Html>
<Head>

<Title> demo page </title>

</Head>

<Body>

</Body>

</Html>

 

If the project has been released to the server, you may need to change the project name to demonstration. If the above Code does not use the EL expression to obtain the project name, the above code may look like the following:

. If the project name demo is modified to demonstration, we use the demo. the jsp code must be modified as follows:

.

Therefore, the first intuitive advantage of using the EL expression language is to extract the changeable part from the page and encapsulate it as a variable, which facilitates code maintenance. Of course, in addition to this, we will

A large number of EL expressions are used, such as getting parameters and variables in the page domain, request domain, session domain, or application domain. We often write such EL forms on the page:

<Input class = "" id = "" name = "username" type = "text" value = "$ {username}"> input boxes such, so what is the meaning of value = "$ {username }?

What about it? Because the form is generally used to encapsulate the data submitted by the user, if the user input is incorrect, the form is submitted to the background program for processing, and the background program prompts that an error cannot be reported properly according to the business logic, for example, the user login form contains the above

<Input> in the input box, the user cannot log on normally if the user name is incorrect. The page should jump to the login page and prompt the corresponding error. If the user name is too long and the input is not very convenient, we want

In case of name echo, $ {username} completes this function. It will start from the page field mentioned above to find whether there is a variable username, and get the variable value through $ {}.

Domain search... if not, query from the session domain. If not, query from the application domain. If yes, output the value. If not, returns ''(or null ). therefore, <input>

There is no value for the first time. If the user inputs an incorrect value, the incorrect value will be displayed when the page jumps back.

Therefore, the second use of EL expressions is usually used for data echo. Here, by the way, EL is used to display page data. Because EL is written in Java at the underlying layer, EL can be easily used to obtain

Java-type data. However, when the jsp page is finally translated to html through the server, only the string-type data is known. Then, EL is used to automatically convert the basic data type, for example, int type data and Date type data

Data. This is incomparable to JavaScript, because JavaScript cannot directly parse the Java type data passed in the background.

So is there a place where EL is often used in development? Obviously, there are still some, but they like to bring their friend JSTL together. They usually work together to complete some work. For example, we need to determine whether a user is logged on.

If you log on, the corresponding operation menu is displayed. We usually use this method:

<C: if test = "$ {user! = Null} ">

<Ul class = "nav">

<Li> <a href = "#"> personal homepage </a> </li>

<Li> <a href = "#"> cancel </a> </li>

</Ul>

</C: if>

Therefore, we can see that JSTL is a bit like a tag. Yes, JSTL is the standard tag library of JSP. The bottom layer of the tag is actually written in Java. It is usually associated with logical judgment, its judgment conditions are often dependent on EL.

So JSTL and EL are good friends who help each other and collaborate with each other. JSTL still has many functions. Due to the limited time and space, I will list the Common Use Cases of JSTL labels in development:

(1) The core label that uses the JSTL label usually uses the alias c, for example, <c: set> sets the value to store a value when it comes, it is generally used by function labels and the <c: out> output value, which can be automatically escaped to prevent script attacks.

Click <c: url>. You can splice the url path with parameters to prevent garbled submission parameters;

(2) there is also the function tag that we just mentioned. We can capture the content to display, fn: substring ();

(3) In addition, we often use the tags of the data format display alias fmt to format and display the date, data, and currency.

 

Finally, eval () in JavaScript is a function that can compute a string and execute the JavaScript code, but we rarely use it directly, when we use it

Use eval () to parse json data strings into an object, for example:

Var data = '{"return_code": 0, "return_message": "success", "data": {"data": [{"id": "1 ", "question": "Where do the princess tokens come from? "},{" Id ":" 2 "," question ":" What is the purpose of the Princess escort service? "},{" Id ":" 3 "," question ":" Where is the arena? "},{" Id ":" 4 "," question ":" Where is the north fault? "},{" Id ":" 5 "," question ":" What is the use of joy orders? "},{" Id ":" 6 "," question ":" What is the use of Token points? "},{" Id ":" 7 "," question ":" Where is the Southern fault? "},{" Id ":" 8 "," question ":" Who will the demon token be given? "},{" Id ":" 9 "," question ":" Where is the workshop? "},{" Id ":" 10 "," question ":" What is the purpose of alert? "}]} ';

 

We can use the following code to obtain the above class Map object data:

Var obj = eval ("(" + data + ")");

Alert ("return_code:" + obj ["return_code"]);

Alert ("first question id:" + obj ["data"] ["data"] [0] ["id"]);

 

Well, it's not too early. I wish myself a good dream of saving the world! The World is not early, Good Night, World!

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.