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中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

Java Thread中join方法使用举例详解

《JavaThread中join方法使用举例详解》JavaThread中join()方法主要是让调用改方法的thread完成run方法里面的东西后,在执行join()方法后面的代码,这篇文章主要介绍... 目录前言1.join()方法的定义和作用2.join()方法的三个重载版本3.join()方法的工作原