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:useBean
, jsp:forward
和jsp:include
操作。 因此,我们将仅关注这些行动要素。
JSP useBean (JSP useBean)
We can use jsp:useBean like below in JSP pages to get the bean object.
我们可以在JSP页面中使用如下所示的jsp:useBean来获取bean对象。
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操作获取其属性,如下所示。
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的属性值,如下所示。
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来实现此目的,如下面的代码片段所示。
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对象。
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属性。
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,那么我们可以简单地编写:
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包括 (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操作的语法为:
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操作将参数传递给包含的资源,如下所示。
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标签将请求转发到另一个资源来处理它。 它的语法如下。
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