jsp内置对象_JSP

jsp内置对象

If you want to interact with a JavaBeans component using the Action tag in a JSP page, you must first declare a bean. The <jsp:useBean> is a way of declaring and initializing the actual bean object. By bean we mean JavaBean component object. Syntax of <jsp:useBean> tag

如果要使用JSP页面中的Action标签与JavaBeans组件进行交互,则必须首先声明一个bean。 <jsp:useBean>是一种声明和初始化实际bean对象的方法。 Bean是JavaBean组件对象。 <jsp:useBean>标记的语法

<jsp:useBean id = "beanName" class = "className" 
              scope = "page | request | session | application">

Here the id attribute specifies the name of the bean. Scope attribute specify where the bean is stored. The class attribute specify the fully qualified classname.

在这里, id属性指定了bean的名称。 范围属性指定Bean的存储位置。 class属性指定完全限定的类名。

useBean Tag in JSP

Given a useBean declaration of following :

给定useBean声明以下内容:

<jsp:useBean id="myBean" class="PersonBean" scope="request" />

is equivalent to the following java code,

等效于以下Java代码,

PersonBean myBean = (PersonBean)request.getAttribute("myBean");
if(myBean == null)
{
   myBean = new PersonBean();
   request.setAttribute("myBean", myBean);
}

If jsp:useBean tag is used with a body, the content of the body is only executed if the bean is created. If the bean already exists in the named scope, the body is skipped.

如果将jsp:useBean标记与主体一起使用,则仅在创建bean时才执行主体的内容。 如果Bean已存在于命名范围中,则跳过主体。

实例时间 (Time for an Example)

In this example we will see how <jsp:useBean> standard tag is used to declare and initialize a bean object. We will use PersonBean class as JavaBean Component.

在本示例中,我们将看到<jsp:useBean>标准标记如何用于声明和初始化bean对象。 我们将使用PersonBean类作为JavaBean组件。

PersonBean.java

PersonBean.java

import java.io.Serializable;

public classPersonBean implements Serializable
{
 private String name;
  
  public PersonBean()
   {
    this.name="";
   }
   public void setName(String name)
   {
    this.name = name;
   }
   public String getName()
   {
    return name;
   }
}

hello.jsp

hello.jsp

<html>
    <head>
        <title>Welcome Page</title>
    </head>
    <jsp:useBean id="person" class="PersonBean" scope="request" />
  <body>
        //Use the bean here...  
  </body>
</html>

Here jsp:useBean declares a "person" bean in the jsp page which can be used there. How to use it, modify it, we will study in coming lessons.

在此, jsp:useBean在jsp页面中声明了一个“人” bean,可以在此处使用它。 如何使用,修改它,我们将在接下来的课程中进行学习。

翻译自: https://www.studytonight.com/jsp/usebean-tag.php

jsp内置对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值