jsp usebean

JSP provides a bunch of standard action tags that we can use for specific tasks such as working with java bean objects, including other resources, forward the request to another resource etc.

JSP提供了一堆标准动作标签,我们可以将它们用于特定任务,例如使用java Bean对象(包括其他资源),将请求转发到另一个资源等。

JSP动作标签 (JSP action tags)

The list of standard JSP action elements are given below.

下面给出了标准JSP动作元素的列表。

JSP Action Description
jsp:include To include a resource at runtime, can be HTML, JSP or any other file
jsp:useBean To get the java bean object from given scope or to create a new object of java bean.
jsp:getProperty To get the property of a java bean, used with jsp:useBean action.
jsp:setProperty To set the property of a java bean object, used with jsp:useBean action.
jsp:forward To forward the request to another resource.
jsp:text To write template text in JSP page.
jsp:element To define the XML elements dynamically.
jsp:attribute To define the dynamically generated XML element attributes
jsp:body To define the dynamically generated XML element body
jsp:plugin To generate the browser-specific code that makes an OBJECT or EMBED tag for the Java plugin.
JSP动作 描述
jsp:include 要在运行时包含资源,可以是HTML,JSP或任何其他文件
jsp:useBean 从给定范围获取Java Bean对象或创建Java Bean的新对象。
jsp:getProperty 要获取Java bean的属性,请与jsp:useBean操作一起使用。
jsp:setProperty 设置与jsp:useBean操作一起使用的java bean对象的属性。
jsp:转发 将请求转发到另一个资源。
jsp:text 在JSP页面中编写模板文本。
jsp:element 动态定义XML元素。
jsp:属性 定义动态生成的XML元素属性
jsp:body 定义动态生成的XML元素主体
jsp:插件 生成特定于浏览器的代码,为Java插件创建OBJECT或EMBED标签。

Mostly in JSP programming, we use jsp:useBean, jsp:forward and jsp:include action. So we will focus on these action elements only.

通常在JSP编程中,我们使用jsp:useBeanjsp:forwardjsp:include操作。 因此,我们将仅关注这些行动要素。

JSP useBean (JSP useBean)

We can use jsp:useBean like below in JSP pages to get the bean object.

我们可以在JSP页面中使用如下所示的jsp:useBean来获取bean对象。

<jsp:useBean id="myBeanAttribute" class="com.journaldev.MyBean" scope="request" />

In the above example, JSP container will first try to find the myBeanAttribute in the request scope but if it’s not existing then it will create the instance of MyBean and then assign it to the myBeanAttribute id variable in JSP and sets it as an attribute to the request scope.

在上面的示例中,JSP容器将首先尝试在请求范围内找到myBeanAttribute ,但如果不存在,它将创建MyBean的实例,然后将其分配给JSP中的myBeanAttribute id变量,并将其设置为请求范围。

Once the bean is defined in JSP, we can get its properties using jsp:getProperty action like below.

一旦在JSP中定义了bean,我们就可以使用jsp:getProperty操作获取其属性,如下所示。

<jsp:getProperty name="myBeanAttribute" property="count" />

Notice that name attribute in jsp:getProperty should be same as id attribute in the jsp:useBean action. JSP getProperty action is limited because we can’t get the property of a property, for example if MyBean has a property that is another java bean, then we can’t use JSP action tags to get it’s value, for that we have JSP EL.

注意,jsp:getProperty中的name属性应该与jsp:useBean操作中的id属性相同。 JSP getProperty操作受到限制,因为我们无法获取属性的属性,例如,如果MyBean具有另一个Java bean的属性,那么我们就无法使用JSP操作标签来获取其值,因为我们拥有JSP EL

We can use jsp:setProperty to set the property values of a java bean like below.

我们可以使用jsp:setProperty来设置Java bean的属性值,如下所示。

<jsp:setProperty name="myBeanAttribute" property="count" value="5" />

If we want to set the property only if jsp:useBean is creating a new instance, then we can use jsp:setProperty inside the jsp:useBean to achieve this, something like below code snippet.

如果仅在jsp:useBean创建新实例时才想设置属性,则可以在jsp:useBean内使用jsp:setProperty来实现此目的,如下面的代码片段所示。

<jsp:useBean id="myBeanAttribute" class="com.journaldev.MyBean" scope="request">
<jsp:setProperty name="myBeanAttribute" property="count" value="5" />
</jsp:useBean>

Most of the times we code in terms of interfaces, so if we have Person interface and Employee implementation and we are setting a request attribute like below.

大多数情况下,我们根据接口进行编码,因此,如果我们具有Person接口和Employee实现,并且正在设置如下所示的request属性。

Person person = new Employee();
request.setAttribute("person", person);

Then we can use type attribute with jsp:useBean to get the java bean object like below.

然后我们可以将type属性与jsp:useBean一起使用,以获取如下所示的java bean对象。

<jsp:useBean id="person" type="Person" class="Employee" scope="request" />

If we don’t provide scope attribute value in jsp:useBean, it’s defaulted to page scope.

如果我们不在jsp:useBean中提供范围属性值,则默认为页面范围。

If we want to set the Java Bean properties from request parameters, we can use param attribute like below.

如果要通过请求参数设置Java Bean属性,可以使用如下所示的param属性。

<jsp:useBean id="myBeanAttribute" class="com.journaldev.MyBean" scope="session">
<jsp:setProperty name="myBeanAttribute" property="id" param="empID" />
</jsp:useBean>

If property and param attribute values are same, we can skip the param attribute. For example if request parameter name is also id then we can simply write:

如果属性和参数属性值相同,则可以跳过参数属性。 例如,如果请求参数名称也是id,那么我们可以简单地编写:

<jsp:useBean id="myBeanAttribute" class="com.journaldev.MyBean" scope="session">
<jsp:setProperty name="myBeanAttribute" property="id" />
</jsp:useBean>

If all the request parameter names match with the java bean properties, then we can simply set bean properties like below.

如果所有请求参数名称都与java bean属性匹配,那么我们可以像下面这样简单地设置bean属性。

<jsp:useBean id="myBeanAttribute" class="com.journaldev.MyBean" scope="session">
<jsp:setProperty name="myBeanAttribute" property="*" />
</jsp:useBean>

JSP包括 (JSP include)

We can use jsp:include action to include another resource in the JSP page, earlier we saw how we can do it using JSP include Directive.

我们可以使用jsp:include动作在JSP页面中包含另一个资源,之前我们已经了解了如何使用JSP include Directive做到这一点。

Syntax of jsp:include action is:

jsp:include操作的语法为:

<jsp:include page="header.jsp" />

The difference between JSP include directive and include action is that in include directive the content to other resource is added to the generated servlet code at the time of translation whereas with include action it happens at runtime.

JSP include指令和include动作之间的区别在于,include指令中的其他资源的内容在转换时会添加到生成的servlet代码中,而include动作会在运行时发生。

We can pass parameters to the included resource using jsp:param action like below.

我们可以使用jsp:param操作将参数传递给包含的资源,如下所示。

<jsp:include page="header.jsp"><jsp:param name="myParam" value="myParam value" />
</jsp:include>

We can get the param value in the included file using JSP Expression Language.

我们可以使用JSP表达式语言在包含的文件中获取param值。

JSP转发 (JSP forward)

We can use jsp:forward action tag to forward the request to another resource to handle it. Its syntax is like below.

我们可以使用jsp:forward action标签将请求转发到另一个资源来处理它。 它的语法如下。

<jsp:forward page="login.jsp" />

That’s all for a quick roundup of JSP action tags. We will look into JSP Standard Tag Library (JSTL) in the future post.

这就是快速汇总JSP操作标签的全部内容。 我们将在以后的文章中探讨JSP标准标记库(JSTL)。

翻译自: https://www.journaldev.com/2082/jsp-action-tags-jsp-usebean-include-forward

jsp usebean

jsp usebean_JSP动作标签– jsp useBean,包含,转发相关推荐

  1. JSP基础--动作标签

    JSP基础--动作标签 JSP动作标签 1 JSP动作标签概述 动作标签的作用是用来简化Java脚本的! JSP动作标签是JavaWeb内置的动作标签,它们是已经定义好的动作标签,我们可以拿来直接使用 ...

  2. JSP(六)动作标签

    一.六大动作标签(JSP标签) 作用:JSP动作标签利用XML语法格式的标记来控制Servlet引擎的行为.利用JSP动作可以动态地插入文件.重用JavaBean组 件.把用户重定向到另外的页面.为J ...

  3. JSP动作标签useBean--jsp:useBean 创建一个对象

    这个标签是笔者认为最重要的标签. 首先介绍 Java Bean 的特点: 可以实现代码服用,易编写,易维护,易使用,可以在任何有 JVM 的机器上使用而不许重新编译. 1. 编写 JavaBean 的 ...

  4. jsp怎么操作html标签,JSP、HTML标签

    JSP 标准标签库(JSTL) JSP 标准标签库(JSTL) JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. JSTL支持通用的.结构化的任务,比如迭代,条件判 ...

  5. JSP常用动作元素有哪些?作用是什么?

    JSP 动作元素有哪些?作用是什么? jsp的标准动作元素有十种之多,但是常用的就以下这六种 jsp:include:在页面被请求的时候引入一个文件. jsp:useBean:寻找或者实例化一个Jav ...

  6. 【HM】第11课:JSTL标签+JSP开发模式

    <pre> day11 昨天内容回顾 (1)jsp *jsp的三个指令 *jsp里面九个内置对象 request response session config application e ...

  7. JSP param动作

    当使用 <jsp:include> 动作标记引入的是一个能动态执行的程序时,如 Servlet 或 JSP 页面,可以通过使用 <jsp:param> 动作标记向这个程序传递参 ...

  8. jsp:setProperty和jsp:getProperty动作到底怎么用举例?

    2.jsp:setProperty和jsp:getProperty动作: jsp:useBean动作获得Bean实例之后,要设置Bean的属性可以通过jsp:setProperty动作进行.读取Bea ...

  9. 003_Jsp动作标签

    一. 介绍3个Jsp动作标签 1. <jsp:include page=""></jsp:include> 2. <jsp:param value=& ...

最新文章

  1. Nature:如何做一篇肠道菌群免疫的顶级文章
  2. Flutter开发之《头条 Flutter iOS 混合工程实践》笔记(54)
  3. python递归出口怎么写_Python进阶 —— 递归
  4. 安装 | Visual Studio Community 2015与OpenCV的安装及下载链接
  5. 基于DotNet构件技术的企业级敏捷软件开发平台 AgileEAS.NET - 系统架构
  6. python常见模块命令(os/sys/platform)
  7. 文献记录(part48)--Vector of Locally and Adaptively Aggregated Descriptors for Image Feature ...
  8. 《统计会犯错——如何避免数据分析中的统计陷阱》—第2章置信区间的优势
  9. matlab6.0序列号,MFC软件获取USB设备的制造商、产品、序列号
  10. 什么是利用计算机化的知识进行自动推理,基于实例模型的知识推理及其在自动阅卷系统中的应用...
  11. c# 数据库操作学习
  12. ssd trim linux,linux – 使用SSD上的BtrFS验证TRIM支持
  13. Eclipse Java快捷键
  14. MATLAB求解线性规划问题
  15. 读书笔记 - 《六神磊磊读唐诗》
  16. Java将JSON对象或JSON数组转list对象
  17. matlab 开启并行,Matlab并行(持续更新)
  18. 目标检测之YOLOX: Exceeding YOLO Series in 2021
  19. java.util.concurrent.RejectedExecutionException异常
  20. 哀其不幸,怒其不争!

热门文章

  1. 关于shell读取文件打印时展开通配符
  2. linq里的select和selectmany操作 投影运算
  3. Repository 仓储,你的归宿究竟在哪?(三)-SELECT 某某某。。。
  4. Cortex - M3 一些基础知识
  5. *args和**kargs
  6. cv::KeyPoint中response的介绍
  7. Ros编译 找不到package
  8. Android功能点(一)——判断网络是否真正连通
  9. php常考面试题,面试常见的几道PHP面试题
  10. echarts x轴加箭头,ECharts X轴(xAxis)