Java学习日记(day28)

2024-09-03 09:36
文章标签 java 学习 day28 日记

本文主要是介绍Java学习日记(day28),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、EL表达式

目的就是去掉JSP中的Java代码,都是以标签的形式表示,以标签的形式美工或者前端都可以修改,利于团队的合作。这套标签和Java语言无关。

JSP核心语法:JSP表达式 JSP脚本

JSP开发的原则:尽量在JSP页面少写甚至不写Java代码,Java代码放在Servlet。

使用EL表达式来代替JSP表达式

EL表达式的作用:向浏览器输出域对象(只能输出域对象)的变量或者表达式计算的结果。

语法:${变量或表达式} ${list}

JSP表达式:

EL表达式替代上面的脚本:${requestScope.name}

EL最主要的作用是获得四大域中的数据,格式${EL表达式}

  1. EL获得pageContext域中的值:${pageScope.key}; PageContext
  2. EL获得request域中的值:${requestScope.key};
  3. EL获得session域中的值:${sessionScope.key};
  4. EL获得application域中的值:${applicationScope.key}; ServletContext

${name} ${sessionScope.key}

从四个域对象中取某个值${}

依次从page域 < request域 < session域 < application域获取值,在某个域中找到不再往后找。

就是现在小的域范围找,找不到再去大的范围找.

1、获得普通字符串

2、获得Student对象的值

3、获得List的值

<body><%--${list}--%><%//JSP页面内置对象//pageContext、request、session、application(ServletContext)域对象,也是内置对象//1.普通字符串pageContext.setAttribute("name", "zhangsan");request.setAttribute("name", "lisi");//2.Student对象Student student = new Student(1, "zhansga", 23, "男");session.setAttribute("student", student);//3.List<Student>List<Student> list = new ArrayList<>();Student student1 = new Student(1, "zhangsan1", 23, "男");Student student2 = new Student(2, "zhangsan2", 23, "男");Student student3 = new Student(3, "zhangsan3", 23, "男");list.add(student1);list.add(student2);list.add(student3);application.setAttribute("list", list);%><%--JSP表达式--%><%=pageContext.getAttribute("name")%><br/><%=request.getAttribute("name")%><br/><%Student stu = (Student) session.getAttribute("student");%><%=stu.getName()%><br/><%=stu.getAge()%><br/><%=((Student) session.getAttribute("student")).getGender()%><br><%List<Student> stuList = (List<Student>) application.getAttribute("list");%><%=stuList.get(0).getName()%><hr/><%--EL表达式--%>${pageScope.name}<br/>${requestScope.name}<br/>${name}<br/>${sessionScope.student.name}<br/>${student.age}<br/>${list}<br/>${list[0].name}<br/></body>

二、JSTL标签

JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。jstl出现的目的同EL一样也是要代替JSP页面中的脚本代码。JSTL标准标准标签库有5个子库,但随着发展,目前常使用的是他的核心库

<%@ page import="com.situ.web.pojo.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head><title>Title</title>
</head>
<body><%--1、向域对象放数据--%><%pageContext.setAttribute("age", 23);session.setAttribute("gender", "男");%><c:set var="age" value="24" scope="request"></c:set>${age} <br/><hr/><%--2、条件判断--%><c:if test="${gender=='男'}">男</c:if><c:if test="${gender=='女'}">女</c:if><hr/><%--3、多条件判断--%><c:set var="score" value="78"></c:set><c:choose><c:when test="${score>=90&&score<=100}">优秀</c:when><c:when test="${score>=80&&score<90}">良好</c:when><c:when test="${score>=70&&score<80}">一般</c:when><c:when test="${score>=60&&score<70}">及格</c:when><c:otherwise>不及格</c:otherwise></c:choose><hr/><%--4、集合遍历List<Student>Map<String, String>Map<String, Student>--%><c:forEach begin="1" end="10" var="i" step="1">${i}</c:forEach><br/><%List<Student> list = new ArrayList<>();Student student1 = new Student(1, "zhangsan1", 23, "男");Student student2 = new Student(2, "zhangsan2", 23, "男");Student student3 = new Student(3, "zhangsan3", 23, "男");list.add(student1);list.add(student2);list.add(student3);application.setAttribute("list", list);%><c:forEach items="${list}" var="student">${student.id}--${student.name}--${student.age}--${student.gender} <br/></c:forEach><hr/><%--Map<String, String>--%><%Map<String, String> map = new HashMap<>();map.put("cn", "中国");map.put("us", "美国");request.setAttribute("map", map);%><c:forEach items="${map}" var="entry">${entry.key}--${entry.value}<br/></c:forEach><hr/><%--Map<String, Student>--%><%Map<String, Student> studentMap = new HashMap<>();studentMap.put("student1", student1);studentMap.put("student2", student2);studentMap.put("student3", student3);request.setAttribute("studentMap", studentMap);%><c:forEach items="${studentMap}" var="entry">${entry.key}--${entry.value.name}--${entry.value}<br/></c:forEach>
</body>
</html>

三、问题总结

JSP本质是Servlet

C:\Users\Gao\AppData\Local\JetBrains\IntelliJIdea2024.1\tomcat\ec76a933-0aaf-4bab-8eb7-d7c16c957743\work\Catalina\localhost\ROOT\org\apache\jsp

这篇关于Java学习日记(day28)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1132641

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06