View layer development using EL and Jstl

Source: Internet
Author: User

1. Why do I need an EL expression

With the MVC design program, the JSP is just a view, and the task of the view is to show the response instead of doing anything about program control and business logic in the JSP. So there should be as few or no Java code in the JSP page as possible. The use of JSP standard actions alone can not meet the requirements, so there is an El expression.

2, EL (Expression Language) expression

Syntax for El Expressions: Starts with "$" and begins with "{", eventually ending with "}".

El Syntax:

${el expression}

For example: ${person.name}.

“.” The called point operator, on the left side of the point operator, can be a JavaBean object, or an El implicit object, and the right side of the point can be either a JavaBean property or a mapping key.

If you use the El Action array, you should use []. For example: ${list[0]}. Not only can you manipulate arrays, but you can also manipulate the objects that the dot operator can manipulate, such as: ${person["name"}, and "[]" should contain "" "when using the [] operator. ${list["0"]} is equivalent to ${list[0]}.

El can be interpreted as a way of not using Java to access Java objects or variables.

El can manipulate objects, arrays, mappings, and perform operational and logical operations on expressions.

3, El expression value range: el expression can only be taken to the page, request, session, application range of property values. The object being evaluated is the name of the property that is stored, as is the property name that is saved.

The EL expression first gets the get ... method, and then the attribute value, which is considered to be the property value after the get.

For example: private String name;

Public String GetUserName () {

return name;

}

Then think of username as a property value, not name.

An implicit object param. Gets the data for form form, <input type= "text" name= "username" > ${param.username}

4, what is Jstl.

JSTL (Java Server pages standerd tag library,jsp standard tag library) contains a standard set of tags for writing and developing JSP pages that can provide users with a script-free environment. In this environment, users can write code using tags without using Java scripts. Jstl contains a variety of tags, such as iteration labels, condition labels, and SQL tags. Iteration labels and condition labels are used to handle page flow control, and SQL tags are used to access databases and update database table data.

5, add Jstl.

New project, MYECLIPSE->J2EE projects->web Project command-> in JSTL Support (support JSTL) area, select Add JSTL libraries to Web-inf/lib folder (add Jstl tag Library to web-inf/lib folder) check box, select the JSTL 1.1 radio button. Two jar files are generated in the project Web-inf/lib directory: Jstl.jar, Standard.jar, and many. tld files, such as C.tld, SQL.TLD, etc., are generated in the Web-inf directory. You can use Jstl when you import a tag library descriptor using the taglib instruction on a JSP page that uses the JSTL tag.

Taglib is a JSP instruction, its role is in the JSP page, the tag library description file (. tld) into the page, and set the prefix, using the label prefix to use the tag library descriptor file tags. The syntax for the taglib instruction is as follows: <%@ taglib uri= "tag Library descriptor file" prefix= "prefix name"%>

6, JSTL core Tag Library classification: According to the function of the general tag library, conditional tag library, iterative tag library.

A generic label is used for a variable created by a JSP page. A condition label is used to conditionally judge and process code in a JSP page, and the iteration label is used to iterate through a collection of objects.

6.1. General Label

Generic tags are used to set, delete, and display variable values within a JSP page. There are 3 universal labels: set, remove, out.

<C:SET>: Sets the value of the variable within the specified range, and creates it if the variable does not exist. The syntax is as follows:

<c:set var= "name" value= "value" scope= page | Request | Session | Application "/>

var specifies the name of the variable to store the value specified by value, the value of the variable specified by VAR, which specifies the range of the variable, and the default is page.

<c:remove>: Used to delete variables.

<c:remove var= "name" scope= "page | Request | Session | Application "/>

where var specifies the name of the variable to be deleted, scope specifies the range of the variable.

<c:out> evaluates the expression and displays the results on the page.

<c:out value= "Value"/>

Value to specify an expression or variable.

For example:

<%@ page language= "java" pageencoding= "GBK"%>

<%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix= "C"%>

<title>jstl Example </title>

<body>

Set the value of Xinxin to 1112 and save the variable in session.

<c:set var= "Xinxin" value= "${1110+2}" scope= "Session"/>

Display the value of a variable xinxin on a page

<c:out value= "${xinxin}"/>

To remove a variable from the session Xinxin

<c:remove var= "Xinxin" scope= "Session"/>

</body>

6.2. Condition Label

The syntax is as follows:

<c:if test= "condition" var= "name" scope= "page" >

When the condition is true, the code executed

</c:if>

TEST Specifies the condition, typically using the El method to perform conditional operations: ${conditional Operations}, operators can use the <, >, = =, or you can use

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.