JSP、EL、JSTL

一:jsp

概念:java服务器端页面

可以定义html标签,又可以定义java代码,用于简化书写

原理:本质就是一个Servlet

脚本:<% %>定义的代码在service方法中,输出在控制台上;            <%!   %>jsp转换后的java类的成员位置, 定义成员变量或成员方法;               <%=   %>定义的java代码会输出到页面上

 

内置对象:pageContext(作用当前页面,还可以共享其他八大对象)          request(一次请求访问的多个资源(转发))       

session(一次会话的多次响应)     application(多个用户间共享数据)                                   前四个域对象,用来共享数据

response(响应对象)             page(当前页面)            out(输出对象)                config(配置对象)          exception(异常对象)

out:字符的输出流对象。和response.getWriter( )类似; 区别:response对象先于out输出.(在tomcat服务器真正作出响应之前,先找response缓冲区,再找out缓冲区)

 

jsp指令:<%@ 指令名称   属性1=值1...   %>

page:配置jsp页面

相关属性:1.contentType(设置响应体的MIME类型以及字符集;设置当前jsp页面编码(高级开发工具会生效))    2. import导包     3.errorPage="500.jsp"(友好页面,如果出现500错误,会给一些友好提示,如服务器拥堵中...,正在抢救中...)   4.isErrorPage标识错误页面(设置为true时,可以使用内置对象exception)

include:页面包含的,导入页面的资源文件(如:<%@  include file="1.jsp" %>)

taglib:导入资源(如:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core "  %>         prefix前缀)

jsp注释:<%--  --%>注释所有   <!--  -->只能注释html代码片段

二:EL:表达式语言${ }

1.基本的运算:

 

 2.从域对象中获取值  $ { 域名称,键名 }

pageScope->pageContext

requesstScope->request

sessionScope->session

applicationScope->application

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

 

 3.获取对象(自定义对象和list集合)

 4.获取map集合,empty函数

 

 三:JSTL:jsp标准标签库

1.作用:简化替换java代码

 解析文件异常:

 

 解决:在WEB-INF的lib目录下导入jstl.jar和standard.jar包,以及在tomcat的lib目录下导入这两个包

 

2.if标签的错误:

 

 

 

 

 3.forEach标签和choose标签

 

 四:案例1:request域中存有一个User对象的List集合,使用EL+JSTL将list集合中的数据展示到jsp的表格table中

 

 

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Date" %>
<%@ page import="ELandJSTL.User" %><%--
  Created by IntelliJ IDEA.
  User: laurarararararara
  Date: 2020/2/19
  Time: 21:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>案例</title>
</head>
<body>
<%
    List list=new ArrayList();
    list.add(new User("laura",22,new Date()));
    list.add(new User("lily",23,new Date()));
    list.add(new User("ning",21,new Date()));
    request.setAttribute("list",list);
%>
<table border="1" align="center">
    <tr bgcolor="#ffc0cb">
        <th>学号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>生日</th>
    </tr>
    <c:forEach items="${list}" var="user" varStatus="s">
        <tr bgcolor="#ffc0cb">
            <td>${s.count}</td>
            <td>${user.name}</td>
            <td>${user.age}</td>
            <td>${user.bir}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

 五:案例2:列表查询

相关异常处理:forEach标签没有嵌入表格

 

Users对象没有实现空构造函数:

 

 

 具体结构:

<%--
  Created by IntelliJ IDEA.
  User: laurarararararara
  Date: 2020/2/20
  Time: 17:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>

<html>
<head>
    <title>用户信息页面</title>
    <style>
        a:link{
            color: blue;
        }
    </style>
</head>
<body>
<table  bgcolor="#ffc0cb" align="center" border="1">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${users}" var="user" varStatus="s">
    <tr>
        <td>${s.count}</td>
        <td>${user.name}</td>
        <td>${user.gender}</td>
        <td>${user.age}</td>
        <td><a href="add.html">修改</a><a href="delete.html">删除</a></td>
    </tr>
    </c:forEach>
    <tr>
        <td colspan="8" align="center"><a href="add.html">添加联系人</a></td>
    </tr>
</table>
</body>
</html>
list.jsp
package service.Impl;
import dao.UserDao;
import dao.impl.UserDaoImpl;
import service.UserService;
import domain.Users;
import java.util.List;
//service层的实现类
public class UserServiceImpl implements UserService {
    UserDao dao=new UserDaoImpl();
    @Override
    public List<Users> findAll() {
        return dao.findAll();
    }
}
UserServiceImpl
package web;
import service.UserService;
import service.Impl.UserServiceImpl;
import domain.Users;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 2020/2/20
 */
@WebServlet("/Login3/userListServlet")
public class UserListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用UserService完成查询
        UserService userService = new UserServiceImpl();
        List<Users> users = userService.findAll();
        //将list存入request域
        request.setAttribute("users",users);
        //转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
控制器servlet
UserDaoImplUserDao
posted @ 2020-02-17 22:16  acehm  阅读(215)  评论(0编辑  收藏  举报