13 JSP, MVC development model, EL expressions and study notes JSPL tag ---

1.JSP

(1) JSP concept: the Java Server Pages and the namely java server pages
can be understood as: a special page, which can either specify the definition of html tags, they can be defined java code
used to simplify writing ! ! !

(2) the principle of
JSP is essentially a Servlet

(3) JSP script: the JSP define how Java code
  1. <Code%%>: java codes defined in the service process. What service method can be defined, what the script can be defined.
  2. <! Code%%>: java code is defined, the position of members of the class java jsp conversion may be defined member variables or member methods . Note: with less
  3. <% = Code%>: java code is defined, is output to the page. What output statement can be defined, what the script can be defined.

 

(4) JSP instruction

Action: to configure JSP page, the resource file import
format:
  <% @ directive name attribute name attribute value = 1 1 = Attribute Name Attribute Value 2 2 ...%>

E.g:

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

Category:
  1. Page directive: Configure JSP page
    contentType: equivalent to response.setContentType ()
      1. Set the response body mime type and character set
      2. Set the current encoding jsp page (only advanced IDE to take effect, if using low-level tools, you need to set the character set property pageEncoding current page)
        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. You can not use the built-in object exception


  2. include directive: page contains. Import page resource file
    * <% @ the include File = "top.jsp"%>
  3. taglib directive: Import Resources
    * <% @ taglib prefix = " c" uri = "http://java.sun.com/jsp / JSTL / Core "%>
    * prefix: prefix custom

(5) Notes:
  1. html NOTE:
    <- -!>: Html code fragment only comment
  2. jsp NOTE: recommended
    <% - -%>: all can be annotated


(6) JSP built-in objects:
no need to obtain and created in jsp page, you can directly use the object
jsp a total of nine built-in objects.
  Request *
  * the Response
  * OUT: character output stream object. You can output the data on the page. And response.getWriter () similar
    * response.getWriter () and out.write () the difference:
    * Before the real tomcat server responds to the client, will go first response data buffer, the buffer data to find out.
    * Response.getWriter () output data always out.write () before

  Variable names true type role
  * pageContext PageContext current page to share data, you can also get eight other built-in objects
  * request HttpServletRequest request access to multiple resources once the (forward)
  * across multiple requests for a session HttpSession session, sharing data
  * application ServletContext among all users to share data
  * response HttpServletResponse response object
  * page Object current page (the Servlet) For the this
  * the JspWriter OUT output target, the output data to the page
  * config ServletConfig Servlet configuration object
  * exception Throwable exception object

2.MVC: development model
(1) jsp history of the evolution
  1. Early only servlet, can only use the response output tag data, very troublesome
  2. Later, jsp, simplifies the development of Servlet, if overused jsp, that is written in the jsp a lot of java code, write html table, makes it difficult to maintain, difficult division of labor
  3. later, web development java, learn mvc development model, making the program a more rational design of

(2) the MVC:
  1. M: the Model, models. 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

3.EL expression
(1) concept: the Expression Language expression language
(2) action: replacing jsp pages and simplify write java code
(3) Syntax: $ {} Expression
(4) Note:
  jsp el default support expression style. If you want to ignore el expression
    method 1. Set the jsp page directive: isELIgnored = "true" ignore all the current expressions of el jsp page
    2. Method \ $ {expression}: el ignore this current expression

(5) using:
  1. operation:
    * operator:
      1. arithmetic operators: + - * / (div)% (MOD)
      2. comparison operators:> <> = <= = ==!
      3. logical operation Fu:! && (and) || ( or) (not)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el_test01</title>
</head>
<body>
    ${3>4}<br>
    <hr>
    <h3>算数运算符</h3>
    ${3+4}<br>
    ${3/4}<br> 
    $ {}. 4. 3 div < br > 
    $ {}. 4. 3% < br > 
    $ {}. 4. 3 MOD < br > 
    < HR > 
    < H3 > Comparison operators </ H3 > 
    $ {}. 4. 3 == < br > 
    < HR > 
    < H3 > logical operators </ H3 > 
    $ {. 3> &&. 4. 3 < . 4 } <br > 


</ body > 
</ HTML >

      4. The air operator: empty
        * Function: means for determining a string, set, or whether the object is null array length is 0.
        * $ List} {empty: determining a string, set, whether the object is null or an array of length 0
        * $ {not empty str}: Analyzing string represents the set, if the object is not null and the array length> 0
  2. Get the value
    1. EL expression can only get the value from the domain object
    2. syntax:
      1. $ { domain name keys}: Gets the value of the specified key from the specified domain.
        * domain name:
          1. pageScope -> the pageContext domain
          2. requestScope -> request domain
          3. sessionScope -> session domain
          4. applicationScope -> application (ServletContext) domain
            * example: in the request is stored domain name = Joe Smith
            * Get: $ {requestScope.name}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        request.setAttribute("name","lucky");
        session.setAttribute("age","25");
    %>

    <h1>el获取值</h1>
    ${requestScope.name}
    ${sessionScope.age}
</body>
</html>

      2. Key $ {name}: Find the smallest sequence indicates whether a domain corresponding to the key value, until it finds.

      3. Get Object, List collection, a collection of values Map
        1. Object: $ {domain name key name attribute names..}
          * Will go getter method calls the object's essence

Case:

User.java

package el;

import java.text.SimpleDateFormat;
import java.util.Date;

public class User {
    private String name;
    private int age;
    private Date birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getBirthdayStr(){
        if(birthday!=null){
            //1.格式化日期对象
            SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            String birthday_format = sdf.format(birthday);
            return birthday_format;
        }else {
            return "";
        }
    }
}

el_test03.jsp

<%@ page import="el.User" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>获取数据</title>
</head>
<body>
    <%
        User user=new User();
        user.setName("lucky");
        user.setAge(25);
        user.setBirthday(new Date());
        request.setAttribute("user",user);
    %>
    <h3>获取对象中的值</h3>
    ${user.name}
    ${user.age}
    ${user.birthday}
    ${user.birthdayStr}
</body>
</html>

  2. List the set: $ {domain name key name [index].}

  3. Map Collection:
    * $ {domain name keys .key name.}
    * {$ Name domain name key [ "key name"].}

<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>获取list集合中的值</title>
</head>
<body>
    <%
        List list=new ArrayList();
        list.add("aaa");
        list.add("bbb");
        request.setAttribute("list",list);

        Map map=new HashMap();
        map.put("name","linda");
        map.put("age","28");
        request.setAttribute("map",map);

    %>

    <h3>获取list集合</h3>
    ${list[0]}

    <h3>获取map集合</h3>
    ${requestScope.map.name}
</body>
</html>

3. implicit objects:
* EL expression in 11 of implicit objects
* pageContext:
* get jsp eight other built-in objects
* $ {pageContext.request.contextPath}: dynamic access to virtual directory

Guess you like

Origin www.cnblogs.com/luckyplj/p/11230686.html