Summary of foreground and background technology in web development--about El expression, Jstl,eval () function

Source: Internet
Author: User
Tags in domain

In our daily web development, El expressions, Jstl tags, and possibly eval () in JavaScript are often used. Here's a little explanation of the techniques and functions.

EL (expression Language), full-expression language, since it is a development language, it is like Java and other languages are used to write statements, usually used in the JSP page, in order to get the JSP provided by the built-in object property values,

We usually use the EL expression, for example, a common requirement in development is to get the root of the project, if the name value of the project changes, when we specify the path, if the root directory of the project is represented by a variable, then it is not

Need to modify our code, so at this point we usually use a common expression ${pagecontext.request.contextpath}, the basic explanation of this expression is to get the JSP's built-in object PageContext request

The value of the ContextPath object's property. Here we can give a small example to see the use of ${pagecontext.request.contextpath}:

For example, our project structure is the following:

--demo

--src

--webroot

--web-inf

--css

--js

--img

--font

--page

If all our JSP files are placed under the page directory, one of the files demo.jsp references an image nav.jpg under the IMG folder, the core code is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>

<! DOCTYPE html>

<title>demo Demo Page </title>

<body>

</body>

If the project is posted to the server, there is a need to change the project name to demonstration, if we do not use El Expression in the above code to get the project name, the above code may look like this

.

So the first intuitive benefit of using the El Expression language is to extract the potentially changing parts of the page and encapsulate them as a variable, which facilitates the maintenance of the code. In addition to this, of course, in development we will still

Using a large number of El expressions, such as obtaining parameters and variables in the page field, Request field, Session field, or application domain, we often write such an El form on our pages:

<input class= "" id= "" name= "username" type= "text" value= "${username}" > Such input box usually appears in the form, then value= "${ What is the meaning of username}

It? Because the form is generally used to encapsulate the user submitted data, then if the user input is wrong, the form submitted to the background program processing, background program prompt error can not be normal according to business logic, such as the user login form has the above

<input> input box, the user entered the wrong user name, then will not be able to log on, the page should jump to the landing page, prompt the corresponding error, then if the user name is very long, input is not very convenient, we would like users to enter the user

The name echoes, then ${username}, is the completion of this function. It will start with the page field above to find out if there is a variable username, and through ${} to get the value of the variable, if no further from the request

Find in Domain ... If it is not yet found in the session field, if it has not, it will eventually be looked up from the application domain, if any, the value is output, if it has not been found, return "(or null). So the page <input>

The first time there is no value, if the user entered the wrong value, the page jumps back when the user entered the wrong value back.

So the second use of El Expressions is often used to do the echo of the data. Here, by the way, El is used to make page data echo benefits, because the El Bottom is also written in the Java language, so using El can easily get

Java type of data, but the JSP page eventually through the server "translated" into HTML, only know the string type of data, when using El it will automatically complete the basic data type conversion, such as int type data, date type of the number

According to This is not comparable to JavaScript, because JavaScript is not able to directly parse the background of the Java type data passed over.

Is there any place in the development where El is often used? Obviously there is, but it likes to bring its friend Jstl together. They usually work together to complete some jobs. For example, we have a need to determine whether the user is logged

If the landing shows the corresponding action menu, we usually use:

<c:if test= "${user! = null}" >

<ul class= "NAV" >

<li><a href= "#" > Personal Home </a></li>

<li><a href= "#" > Logout </a></li>

</ul>

</c:if>

So you can see that jstl a bit like a label, yes JSTL is the JSP standard tag library, the bottom layer of the tag is actually written in Java, it is usually associated with logical judgments, at the same time, its judgment conditions are often dependent on El to

Get, so say Jstl and El are a pair of good friends, help each other, collaborate with each other. Jstl still has a lot of features, limited to time and space, I will be the development of common JSTL tags used in the scene to enumerate:

(1) using the core label of the JSTL tag, often use alias C, such as <c:set> set value, used to store a value, generally is for function tag use, <c:out> output value, can be automatically escaped, prevent script attack

Click,<c:url> can be stitching URL path and with parameters, this can also prevent the submission of parameters garbled;

(2) There is also a function tag, we can intercept the content display, fn:substring ();

(3) In addition, we often use the format of the data to display the alias FMT label, the implementation of formatting display date, data, currency.

Finally, the eval () in JavaScript, first it is a function that computes a string and executes the JavaScript code in it, but we seldom use it directly, and the case we use it is

Use eval () to parse a string of data in JSON format into an object, such as the following example:

var data = ' {"Return_code": 0, "Return_message": "Success", "data": {"Data": [{"id": "1", "question": "Where is the princess token handed in?" "},{" id ":" 2 "," question ":" What is the use of Princess protection? " "},{" id ":" 3 "," question ":" Where is the arena? " "},{" id ":" 4 "," question ":" Where is the North fault? " "},{" id ":" 5 "," question ":" What's the use of the joy order? " "},{" id ":" 6 "," question ":" What is the use of token integrals? " "},{" id ":" 7 "," Question ":" Where is the southern fault? " "},{" id ":" 8 "," question ":" Who's the big demon token? " "},{" id ":" 9 "," question ":" Where is the workshop? " "},{" "id": "Ten", "question": "What is the use of warning demon beads?" "}]}} ';

We can get the above class map object data by using the following code:

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

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

Alert ("First question ID:" + obj["Data" ["Data"][0]["id"]);

Well, the time is not early, I wish myself to do a good dream to save the world! The world is not early, good night.

 

Summary of foreground and background technology in web development--about El expression, Jstl,eval () function

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.