jsp action elements (six)

JSP action elements

JSP and instruction elements is different, JSP elements play a role in the operation request processing stage. JSP action element is written using XML syntax.

Using JSP file operation can be dynamically inserted reuse JavaBean components, redirect the user to another page, HTML code is generated as a Java plug.

Action element is only one syntax, which conforms to the XML standard:

<jsp:action_name attribute="value" />

Action element basically predefined functions, JSP standard specification defines a set of actions, which with JSP prefixed actions available standard elements as follows:

grammar description
jsp:include The introduction of a file when the page is requested.
jsp:useBean Or looking for instantiating a JavaBean.
jsp:setProperty Set JavaBean properties.
jsp:getProperty The output of a JavaBean properties.
jsp:forward The request to a new page.
jsp:plugin Generate OBJECT or EMBED tag for the Java plug-in according to the type of browser.
jsp:element Define dynamic XML elements
jsp:attribute Set XML element attributes dynamically defined.
jsp:body Setting XML element content dynamically defined.
jsp:text The use of written text in JSP pages and document templates

Common attributes

All the action elements has two attributes: id attribute and scope attributes.

  • id attribute:

    id attribute uniquely identifies the action elements can be referenced in a JSP page. id value of the action elements created can be invoked by PageContext.

     

  • scope attribute:

    This attribute is used to identify the life cycle of the operation element. id attribute and scope attributes directly related, scope attribute defines a lifetime associated with the object id. The scope attribute has four possible values: (a) page, (b) request, (c) session, and (d) application.

     


<Jsp: include> action element

<Jsp: include> action element used to contain static and dynamic files. The action specified file into the page being generated. Syntax is as follows:

<Jsp: include page = "relative URL address" flush = "true" />

 It has been introduced include instructions, which was introduced in the JSP file is converted into the file when the Servlet, and here jsp: include different actions, the time when the document is inserted in the requested page.

The following is a list of include action-related properties.

Attributes description
page It included in the page relative URL address.
flush Boolean attribute defines whether the refresh buffer before containing the resource.

Examples

We define the following two files  date.jsp  and  main.jsp , the code looks like this:

date.jsp file code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<p>
   Today's date is: <.% = (New java.util.Date ()) toLocaleString ()%>
</p>

main.jsp file code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<Title> novice tutorial (runoob.com) </ title>
</head>
<body>

<H2> include an operation example </ h2>
<jsp:include page="date.jsp" flush="true" />

</body>
</html>

Now more than two files in the root directory of the server, access main.jsp file. The results show the following:

Examples include actions

Today's date: 2016-6-25 14:08:17

<Jsp: useBean> action element

jsp: useBean  action is used to load a JavaBean to be used in a JSP page.

This feature is very useful because it allows us to take advantage of Java reusable components.

jsp: useBean action simplest syntax is:

<jsp:useBean id="name" class="package.class" />

After the class is loaded, we can both jsp: to modify and retrieve bean properties getProperty action: setProperty and jsp.

The following is a list of useBean action related attributes.

Attributes description
class Specify the full package name of Bean.
type Specify the type of the object reference variables.
beanName () Method to specify the name of the Bean by java.beans.Beans instantiate.

Before give a specific example, let's take a look at the jsp: setProperty and jsp: getProperty action elements:


<Jsp: setProperty> action element

jsp: setProperty to set the instantiated attributes of the object Bean, in two ways. First, you can jsp: Use jsp useBean element outside (rear): the setProperty, as follows:

<jsp:useBean id="myName" ... />
...
<jsp:setProperty name="myName" property="someProperty" .../>

At this point, regardless of jsp: useBean is to find an existing Bean, or create a new Bean instance, jsp: setProperty will be executed. The second is to use jsp: setProperty into jsp: useBean internal elements, as follows:

<jsp:useBean id="myName" ... >
...
   <jsp:setProperty name="myName" property="someProperty" .../>
</jsp:useBean>

At this point, jsp: setProperty will only be performed in the new Bean instance, if you are using an existing instance is not performed jsp: setProperty.

jsp: setProperty operation has the following four properties, as follows:

Attributes description
name The name attribute is required. It pledged to set the properties of which Bean.
property property attribute is required. It indicates which property to be set. There is a special cases: If the value of the property is "*" indicates the names of all request parameters and attributes names matched Bean will be passed to the corresponding attribute set method.
value value attribute is optional. This property is used to specify the value of the property Bean. The data string by standard methods in the target class valueOf automatically converted to digital, boolean, Boolean, byte, Byte, char, Character. For example, boolean and Boolean type property values ​​(such as "true") by Boolean.valueOf conversion, and attribute values ​​int Integer type (such as "42") conversion by Integer.valueOf. param value and can not be used, but either one may be used.
param param is optional. It specifies which parameters as a request attribute value Bean. If the current request has no parameters, then do not do anything, the system does not pass null method to set Bean properties. So you can make your own Bean provide default attribute values, only when the request parameter explicitly specify a new value to modify the default property values.

<Jsp: getProperty> action element

jsp: getProperty Action Bean extracts specified attribute value, converted to a string, and then output. Syntax is as follows:

<jsp:useBean id="myName" ... />
...
<jsp:getProperty name="myName" property="someProperty" .../>

The following table is the attribute associated with the getProperty:

Attributes description
name Bean attribute name to be retrieved. Bean must have been defined.
property It represents the value of the property to extract Bean

Examples

The following examples we used the Bean:

package com.runoob.main;

public class TestBean {
   private String message = "rookie course";
 
   public String getMessage() {
      return(message);
   }
   public void setMessage(String message) {
      this.message = message;
   }
}

The above examples compiled file TestBean.java:

$ javac TestBean.java

编译完成后会在当前目录下生成一个 TestBean.class 文件, 将该文件拷贝至当前 JSP 项目的 WebContent/WEB-INF/classes/com/runoob/main 下( com/runoob/main 包路径,没有需要手动创建)。

下面是一个 Eclipse 中目录结构图:

下面是一个很简单的例子,它的功能是装载一个Bean,然后设置/读取它的message属性。

 

现在让我们在main.jsp文件中调用该Bean:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<h2>Jsp 使用 JavaBean 实例</h2>
<jsp:useBean id="test" class="com.runoob.main.TestBean" />
 
<jsp:setProperty name="test" 
                    property="message" 
                    value="菜鸟教程..." />
 
<p>输出信息....</p>
 
<jsp:getProperty name="test" property="message" />

</body>
</html>

浏览器访问,执行以上文件,输出如下所示:


<jsp:forward> 动作元素

 jsp:forward动作把请求转到另外的页面。jsp:forward标记只有一个属性page。语法格式如下所示:

<jsp:forward page="相对 URL 地址" />

以下是forward相关联的属性:

属性 描述
page page属性包含的是一个相对URL。page的值既可以直接给出,也可以在请求的时候动态计算,可以是一个JSP页面或者一个 Java Servlet.

实例

以下实例我们使用了两个文件,分别是: date.jsp 和 main.jsp。

date.jsp 文件代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<p>
   今天的日期是: <%= (new java.util.Date()).toLocaleString()%>
</p>

main.jsp文件代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<h2>forward 动作实例</h2>
<jsp:forward page="date.jsp" />
</body>
</html>

现在将以上两个文件放在服务器的根目录下,访问main.jsp文件。显示结果如下:

今天的日期是: 2016-6-25 14:37:25

<jsp:plugin>动作元素

jsp:plugin动作用来根据浏览器的类型,插入通过Java插件 运行Java Applet所必需的OBJECT或EMBED元素。

如果需要的插件不存在,它会下载插件,然后执行Java组件。 Java组件可以是一个applet或一个JavaBean。

plugin动作有多个对应HTML元素的属性用于格式化Java 组件。param元素可用于向Applet 或 Bean 传递参数。

以下是使用plugin 动作元素的典型实例:

<jsp:plugin type="applet" codebase="dirname" code="MyApplet.class"
                           width="60" height="80">
   <jsp:param name="fontcolor" value="red" />
   <jsp:param name="background" value="black" />
 
   <jsp:fallback>
      Unable to initialize Java Plugin
   </jsp:fallback>
 
</jsp:plugin>

如果你有兴趣可以尝试使用applet来测试jsp:plugin动作元素,<fallback>元素是一个新元素,在组件出现故障的错误时发送给用户错误信息。


<jsp:element> 、 <jsp:attribute>、 <jsp:body>动作元素

<jsp:element> 、 <jsp:attribute>、 <jsp:body>动作元素动态定义XML元素。动态是非常重要的,这就意味着XML元素在编译时是动态生成的而非静态。

以下实例动态定义了XML元素:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<jsp:element name="xmlElement">
<jsp:attribute name="xmlElementAttr">
   属性值
</jsp:attribute>
<jsp:body>
   XML 元素的主体
</jsp:body>
</jsp:element>
</body>
</html>

浏览器访问以下页面,输出结果如下所示:


<jsp:text>动作元素

<jsp:text>动作元素允许在JSP页面和文档中使用写入文本的模板,语法格式如下:

<jsp:text>模板数据</jsp:text>

以上文本模板不能包含重复元素,只能包含文本和EL表达式(注:EL表达式将在后续章节中介绍)。请注意,在XML文件中,您不能使用表达式如 ${whatever > 0},因为>符号是非法的。 你可以使用 ${whatever gt 0}表达式或者嵌入在一个CDATA部分的值。

<jsp:text><![CDATA[<br>]]></jsp:text>

如果你需要在 XHTML 中声明 DOCTYPE,必须使用到<jsp:text>动作元素,实例如下:

<jsp:text><![CDATA[<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">]]>
</jsp:text>
<head><title>jsp:text action</title></head>
<body>

<books><book><jsp:text>  
    Welcome to JSP Programming
</jsp:text></book></books>

</body>
</html>

你可以对以上实例尝试使用<jsp:text>及不使用该动作元素执行结果的区别。

发布了270 篇原创文章 · 获赞 52 · 访问量 7万+

Guess you like

Origin blog.csdn.net/LuckFairyLuckBaby/article/details/103120729
jsp