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

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。