jsp&MVC&EL expression

Posted by atdawgie on Tue, 21 Sep 2021 23:15:33 +0200

jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>jsp form</title>
</head>
<body>
<h1>
    Hello
</h1>

</body>
</html>

instructions

Function: used to configure jsp pages and import resource files
Format:
<% @ instruction name attribute name 1 = attribute value 1 attribute name 2 = attribute value 2...% >
Classification:
1. page: configure the of jsp pages

  • contentType: equivalent to response.setContentType()
    1) Set the mine type and character set of the response body
    2) Set the encoding of the current page (only high-level IDE can take effect. If low-level tools are used, you need to set the pageEncoding property and set the character set of the current page)
  • Import: import package
    <%@ page import="java.util.ArrayList" %>
  • errorPage: after an exception occurs on the current page, it will automatically jump to the specified page
    <%@ page contentType="text/html;charset=UTF-8" errorPage="500.jsp" language="java" %>
  • isErrorPage: identifies whether the current is the specified error page
    • true: Yes, you can use the built-in object exception
    • false: No. Default value. The built-in object exception cannot be used
      <%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
      2. include: the resource file contained in the page and imported into the page
      <%@include file="top.jsp"%>
      3. taglib: import resources
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      • Prefix: prefix, customized

notes

  1. html comments:
    : only html snippets can be annotated
  2. jsp comments: Recommended
    <% ---% >: all comments can be made

Built in object

  • In jsp pages, you do not need to create objects that can be used directly

  • There are 9:
    Variable name real type function
    *pageContext PageContext the current page shares data, and eight other built-in objects can be obtained
    *request HttpServletRequest requests multiple resources to be accessed at one time (forwarding)
    *session HttpSession multiple requests in a session
    *application ServletContext all users share data
    *response HttpServletResponse response response object
    *page Object this object of the current page (Servlet)
    *out JspWriter output object, data output to page
    *config ServletConfig Servlet's configuration object
    *exception Throwable exception object

  • The four scopes pageContext, request, session and application are different:
    1. The scope of pageContext is page, and its valid scope is only in the current jsp page. You can use this variable from putting the variable into pageContext to the end of jsp page.
    2. The request scope is request, and its valid range is the current request cycle. The so-called request cycle refers to the whole process from the initiation of http request to the end of server processing and the return of response. In this process, you may use the forward method to jump to multiple jsp pages, in which you can use this variable.
    3. The session scope is session, and its valid range is the current session. The so-called current session refers to the process from the user opening the browser to the user closing the browser. This process may contain multiple request responses. That is, as long as the user does not close the browser, the server can know that these requests are initiated by one person. The whole process is called a session, and the variables in the session,
    4. The scope of application is application, and its effective scope is the whole application. The whole application refers to the start of the application and the end of the application.

MVC development mode

  1. M: Model, model. JavaBean
    *Complete specific business operations, such as querying the database and encapsulating objects
  2. 5: V iew, View. JSP
    *Display data
  3. C: Controller, controller. Servlet
    *Get user input
    *Call model
    *Give the data to the view for presentation

EL expression

Concept: Expression Language
Function: replace and simplify the writing of java code in jsp pages
Syntax: ${expression}
be careful:
*jsp supports el expressions by default. If you want to ignore the el expression
1. Set the page instruction in jsp: isELIgnored = "true" ignore all el expressions in the current JSP page
2. ${expression}: ignore the current el expression
use:

  1. Operation:
    *Operator:
    1) Arithmetic operator: + - * / (DIV)% (MOD)
    2) Comparison operator: > < > = < = ==
    3) Logical operator: & & (and) | (or)! (not)
    4) Air transport operator: empty
    *Function: used to judge whether string, collection and array objects are null or length is 0
    *${empty list}: judge whether the string, collection and array objects are null or have a length of 0
    *${not empty str}: indicates whether the string, collection and array objects are not null and the length is > 0
  2. Get value
    1) el expressions can only get values from domain objects
    2) Syntax:
    (1) field name call . key name : from finger set field in Obtain take finger set key of value ∗ field name call : 1. p a g e S c o p e − − > p a g e C o n t e x t 2. r e q u e s t S c o p e − − > r e q u e s t 3. s e s s i o n S c o p e − − > s e s s i o n 4. a p p l i c a t i o n S c o p e − − > a p p l i c a t i o n ( S e r v l e t C o n t e x t ) ∗ lift example : stay r e q u e s t field in Save Store Yes n a m e = Zhang three ∗ Obtain take : {domain name. Key name}: get the value of the specified key from the specified domain * domain name: 1. Pagescope -- > pagecontext 2. Requestscope -- > request 3. Sessionscope -- > session 4. Applicationscope -- > Application (ServletContext) * example: name = Zhang San * is stored in the request domain: Domain name. Key name: get the value of the specified key from the specified domain * domain name: 1.pageScope − − > pagecontext2. Requestscope − > request3. Sessionscope − > session4. Applicationscope − > Application (ServletContext) * example: name = Zhang San * is stored in the request domain: {requestScope.name}
    (2) ${key name}: indicates whether there is a value corresponding to the key from the smallest field until it is found.
<%--
  Created by IntelliJ IDEA.
  User: qing
  Date: 2021/9/21
  Time: 10:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL1 Get the data in the domain from</title>
</head>
<body>
<%
    //Storing data in a domain
    request.setAttribute("name","Zhang San");
    session.setAttribute("name","Li Si");
    session.setAttribute("age","23");
    session.setAttribute("str","");
%>
<h3>Get value</h3>
${requestScope.name}
${sessionScope.name}
${sessionScope.age}
${sessionScope.str}

</body>
</html>

	    3)Get object List Set Map The value of the collection
	        (1) Object: ${Domain name.Key name.Attribute name}
					* In essence, it will call the object getter method
            (2)List Set: ${Domain name.Key name[Indexes]}
            (3) Map Set:
					* ${Domain name.Key name.key name}
					* ${Domain name.Key name["key name"]}
<%@ page import="domain.User" %><%--
  Created by IntelliJ IDEA.
  User: qing
  Date: 2021/9/21
  Time: 11:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="domain.User" %>
<%@ page import="java.util.*" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    User user=new User();
    user.setName("Zhang San");
    user.setAge(22);
    user.setBirthday(new Date());

    request.setAttribute("u",user);

    List list=new ArrayList();
    list.add("aaa");
    list.add("bbb");
    list.add("user");

    request.setAttribute("list",list);

    Map map=new HashMap();
    map.put("name","Li Si");
    map.put("gender","female");
    map.put("user","user");

    request.setAttribute("map",map);
%>
<h3>Gets the value in the object</h3>
${requestScope.user}
${u.name}
${u.age}
${u.birthday.month}
${u.birStr}

<h3>obtain list value</h3>
${list}
${list[0]}
${list[1]}

<h3>obtain map value</h3>
${map.gender}<br>
${map["name"]}
</body>
</html>

package domain;

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

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

    public User() {
    }

    public User(String name, int age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = 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 getBirStr(){
        if (birthday!=null){
            //1. Format date object
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM--dd HH:mm:ss");
            //2. Just return the string
            return sdf.format(birthday);
        }else{
            return "";
        }
    }
}

  1. Implicit objects:
    *There are 11 implicit objects in the el expression
    * pageContext:
    *Get the jsp and the other eight built-in objects
    *${pageContext.request.contextPath}: get virtual directory dynamically

Topics: JSP mvc