发布于 2015-08-17 14:46:01 | 207 次阅读 | 评论: 0 | 来源: 网络整理

JSP动作使用XML语法结构来控制Servlet引擎的行为。您可以动态地插入文件,重用JavaBeans组件,用户转发到另一个页面,或生成HTML的Java插件。

只有一个用于操作元素语法,因为它符合XML标准:


<jsp:action_name attribute="value" />

动作要素基本上都是预定义的功能,而且已有下面的JSP操作:

语法 目的
jsp:include Includes a file at the time the page is requested
jsp:useBean Finds or instantiates a JavaBean
jsp:setProperty Sets the property of a JavaBean
jsp:getProperty Inserts the property of a JavaBean into the output
jsp:forward Forwards the requester to a new page
jsp:plugin Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin
jsp:element Defines XML elements dynamically.
jsp:attribute Defines dynamically defined XML element's attribute.
jsp:body Defines dynamically defined XML element's body.
jsp:text Use to write template text in JSP pages and documents.

常见的属性:

有两个属性是所有动作要素:id属性和scope属性。

  • Id attribute: id属性唯一标识该动作元素,并允许操作要在JSP页面内引用。如果Action创建一个对象的id值可以通过使用隐式对象引用它的实例的PageContext

  • Scope attribute: 此属性标识的Action元素的生命周期。 id属性和范围的属性有直接关系,如范围属性确定与该ID相关联的对象的寿命。 scope属性有四个可能的值: (a) page, (b)request, (c)session, 和(d) application.

<jsp:include> 动作

此动作可让您将文件插入正在生成的页面。其语法如下所示:


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

不同于include指令,它会插入这个文件当时的JSP页面转换成servlet,这个动作插入文件在页面被请求时。

以下是相关联的属性的列表,包括动作:

属性 描述
page The relative URL of the page to be included.
flush The boolean attribute determines whether the included resource has its buffer flushed before it is included.

例子:

让我们定义下面的两个文件(一个)date.jps及(b)main.jsp中,如下所示:

以下是date.jsp文件的内容:


<p>
   Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>

这里是main.jsp的文件的内容:


<html>
<head>
<title>The include Action Example</title>
</head>
<body>
<center>
<h2>The include action Example</h2>
<jsp:include page="date.jsp" flush="true" />
</center>
</body>
</html>

现在让我们保持在根目录下的所有这些文件,并且尝试访问main.jsp。这将显示的结果是这样的:

The include action 例子

Today's date: 12-Sep-2013 14:54:22

<jsp:useBean> 动作

useBean动作是相当具有通用性。它利用id和scope变量的现有对象首先搜索。如果没有找到一个对象,然后它试图创建指定的对象。

加载bean最简单的方法如下:


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

一旦一个bean类被加载,你可以用 jsp:setProperty 和jsp:getProperty 动作来修改和检索bean属性。

以下是useBean动作相关的属性列表:

属性 描述
class Designates the full package name of the bean.
type Specifies the type of the variable that will refer to the object.
beanName Gives the name of the bean as specified by the instantiate () method of the java.beans.Beans class.

让我们讨论 jsp:setProperty 和jsp:getProperty 提供有关这些行动的有效示例。

<jsp:setProperty> 动作

setProperty 动作设置一个bean的属性。 bean必须有这个动作之前,预先定义的。有使用setProperty动作两种基本方式:

使用jsp:setProperty 后,您可以使用 jsp:useBean 元素,如下所示: 


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

在这种情况下,在 jsp:setProperty无论是一个新的bean是否被实例化或现有的bean被发现执行。

第二个方面 jsp:setProperty 可以出现是一个 jsp:useBean元素中,如下所示:


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

在这里,在jsp:setProperty只执行,如果一个新的对象实例化,如果没有一个现有的被发现。

以下是setProperty动作相关的属性列表:

属性 描述
name Designates the bean whose property will be set. The Bean must have been previously defined.
property Indicates the property you want to set. A value of "*" means that all request parameters whose names match bean property names will be passed to the appropriate setter methods.
value The value that is to be assigned to the given property. The the parameter's value is null, or the parameter does not exist, the setProperty action is ignored.
param The param attribute is the name of the request parameter whose value the property is to receive. You can't use both value and param, but it is permissible to use neither.

<jsp:getProperty> 动作

使用getProperty动作用于检索一个给定的属性的值,并将其转换为字符串,并最终将其插入到输出。

使用getProperty动作仅具有两个属性,这两者都是必需的,简单的语法如下:


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

以下是与setProperty动作相关的必需的属性列表:

属性 描述
name The name of the Bean that has a property to be retrieved. The Bean must have been previously defined.
property The property attribute is the name of the Bean property to be retrieved.

例子:

让我们定义一个测试的bean,我们在我们的例子将使用:


/* File: TestBean.java */
package action;
 
public class TestBean {
   private String message = "No message specified";
 
   public String getMessage() {
      return(message);
   }
   public void setMessage(String message) {
      this.message = message;
   }
}

编译上面的代码生成TestBean.class文件,并确保在C复制到TestBean.classC:apache-tomcat-7.0.2webappsWEB-INFclassesaction 文件夹,并CLASSPATH变量也应设置到这个文件夹: 

现在使用中的main.jsp文件,该文件加载bean并设置/获取一个简单的String参数如下代码:


<html>
<head>
<title>Using JavaBeans in JSP</title>
</head>
<body>
<center>
<h2>Using JavaBeans in JSP</h2>
 
<jsp:useBean id="test" class="action.TestBean" />
 
<jsp:setProperty name="test" 
                    property="message" 
                    value="Hello JSP..." />
 
<p>Got message....</p>
 
<jsp:getProperty name="test" property="message" />
 
</center>
</body>
</html>

现在尝试访问main.jsp中,它会显示以下结果:

Using JavaBeans in JSP

Got message....
Hello JSP...

<jsp:forward> 动作

正向行动终止当前页面的动作,将请求转发给另一个资源,如静态页面,另一个JSP页面或Java Servlet。

这个动作的简单语法如下:


<jsp:forward page="Relative URL" />

以下是与向前有关的行动所需的属性列表:

属性 描述
page Should consist of a relative URL of another resource such as a static page, another JSP page, or a Java Servlet.

例子:

让我们再利用下列两个文件(一个)date.jps及(b)main.jsp中,如下所示:

以下是date.jsp文件的内容:


<p>
   Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>

这里是main.jsp的文件的内容:


<html>
<head>
<title>The include Action Example</title>
</head>
<body>
<center>
<h2>The include action Example</h2>
<jsp:forward page="date.jsp" />
</center>
</body>
</html>

现在让我们保持在根目录下的所有这些文件,并且尝试访问main.jsp。这将显示结果类似如下。在这里,它丢弃从主网页内容,并从只转发页面中显示的内容。

Today's date: 12-Sep-2010 14:54:22

<jsp:plugin> 动作

plugin动作是用来插入Java组件到一个JSP页面。它决定了浏览器的类型,并插入需要的<object>或<embed>标签。 

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

插件操作有对应于用来格式化Java组件常用的HTML标记几个属性。 <param>元素也可以用来将参数发送到applet或Bean。

下面是一个使用插件行为的典型的语法:


<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>

可以使用一些小程序,如果你有兴趣尝试此操作。一个新的元件,所述<fallback>元件,可用于指定要发送给用户的情况下的组件出现故障的错误的字符串。

<jsp:element> 动作

<jsp:attribute> 动作

<jsp:body> 动作

<jsp:element>,<jsp:attribute>和<jsp:body>动作来动态定义的XML元素。字动态是非常重要的,因为它意味着,XML元素可以在请求时在编译时生成的,而不是静态的。

下面是一个简单的例子来动态地定义XML元素:


<%@page language="java" contentType="text/html"%>
<html xmlns="http://www.w3c.org/1999/xhtml"
      xmlns:jsp="http://java.sun.com/JSP/Page">

<head><title>Generate XML Element</title></head>
<body>
<jsp:element name="xmlElement">
<jsp:attribute name="xmlElementAttr">
   Value for the attribute
</jsp:attribute>
<jsp:body>
   Body for XML element
</jsp:body>
</jsp:element>
</body>
</html>

这将产生在运行时下面的HTML代码:


<html xmlns="http://www.w3c.org/1999/xhtml"
      xmlns:jsp="http://java.sun.com/JSP/Page">
 
<head><title>Generate XML Element</title></head>
<body>
<xmlElement xmlElementAttr="Value for the attribute">
   Body for XML element
</xmlElement>
</body>
</html>

<jsp:text> 动作

<jsp:text>动作可以用来写在JSP页面和文档模板文本。以下是此动作的简单语法:


<jsp:text>Template data</jsp:text>

模 板的body不能包含其他元素,它只能包含文本和EL表达式(注:EL表达式将在后续章节中介绍)。请注意,在XML文件中,您不能使用表达式, 如 ${whatever gt 0} ,因为除了标志的更大的是非法的。相反,使用GT形式,如${whatever gt 0}或另一种是嵌入在CDATA节中的值。


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

如果你需要包含一个DOCTYPE声明,例如对于XHTML,还必须使用<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>动作。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务