ES & JSTL January 27,2020

## JSP:


An instruction
  * Function: Used configuration JSP page, the resource file import
  * format:
    <% @ directive name attribute name attribute value = 1 1 = Attribute Name Attribute Value 2 2 ...%>
  * Category:
    1. Page: Configuration JSP pages
      * contentType: equivalent to response.setContentType ()
        1. set the mime type and character set of the response body
        2. set the encoding of the current jsp page (only advanced IDE to take effect, if a low-level tool, you need to set pageEncoding property of the current page character set)
      * Import: guide package
      * errorPage: exception occurs after the current page will automatically jump to a specific error page
      * isErrorPage: identify the current is whether the error page.
        * True: yes, you can use the built-in object Exception
        * false: no. Defaults. Can not use the built-in object exception


    2. include: the page contains. Import page resource file
      * <% @ the include File = "top.jsp"%>
    3. taglib: Import Resources
      * <% @ taglib prefix = " c" uri = "http://java.sun.com/jsp/ JSTL / Core "%>
      * prefix: prefix custom
II Note:
  1. html NOTE:
    <- ->:! html code fragment only comment
  2. jsp NOTE: recommended
    <% - - %>: You can annotate all

  3. built-in objects
    * does not need to be created in the jsp page, the objects used directly
    * a total of 9:
    variable name real effect type
    * pageContext PageContext current page to share data, you can also get eight other built-in objects
    * Request the HttpServletRequest one request access a plurality of resources (forward)
    * the session among the plurality of requests in a session HttpSession
    * file application among all users to share data ServletContext
    * response the HttpServletResponse response object
    * page Object current page (the Servlet) For the this
    * OUT output JspWriter output target, the data the page
    * config the ServletConfig Servlet configuration object
    * Exception Throwable exception object

 

## MVC: development model

MVC:
  1. M: Model, model. The JavaBean
    * accomplish specific business operations, such as: a database query, encapsulate the object
  2. V: View, View. JSP
    * Display data
  3. C: Controller, Controller. Servlet
    * get the user's input
    * call model
    * to view the data on display


  * Advantages and disadvantages:
    1. Advantages:
      1 low coupling, easy maintenance, can be beneficial division of labor
      2. high reusability

    2. Disadvantages:
      1. make the project architecture is complicated, high requirements for developers

 

 

 

 

 

 

## EL expression


First, the concept : Expression Language expression language
Second, the role:   to write and simplify replacement jsp page java code
three syntax:   $ {expression}
Fourth, note:
  * jsp el expressions of support by default. If you want to ignore el expressions
  1. Set the jsp page directive: isELIgnored = "true" ignore all the current expressions of el jsp page
  2. \ $ {expression}: el ignore this current expression


五、使用:
  1. 运算:
    * 运算符:
      1. 算数运算符: + - * /(div) %(mod)
      2. 比较运算符: > < >= <= == !=
      3. 逻辑运算符: &&(and) ||(or) !(not)
      4. 空运算符: empty
        * 功能:用于判断字符串、集合、数组对象是否为null或者长度是否为0
        * ${empty list}:判断字符串、集合、数组对象是否为null或者长度为0
        * ${not empty str}:表示判断字符串、集合、数组对象是否不为null 并且 长度>0
  2. 获取值
    1. el表达式只能从域对象中获取值
    2. 语法:
      1. ${域名称.键名}:从指定域中获取指定键的值
        * 域名称:
          1. pageScope --> pageContext
          2. requestScope --> request
          3. sessionScope --> session
          4. applicationScope --> application(ServletContext)
          * 举例:在request域中存储了name=张三
          * 获取:${requestScope.name}

      2. ${键名}:表示依次从最小的域中查找是否有该键对应的值,直到找到为止。



      3. 获取对象、List集合、Map集合的值
        1. 对象:${域名称.键名.属性名}
          * 本质上会去调用对象的getter方法

        2. List集合:${域名称.键名[索引]}

        3. Map集合:
          * ${域名称.键名.key名称}
          * ${域名称.键名["key名称"]}


  3. 隐式对象
    * el表达式中有11个隐式对象
    * pageContext:
    * 获取jsp其他八个内置对象
    * ${pageContext.request.contextPath}:动态获取虚拟目录


## JSTL


一、概念:JavaServer Pages Tag Library JSP标准标签库
  * 是由Apache组织提供的开源的免费的jsp标签 <标签>

二、作用:用于简化和替换jsp页面上的java代码

三、使用步骤:
  1. 导入jstl相关jar包
  2. 引入标签库:taglib指令: <%@ taglib %>
  3. 使用标签

四、常用的JSTL标签
  1. if: 相当于java代码的if语句
    1. 属性:
      * test 必须属性,接受boolean表达式
      * 如果表达式为true,则显示if标签体内容,如果为false,则不显示标签体内容
      * 一般情况下,test属性值会结合el表达式一起使用
    2. 注意:
      * c:if标签没有else情况,想要else情况,则可以在定义一个c:if标签
  2. choose: 相当于java代码的switch语句
    1. 使用choose标签声明 相当于switch声明
    2. 使用when标签做判断 相当于case
    3. 使用otherwise标签做其他情况的声明 相当于default

  3. foreach:相当于java代码的for语句

Guess you like

Origin www.cnblogs.com/yyanghang/p/12236919.html