JavaBean、EL表达式${ }、作用域-自动转换、常用方法

2024-08-31 23:08

本文主要是介绍JavaBean、EL表达式${ }、作用域-自动转换、常用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JavaBean
1.JavaBean本身就是一个类,属于Java的面向对象编程。

2.在JSP中如果要应用JSP提供的Javabean的标签来操作简单类的话,则此类必须满足如下的开发要求:

(1)所有的类必须放在一个包中,在WEB中没有包的是不存在的;

(2)所有的类必须声明为public class,这样才能够被外部所访问;

(3)类中所有的属性都必须封装,即:使用private声明;

(4)封装的属性如果需要被外部所操作,则必须编写对应的setter、getter方法;

(5)一个JavaBean中至少存在一个无参构造方法,此为JSP中的标签所使用。

如下就是一个JavaBean

package com.safly;
public class Customer {private Integer id;private String name;private int age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}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 Customer(){System.out.println("customer");}
}

jsp:useBean

<jsp:useBean id="customer" class="com.safly.Customer" scope="session"></jsp:useBean>

属性用法:
id 命名引用该Bean的变量。如果能够找到id和scope相同的Bean实例,jsp:useBean动作将使用已有的Bean实例而不是创建新的实例。
class 指定Bean的完整包名。
scope 指定Bean在哪种上下文内可用,可以取下面的四个值之一:page,request,session和application。
默认值是page,表示该Bean只在当前页面内可用(保存在当前页面的PageContext内)。
request表示该Bean在当前的客户请求内有效(保存在ServletRequest对象内)。
session表示该Bean对当前HttpSession内的所有页面都有效。
最后,如果取值application,则表示该Bean对所有具有相同ServletContext的页面都有效。

我们看一段代码:

<body><jsp:useBean id="customer" class="com.safly.Customer" scope="session"></jsp:useBean><jsp:setProperty property="age" value="10" name="customer" />age:<jsp:getProperty property="age" name="customer" /><%Customer customer1 = (Customer) session.getAttribute("customer");if (customer1 == null) {customer1 = (Customer) Class.forName("beans.Customer").newInstance();session.setAttribute("customer", customer1);}%><%customer1.setAge(10);%><%=customer1.getAge()%>
</body>

浏览器输入http://localhost:8080/day01/el.jsp
提交输出 age:10 10

EL表达式
el1.jsp

<body><form action="el.jsp" method="post">username:<input type="text" name="username" value="${param.username }"/><input type="submit" value="Submit"/></form>username:<%= request.getParameter("username") %><!-- el表达式 --><jsp:useBean id="customer" class="com.safly.Customer" scope="session"></jsp:useBean><jsp:setProperty property="age" value="12" name="customer"/><a href="el2.jsp">to el2.jsp</a>
</body>

el2.jsp

<body>age:${sessionScope.customer.age }age:${sessionScope.customer["age"] }<%Customer customer = (Customer)session.getAttribute("customer");out.print(customer.getAge());%>
</body>

http://localhost:8080/day01/el.jsp
这里写图片描述
http://localhost:8080/day01/el2.jsp
age:12 age:12 12

楼上el的隐藏对象的范围是sessionScope,另外还有pageScope、requestScope、applicationScope
他们基本上和pageContext、request、session、application一样

El的作用域–及其自动转换功能
el1.jsp

<body><jsp:useBean id="customer" class="com.safly.Customer" scope="session"></jsp:useBean><jsp:setProperty property="age" value="10" name="customer"/>age:<jsp:getProperty property="age" name="customer"/><a href="el2.jsp?score=89">To El2.jsp</a>
</body>

el2.jsp

<body><!-- getParameter -->score:${param.score}score:<%= request.getParameter("score")%>score:${param.score + 11 }score:<%= request.getParameter("score")+11 %><%Customer cust = new Customer();cust.setAge(25);request.setAttribute("customer",cust);%>age:${customer.age }age:${sessionScope.customer["age"] }<br>
</body>

浏览器输入
http://localhost:8080/day01/el.jsp
浏览器输出
age:10 To El2.jsp
点击上面一行中的超练级,浏览器地址栏变为如下:
http://localhost:8080/day01/el2.jsp?score=89
浏览器输入:
score:89 score:89 score:100 score:8911 age:25 age:10

大概说一下:
在el1.jsp中,将customer放到了session中

<jsp:useBean id="customer" class="com.safly.Customer" scope="session"></jsp:useBean>

在el2.jsp中从session中取出来customer对象的age属性

 age:${sessionScope.customer["age"] }

在el2.jsp中

    <%Customer cust = new Customer();cust.setAge(25);request.setAttribute("customer",cust);%>age:${customer.age }

是在customer
先从pageContext、request、session、application依次从小往大找
这里就是从request中获取的

El表达式的其他方法
el1.jsp

<body>
<jsp:useBean id="customer" class="com.safly.Customer" scope="session"></jsp:useBean><jsp:setProperty property="age" value="10" name="customer"/>age:<jsp:getProperty property="age" name="customer"/><a href="el2.jsp?score=89&name=A&name=B">To El2.jsp</a>
</body>

el2.jsp

<body><%ArrayList<String> names = new ArrayList<String>();names.add("abc");request.setAttribute("names",names);%>names is empty:${empty requestScope.names }names is empty:${empty requestScope.names2 }<!-- pageContext只能读取属性 -->sessinAttributeName:${pageContext.session.attributeNames}<br>sessionId:${pageContext.session.id }<br><%= request.getContextPath() %><br>contextPath:${pageContext.request.contextPath }<br>jsession:${cookie.JSESSIONID.name } -- ${cookie.JSESSIONID.value } <br>score:${param.score}<br>names:${paramValues.name[0]}<br><%= request.getParameterValues("name")[0].getClass().getName() %>
</body>

http://localhost:8080/day01/el.jsp
age:10 To El2.jsp
http://localhost:8080/day01/el2.jsp?score=89&name=A&name=B

names is empty:false names is empty:true sessinAttributeName:org.apache.catalina.util.Enumerator@1120aa6
sessionId:19A0C4DD75349287A283DBABA8EE7EF1
/day01
contextPath:/day01
jsession:JSESSIONID – 19A0C4DD75349287A283DBABA8EE7EF1
score:89
names:A
java.lang.String

这篇关于JavaBean、EL表达式${ }、作用域-自动转换、常用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

MyBatis常用XML语法详解

《MyBatis常用XML语法详解》文章介绍了MyBatis常用XML语法,包括结果映射、查询语句、插入语句、更新语句、删除语句、动态SQL标签以及ehcache.xml文件的使用,感兴趣的朋友跟随小... 目录1、定义结果映射2、查询语句3、插入语句4、更新语句5、删除语句6、动态 SQL 标签7、ehc

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三