后台技术学习5

MVC

  1、M:model模型,JavaBean 完成具体业务操作

  2、V:view 视图jsp,展示数据

  3、C:controller 控制器 Servlet 获取用户输入 调用模型

MVC三层架构

  1、界面层(表示层/web层):接受用户参数,封装数据,调用业务逻辑层完成处理,转发jsp展示 SpringMVC框架

  2、业务逻辑层:组合DAO层中简单方法,行程复杂的功能 Spring框架

  3、数据访问层:CRUD操作,MyBaits框架

EL表达式

  1、表达式语言,替换和简化jsp页面中的Java代码编写,${表达式}

  2、忽略el表达式 

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

\${el表达式}

 

  3、算术运算符、比较运算符、逻辑运算符、空运算符(empty)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el1</title>
</head>
<body>
<%--
    EL表达式是一个表达式语言,替换和简化JSP页面中java代码的编写,
    ${表达式}
    在page中 isELIgnore = "true" 以及 \${}都可以使el表达式失效
    1、算术运算符
        + - * /(div) %(mod)
    2、比较运算符
        > < >= <= == !=
    3、逻辑运算符
        &&(and) ||(or) !(not)
--%>
<h3>算术运算符</h3>
${5 * 2}<br>
${3 + 4}<br>
${3 / 4}<br>
${3 div 4}<br>
${5 % 2}<br>
${7 mod 4}<br>
<hr>
<h3>比较运算符</h3>
${4 == 8}<br>
${3 != 0}<br>
${6 >= -9}<br>
<hr>
<h3>逻辑运算符</h3>
${5 < 6 && 8 > 9}<br>
${7 < 10 || 8 > 10}<br>
<hr>
<h3>empty运算符</h3>
<%
    String s1 = "";
    String s2 = null;
    String s3 = "abc";
    request.setAttribute("s1",s1);
    session.setAttribute("s2",s2);
    pageContext.setAttribute("s3","s3");
%>
${empty s1}<br>
${empty s2}<br>
${empty s3}<br>
</body>
</html>

  4、获取值

  (1)、el表达式只能从域对象中获取值

   获取方法①${域名.键名}:从指定域中获取指定键

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

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el2获取数据</title>
</head>
<body>
<%--
    1、el表达式只能从域对象中获取
    2、①${域名.键名}:从指定域中获取指定键
        域名:pageScope   pageContext
             requestScope   request
             sessionScope   session
             applicationScope   application(ServletContext)
       ②${键名}一次从最小的域中查找是否有该键对应的值
--%>
<%
//    在域中存储数据
    pageContext.setAttribute("name","张三");
    request.setAttribute("age","30");
    session.setAttribute("phone","89764");
    application.setAttribute("id","890");
    request.setAttribute("mmm","7688");
%>
<h3>el获取数据</h3>
${pageScope.name}<br>
${requestScope.age}<br>
${sessionScope.phone}<br>
${applicationScope.id}<br>
${mmm}
</body>
</html>

  (2)获取对象、List集合、Map集合的值

  对象:${域名.键名.属性值}本质上会调用对象的getter方法

  List集合:${域名.键名[index]}

  Map集合:${域名.键名["key"]}或${域名.键名.key名}

<%@ page import="Bean.Student" %>
<%@ 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>对象、集合的获取</title>
</head>
<body>
<%--获取对象、List集合、Map集合的值
    对象:${域名.键名.属性值}本质上是调用对象的gettwe方法
    List集合:${域名.键名[index]}
    Map集合:${域名.键名.key名称}   ${域名.键名["key"]}
--%>
<%
    Student student = new Student();
    student.setId(2000987);
    student.setName("李四");
    student.setAge(29);
    student.setPhone("897424");
    request.setAttribute("stu",student);

    List list = new ArrayList();
    list.add("张三");
    list.add("罗翔");
    request.setAttribute("list",list);

    Map map = new HashMap();
    map.put("sname","王五");
    map.put("sex","");
    request.setAttribute("map",map);
%>
<h3>获取对象中的数据</h3>
${requestScope.stu.id}<br>
${stu.name}<br>
${stu.age}<br>
${stu.phone}<br>
<hr>
<h3>获取list集合中的数据</h3>
${list}<br>
${list[1]}<br>
<h3>获取Map集合中的数据</h3>
${map.sname}<br>
${map["sex"]}
</body>
</html>

  (3)、隐式对象

    el表达式有11个隐式对象,pageContext:能够获取jsp其他8个内置对象

    ${pageContext.request.contextPath}动态获取虚拟目录,可以作页面的跳转,防止出错

JSTL

  jstl是jsp标准标签库,用于简化和替换jsp页面上的Java代码,使用jstl需要导包,并且在jsp页面上导入

 

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

 

官网:

http://tomcat.apache.org/taglibs/standard/

 常用标签有

1、if标签,相当于Java中的if标签

2、choose标签 相当于switch标签

3、foreach标签,相当于for

4、set标签,可以保存数据

具体用法如下:

<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>if标签</title>
</head>
<body>
<c:if test="false">
    <h2>mmmm</h2>
</c:if>

<%
    List list = new ArrayList();
    list.add("zzz");
    request.setAttribute("list",list);

    request.setAttribute("number",17);
%>
<c:if test="${not empty list}">
    遍历集合
</c:if>
<br>
<c:if test="${number % 2 != 0}">
    ${number}是奇数
</c:if>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>choosr标签</title>
</head>
<body>
<%
    request.setAttribute("number",4);
%>
<c:choose>
    <c:when test="${number == 1}">周一</c:when>
    <c:when test="${number == 2}">周二</c:when>
    <c:when test="${number == 3}">周三</c:when>
    <c:when test="${number == 4}">周四</c:when>
    <c:when test="${number == 5}">周五</c:when>
    <c:when test="${number == 6}">周六</c:when>
    <c:when test="${number == 7}">周日</c:when>

    <c:otherwise>周八</c:otherwise>
</c:choose>

</body>
</html>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>foreach标签</title>
</head>
<body>
<c:forEach begin="1" end="10" var="i" step="1">
    ${i}<br>
</c:forEach>
<hr>
<%
    List list = new ArrayList();
    list.add("a");
    list.add("b");
    list.add("c");

    request.setAttribute("list",list);
%>
<c:forEach var="i" items="${list}" varStatus="s">
    ${s.index} ${s.count} ${i}<br>
</c:forEach>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>set标签</title>
</head>
<body>
保存之前:${requestScope.abc}
<c:set var="abc" scope="request" value="hhh"></c:set>
保存之后:${requestScope.abc}
</body>
</html>

 

 

 

 

  

 

posted on 2022-01-19 17:57  跨越&尘世  阅读(53)  评论(0编辑  收藏  举报