JSP


Javaweb - day10 -JSP

JSP

  • 概念: Java Server Pages,Java服务端页面
  • 一种动态的网页技术,其中既可以定义HTML、JS、CSS等静态内容,还可以定义Java代码的动态内容
  • JSP = HTML + Java

实例

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    
        <h1>hello jsp</h1>
    
        <%
            System.out.println("hello,jsp~");
            int i = 3;
        %>


​    
​        <%="hello"%>
​        <%=i%>
​    

        <%!
            void  show(){}
            String name = "zhangsan";
        %>
        
    </body>
    </html>

JSP快速入门

实例(导坐标)

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

JSP原理

  • JSP本质上就是一个Servlet
  • JSP在被访问时,由JSP容器(Tomcat)将其转换为Java文件(Servlet),在由JSP容器(Tomcat)将其编译,最终对外提供服务的其实就是这个字节码文件。

JSP脚本

  • JSP脚本用于在JSP页面内定义Java代码

JSP脚本分类

案例(使用JSP 脚本展示品牌数据)

Brand.java实体类

    package com.itheima.pojo;
    
    /**
     * 品牌实体类
     */
    
    public class Brand {
        // id 主键
        private Integer id;
        // 品牌名称
        private String brandName;
        // 企业名称
        private String companyName;
        // 排序字段
        private Integer ordered;
        // 描述信息
        private String description;
        // 状态:0:禁用  1:启用
        private Integer status;


​    
​        public Brand() {
​        }
​    

        public Brand(Integer id, String brandName, String companyName, String description) {
            this.id = id;
            this.brandName = brandName;
            this.companyName = companyName;
            this.description = description;
        }
    
        public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) {
            this.id = id;
            this.brandName = brandName;
            this.companyName = companyName;
            this.ordered = ordered;
            this.description = description;
            this.status = status;
        }
        // getId  ${brand.id}   Id   getId
        public Integer getId() {
            System.out.println("getId方法被调用了...");
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getBrandName() {
            return brandName;
        }
    
        public void setBrandName(String brandName) {
            this.brandName = brandName;
        }
    
        public String getCompanyName() {
            return companyName;
        }
    
        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }
    
        public Integer getOrdered() {
            return ordered;
        }
    
        public void setOrdered(Integer ordered) {
            this.ordered = ordered;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public Integer getStatus() {
            return status;
        }
    
        public void setStatus(Integer status) {
            this.status = status;
        }
    
        @Override
        public String toString() {
            return "Brand{" +
                    "id=" + id +
                    ", brandName='" + brandName + '\'' +
                    ", companyName='" + companyName + '\'' +
                    ", ordered=" + ordered +
                    ", description='" + description + '\'' +
                    ", status=" + status +
                    '}';
        }
    }

brand.jsp

    <%@ page import="com.itheima.pojo.Brand" %>
    <%@ page import="java.util.List" %>
    <%@ page import="java.util.ArrayList" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    <%
        // 查询数据库
        List<Brand> brands = new ArrayList<Brand>();
        brands.add(new Brand(1,"三只松鼠","三只松鼠",100,"三只松鼠,好吃不上火",1));
        brands.add(new Brand(2,"优衣库","优衣库",200,"优衣库,服适人生",0));
        brands.add(new Brand(3,"小米","小米科技有限公司",1000,"为发烧而生",1));
    
    %>


​    
​    <!DOCTYPE html>
​    <html lang="en">
​    <head>
​        <meta charset="UTF-8">
​        <title>Title</title>
​    </head>
​    <body>
​    <input type="button" value="新增"><br>
​    <hr>
​    <table border="1" cellspacing="0" width="800">
​        <tr>
​            <th>序号</th>
​            <th>品牌名称</th>
​            <th>企业名称</th>
​            <th>排序</th>
​            <th>品牌介绍</th>
​            <th>状态</th>
​            <th>操作</th>
​    

        </tr>


​    
​    
​        <%
​            for (int i = 0; i < brands.size(); i++) {
​                Brand brand = brands.get(i);
​        %>
​    

        <tr align="center">
            <td><%=brand.getId()%></td>
            <td><%=brand.getBrandName()%></td>
            <td><%=brand.getCompanyName()%></td>
            <td><%=brand.getOrdered()%></td>
            <td><%=brand.getDescription()%></td>
    
            <%
                if(brand.getStatus() == 1){
                    //显示启用
            %>
                <td><%="启用"%></td>
            <%
                }else {
                    // 显示禁用
            %>
                <td><%="禁用"%></td>
            <%
                }
            %>
    
            <td><a href="#">修改</a> <a href="#">删除</a></td>
        </tr>
    
        <%
            }
        %>


​    
​    
​    </table>
​    

    </body>
    </html>

JSP缺点

EL表达式

  • Expression Language表达式语言,用于简化JSP页面内的Java代码
  • 主要功能:获取数据

实例(ServletDemo1)

package com.itheima.web;

import com.itheima.pojo.Brand;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 准备数据
        List<Brand> brands = new ArrayList<Brand>();
        brands.add(new Brand(1,"三只松鼠","三只松鼠",100,"三只松鼠,好吃不上火",1));
        brands.add(new Brand(2,"优衣库","优衣库",200,"优衣库,服适人生",0));
        brands.add(new Brand(3,"小米","小米科技有限公司",1000,"为发烧而生",1));


        //2. 存储到request域中
        request.setAttribute("brands",brands);
        request.setAttribute("status",1);

        //3. 转发到 el-demo.jsp
        //request.getRequestDispatcher("/el-demo.jsp").forward(request,response);
        //request.getRequestDispatcher("/jstl-if.jsp").forward(request,response);
        request.getRequestDispatcher("/jstl-foreach.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

实例(el-demo.jsp)

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

    ${brands}


</body>
</html>

JSTL标签

  • JSP标准标签库(Jsp Standarded Tag Library),使用标签取代JSP页面上的Java代码。

实例(导坐标)

<!--jstl-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

使用(c:if)

  • <c:if>

实例(jstl-if.jsp)(和他有关的request域在上面的ServletDemo1中)

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    
        <%--
            c:if:来完成逻辑判断,替换java  if else
        --%>
    
    
        <c:if test="true">
            <h1> true</h1>
        </c:if>
    
        <c:if test="false">
            <h1> false</h1>
        </c:if>
    
        <c:if test="${status ==1}">
            启用
        </c:if>
    
        <c:if test="${status ==0}">
            禁用
        </c:if>
    
    </body>

</html>

使用(c:forEach)

  • <c:forEach>:相当于for循环
  • items:被遍历的容器
  • var:遍历产生的临时变量

实例(jstl-forEach.jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="button" value="新增"><br>
<hr>
<table border="1" cellspacing="0" width="800">
    <tr>
        <th>序号</th>
        <th>品牌名称</th>
        <th>企业名称</th>
        <th>排序</th>
        <th>品牌介绍</th>
        <th>状态</th>
        <th>操作</th>

    </tr>


    <c:forEach items="${brands}" var="brand" varStatus="status">
        <tr align="center">
            <%--<td>${brand.id}</td>--%>
            <td>${status.count}</td>
            <td>${brand.brandName}</td>
            <td>${brand.companyName}</td>
            <td>${brand.ordered}</td>
            <td>${brand.description}</td>
            <c:if test="${brand.status == 1}">
                <td>启用</td>
            </c:if>
            <c:if test="${brand.status != 1}">
                <td>禁用</td>
            </c:if>

            <td><a href="#">修改</a> <a href="#">删除</a></td>
        </tr>

    </c:forEach>




</table>


<hr>

<c:forEach begin="1" end="10" step="1" var="i">
    <a href="#">${i}</a>
</c:forEach>


</body>
</html>

MVC模式和三层架构

MVC模式

  • MVC是一种分层开发的模式
  • M: Model,业务模型,处理业务
  • V: View,视图,界面展示
  • c: Controller,控制器,处理请求,调用模型和视图

三层架构

  • 表现层:接收请求,封装数据,调用业务逻辑层,响应数据
  • 业务逻辑层:对业务逻辑进行封装,组合数据访问层层中基本功能,形成复杂的业务逻辑功能
  • 数据访问层:对数据库的CRUD基本操作

MVC模式和三层架构的区别

声明:三二一的一的二|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - JSP


三二一的一的二