JSP 2.0的20项Action元素
目前的jsp2.0规范中,主要有20项Action Elements: 一.用来存取JavaBean: <jsp:useBean> <jsp:setProperty> <jsp:getProperty> 二.jsp1.2中原有的action元素 <jsp:include> <jsp:forward> <jsp:param> <jsp:plugin> <jsp:params> <jsp:fallback> 三.主要用在JSP Document之中 <jsp:root> <jsp:declaration> <jsp:scriptlet> <jsp:expression> <jsp:text> <jsp:output> 四.主要用来动态产生XML元素标签的值 <jsp:attribute> <jsp:body> <jsp:element> 五.主要用在Tag File中 <jsp:invoke> <jsp:doBody> 定义XML元素标签本体内容 <%@ page contentType="text/html; charset=Big5" %> <html> 这是包含进来的内容 include之后的显示,前面内容是include进来的 <%@ page contentType="text/html; charset=Big5" %> <%@ page contentType="text/html; charset=Big5" %> 这是一个密码检查程式
元素名称
简单描述
元素名称
简单描述
<jsp:useBean>
存取JavaBean
<jsp:declaration>
主要用在JSP Document之中
<jsp:setProperty>
存取JavaBean
<jsp:scriptlet>
主要用在JSP Document之中
<jsp:getProperty>
存取JavaBean
<jsp:expression>
主要用在JSP Document之中
<jsp:include>
包含静态或动态的网页文件
<jsp:text>
主要用在JSP Document之中
<jsp:forward>
网页转向
<jsp:output>
主要用在JSP Document之中
<jsp:param>
提供key/value的资讯
<jsp:attribute>
定义XML元素属性或标签属性值
<jsp:plugin>
在浏览器中播放或显示一个物件
<jsp:body>
<jsp:params>
通常和plugin搭配使用
<jsp:element>
动态定义XML元素标签值
<jsp:fallback>
通常和plugin搭配使用
<jsp:incoke>
主要用在Tag File之中
<jsp:root>
主要用在JSP Document之中
<jsp:doBody>
主要用在Tag File之中
(1) 简单include动作: 将hello.htm包含进主网页内,程式码:<jsp:include page="hello.htm" />
"include"的使用范列--主程式
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=big5">
<title>include的使用范列</title>
</head>
<body>
<font color="yellow">
<jsp:include page="hello.htm" /> <==不要忘了"/"
<p>include之后的显示,前面内容是include进来的</p>
<p>主网页的内容显示</p>
</font>
</body>
</html>
"include"的使用范列--被包含的程式(hello.htm)
<head>
<meta http-equiv="content-type" content="text/html; charset=big5">
<title>被include的档案--hello.htm</title>
</head>
<body>
<font color="red">
<center>
<p>这是包含进来的内容</p>
<p>档名是hello.htm</p>
<p>请考虑用include的方便性</p>
<p>可避免程式码的重覆key-in</p>
</center>
</font>
</body>
</html>
执行结果
档名是hello.htm
请考虑用include的方便性
可避免程式码的重覆key-in
主网页的内容显示
(2)除了include外,还包含传递参数值:程式码如下
<jsp:include page="check.jsp"> ?????????????????????????????????????<==注意结尾并没有"/",代表include标签并未结束
<jsp:param name="username" value="hyh" /> ?????????????<==注意结尾的"/"
<jsp:param?name="password"?value="123456"/>????????<==注意结尾的"/"
</jsp:include> ????????????????????????????????????????????????????????????????<==include标签的结束
"include"+"传递参数"的使用范列--主程式
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=big5">
<title>include的使用范列</title>
</head>
<body>
<font color="yellow">
<p>这是一个密码检查程式</p>
<jsp:include page="check.jsp">
<jsp:param name="username" value="hyh" />
<jsp:param name="password" value="123456" />
</jsp:include>
<p>include之后的显示,前面内容是include进来的</p>
<p>主网页的内容显示</p>
</font>
</body>
</html>
"include"的使用范列--被包含的程式(check.jsp)
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=big5">
<title>密码检查程式</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("hyh")) out.println("使用者名称正确<br>");
else out.println("使用者名称错误<br>");
if (password.equals("123456")) out.println("密码正确<br>");
else out.println("密码错误<br>");
%>
<p>返回主程式</p>
</body>
</html>
执行结果
使用者名称正确
密码正确
返回主程式
include之后的显示,前面内容是include进来的
主网页的内容显示
(1) <jsp:forward page="hello.htm" />:将网页重导到"hello.htm"
(2) <jsp:forward page="hello.htm">
<jsp:param name="username" value="hyh" />
</jsp:forward>:将参数username的值设为hyh,并传递到hello.htm
附注一:HTML亦有网页重导的语法:
<meta http-equiv=REFRESH CONTENT=1;url=result.jsp><center>显示完后网页重导!</center>
以上的命令将於显示"显示完后网页重导!"后的1秒之后,网页将被重导到"result.jsp"
附注二:JSP另有重导网页的命令response.sendRedirect("网页路径与名称或url");
response.sendRedirect("http://hyh.mis.dwu.edu.tw"); ==>将网页重导到"学习与服务"网站
附注三:respone另有一种写法修正网页标头达到重导网的目的response.setHeader("Refresh","5;URL=result.jsp")
经5秒后将网页重导到result.jsp,用法和附注一是相同的,只是利用response来改变标头的值
注意:用<jsp:forward>可传递参数,但较耗系统资源,而附注的二种方法只可重导,无法传递参数
<jsp:params>用来传递多个参数给Applet或Bean,而<jsp:fallback>将一段文字於Applet或Bean无法启动时显示在萤幕上,例如:
<jsp:plugin type="applet" code="test.class" codebase="/jsp_dir"> //指定被执行的物件是Applet,而Java类别的名称为"test.class"
//而test.class是在"/jsp_dir"的目录中
<jsp:params> //开始参数传递
<jsp:param name="username" value="hyh" /> //将参数username的值hyh传递给Applet test.class
</jsp:params> //结束参数传递
<jsp:fallback>
<p>Unable to start plugin</p> //若Applet无法启动,将"Unable to start plugin"显示在萤幕上
</jsp:fallback>
</jsp:plugin>
(1)简单用法:
<jsp:element name="hyh"> // ?XML元素标签名称为"hyh"
</jsp:element> // ?输出结果为<hyh></hyh>
(2)结合<jsp:attribute>:
<jsp:element name="userlabel">
<jsp:attribute name="userdefine">My_Definition</jsp:attribute>
<jsp:body>My_Context</jsp:body>
</jsp:element>
以上的执行结果如下:
<userlabel userdefine="My_Definition">My_Context</userlable>
没有评论:
发表评论