Java Learning Jstl and El

Source: Internet
Author: User
Tags control label

first, the basic

1 , EL (Expression Language): to make JSP It is easier to write and provides a JSP methods for simplifying expressions in

2 Span lang= "ZH-CN" style= "Font-family:simsun", jstl: (JSP Standard tag Library) jstl standards tag libraries, from apache implementation

3 , JSTL generally to cooperate with EL expression is used together with the JSTL use a lot when you assign a value EL, can say EL is a JSTL a kind of auxiliary.

4, when in use:JSTLneed to introduce:Jstl.jarand theStandart.jarto theWeb_inf/libfolder, but only using theELexpressions do not need to be referenced, no matter whatJarpackage, just toJsp/servletthe container implements thej2ee1.4/servlet2.4,Jsp2.0specification will be able to.

Second, why

Before you learnJSPwas also said to beHTMLcode, andJavacode separation, but most of the time assume that there are no restrictions,JSPcalled inJavaThe logic class is very difficult to avoid, so we have aJSTLto implement the separation of the presentation layer from the business logic, we are able toJSTLimplementation: Withservletdata interaction, manipulationXML, formatted data display and style display,Requestreading data, looping, defining functions, etc.

Three,EL expression:

1 , the basic syntax: ${}: Example: hello:${hello}<br>

2 , built-in objects: Pagescope,requestscope,sessionscope,applicationscope (sorted by default search order)

3 , specify search: ${requestscope.hello}

4 , simple example:

In fact , EL is very easy .

The first thing we want to write is the servlet: The code for the servlet is the following:

Package Com.tgb.jstl;import Java.io.ioexception;import Java.util.arraylist;import java.util.hashmap;import Java.util.list;import Java.util.map;import Javax.servlet.servletexception;import Javax.servlet.http.HttpServlet; Import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * Test El expression * @author Youngjong * */public class Jstlelservlet extends HttpServlet {@Overrideprotected void doget (HttpServletRequest req, HttpS Ervletresponse resp) throws Servletexception, IOException {//TODO auto-generated method Stub//super.doget (req, resp);// Ordinary string Req.setattribute ("Hello", "Hello World");//Structure Group group=new Group (); Group.setname ("Chenlina good"); User User=new User (); User.setusername ("Yang"); user.setage; User.setgroup (group); Req.setattribute ("User", user);    /map map Map=new HashMap ();    Map.put ("K1", "v1");    Map.put ("K2", "V2");            Req.setattribute ("map", map);    Array string[] strarray=new string[]{"A", "B", "C"}; Req.setattribute ("Str_array", StrarrAY);    Object array user[] users=new user[10];    for (int i=0;i<users.length;i++) {users[i]=new User ();    Users[i].setusername ("Zhang San _" +i);    } req.setattribute ("Users", users);    List List userlist=new ArrayList ();    for (int i=0;i<10;i++) {User userl=new user ();    Userl.setusername ("John Doe _" +i);    Userlist.add (Userl);        } req.setattribute ("UserList", userlist); Req.getrequestdispatcher ("/jstl_el.jsp"). Forward (req, resp);}}

Here's how to get in the JSP :

<body> 

Effect:


Four,JSTL Core library:

despite EL very easy, but his function is limited, such as: for return null value EL There is no way to control the loop output array element, assuming that the combined JSTL you don't have to worry about these problems any more.

Regular use of JSTL There are a lot of tags like: <c:out><c:if><c:choose><c:forEach> wait

here is not introduced, the following introduction of a EL with the JSTL combined with the convenience of:

Simple example: the first is Servlet The code:

Package Com.tgb.jstl;import Java.io.ioexception;import Java.io.printwriter;import java.util.arraylist;import Java.util.hashmap;import Java.util.list;import Java.util.map;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Jstlcoreservlet extends HttpServlet {@Overrideprotected void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {// Ordinary string Request.setattribute ("Hello", "Hello World");//html string Request.setattribute ("Welcome", "<font color= ' red ' > Welcome you to this world! </font> ");//structure Group group = new Group (); Group.setname (" accelerated "); List users = new ArrayList (), for (int i=0; i<10; i++) {User user = new User (), User.setusername ("Chenlina _" + i); User.setage ( User.setgroup (group); Users.add (user);} Request.setattribute ("Users", "users"),//mapmap map = new HashMap (), Map.put ("K1", "v1"), Map.put ("K2", "V2"); Request.setattributE ("map", map); Request.getrequestdispatcher ("/jstl_core.jsp"). Forward (request, response);}} 

The code for the JSP page:

<%@ page language= "java" contenttype= "text/html; charset=gb18030 "pageencoding=" GB18030 "%><%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix=" C "%> & lt;! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

It should be noted that the introduction of:

<%@ tagliburi= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>

Page display:


Use JSTL with the EL The combination separates the page and logic, making the layering more clear and the code clearer.

Java Learning Jstl and El

Related Article

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.