`
勤业思行
  • 浏览: 82368 次
  • 性别: Icon_minigender_1
  • 来自: 贵阳
社区版块
存档分类
最新评论

JSP系列三:动作元素

    博客分类:
  • JSP
阅读更多

JSP动作元素:比较简单,但是一些不常用的在使用时一时想不起来。自己做一下备忘!

JSP动作元素可以用来动态地包含文件、网页跳转及使用JavaBean组件等。

JSP动作元素的一般语法是:

      <jsp:xxx />

或者

   <jsp:xxx> </jsp:xxx>

JSP动作元素可以分为以下五类:

第一类是与存取JavaBean有关:

<jsp:useBean>、<jsp:setProperty>、<jsp:getProperty>

 

第二类是JSP1.2就有的基本动作元素:     <jsp:include>,<jsp:forward>,<jsp:param>,<jsp:plugin>,<jsp:params>,<jsp:fallback>

 

第三类是JSP2.0新增的动作元素,主要与JSP document有关: <jsp:root>,<jsp:declaration>,<jsp:scriptlet>,<jsp:expressin>,<jsp:text>,<jsp:output>

 

第四类是JSP2.0新增的动作元素,主要用来动态生成XML元素标签的值:<jsp:attribute>,<jsp:body>,<jsp:element>

 

第五类是JSP2.0新增的动作元素,主要用在Tag File中:

<jsp:invoke>,<jsp:doBody>

 

一、<jsp:include>:用于包含静态和动态的文件。语法格式:

<jsp:include page="包含文件URL地址" flush="true|false">


 flush默认值为FALSE。

1、包含静态文件:只是单纯地把静态文件内容加到JSP页面中。不会进行任何处理。

2、包含动态文件:会先对动态文件进行处理,然后将处理结果加到JSP页面中。

例:includeJSP.jsp

 

 

 

<%@ page language="java" contentType="text/html;charset=gb2312"%> <html> <head> <title>包含动态文件</title> </head> <body> 使用jsp:include动态元素包含动态文件<br> 当前日期与时间:<jsp:include page="content.jsp"></jsp:include> </body> </html>


 

 被包含的JSP文件:content.jsp

<%@ page import="java.util.*"%> <%=new Date()%>


 

这时,将会将content.jsp页面的运行结果返回给includeJSP.jsp页面。

3、<jsp:include>和<%@ include%> 的区别:<%@ include %>指令元素只是将页面的内容静态地包含进来,如果被包含文件中有JSP代码,则会执行该代码。而<jsp:include>是不会执行静态文件中的JSP代码。

例:静态文件content.txt

<%@ page import="java.util.*"%> <%=new Date()%>


   主页面: includeJSP.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%> <html> <head> <title>包含动态文件</title> </head> <body> 使用jsp:include动态元素包含动态文件<br> 当前日期与时间:<jsp:includepage="content.txt"></jsp:include> 使用include指令元素包含一个包含JSP代码的文本文件<br> 当前日期与时间:<%@ include file="content2.txt"%> </body> </html>


 此时,页面的第一部分将直接输出content.txt文本中的内容:JSP代码。而第二部分将输出content.txt中的JSP代码执行后的结果:当前时间。

二、<jsp:forward>:执行页面的跳转。执行时会先执行<jsp:forward>前面的代码再执行跳转。

三、<jsp:param>:用于传递参数。一般与<jsp:include>、<jsp:forward>联合使用。语法格式为:

<jsp:param name="参数名" value="参数值" />


 1、与<jsp:include>使用

ParamInclude.jsp:把参数age的值19传递到<jsp:include>中的page中。

<%@ page language="java" contentType="text/html;charset=gb2312"%> <html> <head> <title>包含JSP文件并传递参数</title> </head> <body> 使用include动作元素包含一个包含JSP文件,并传递参数<br> <jsp:include page="contentDemo.jsp"> <jsp:param name="age" value="19"/> </jsp:include> </body> </html>


 

contentDemo.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%> <h2>被包含页</h2> <p>接受到的参数:</p> <% String strAge = request.getParameter("age"); // 接受参数 %> <%--输出参数内容--%> <%="age参数值为" + strAge %>


 

输出:age的参数值为19。

2、与<jsp:forward>联合使用:可以实现跳转页面的同时向跳转页面传递参数

ParamForward.jsp:将页面跳转到<jsp:forward>的page中,并把参数age的值19传递到该跳转页面。

<%@ page language="java" contentType="text/html;charset=gb2312"%> <html> <head> <title>跳转并传递参数</title> </head> <body> 使用forward动作元素跳转到另一个JSP文件,并传递参数<br> <jsp:forward page="ForwardedDemo.jsp"> <jsp:param name="age" value="19"/> </jsp:forward> </body> </html>


 

ForwardDemo.jsp:

<%@ page language="java" contentType="text/html;charset=gb2312"%> <html> <head> <title>跳转到的页面</title> </head> <body> <h2>跳转到的页面</h2> <p>接受到的参数:</p> <% String strAge = request.getParameter("age"); // 接受参数 %> <%--输出参数内容--%> <%="age参数值为" + strAge %> </body> </html>


 

四、<jsp:plugin>,<jsp:params>,<jsp:fallback>:这三个动作元素一般都是搭配使用的。

1、<jsp:plugin>:用来再JSP后总嵌入Java插件。比如Applet。<jsp:plugin>会自动根据浏览器版本替换<object>标签和<embed>标签。其中<object>用于HTML4.0版本,而<embed>用于HTML3.2版本。<jsp:plugin>的语法格式为:

<jsp:plugin type="bean|applet" name="Applet名称" code="Java类名" align="对齐方式" height="高度" width="宽度" hspace="水平间距" vspace="垂直间距" archive="预先加载的类列表" jreversion="JRE版本" iepluginurl="url" nspluginurl="url" codebase="Java类所在目录"> </jsp:plugin>


 例:JSPPlugin.jsp:包含了一个在当前目录下的Applet目录下的一个Clock.class的Applet。

<%@ page language="java" contentType="text/html;charset=gb2312"%> <html> <head> <title>执行Applet</title> </head> <body> <jsp:plugin code="Clock.class" codebase="Applet" type="applet"> <jsp:params> <jsp:param name="bgcolor" value="000000"/> <jsp:param name="fgcolor1" value="ff0000"/> <jsp:param name="fgcolor2" value="00ff00"/> </jsp:params> <jsp:fallback> 不能加载该Applet </jsp:fallback> </jsp:plugin> </body> </html>


 

通过反编译得到的Clock.class为:

import java.applet.Applet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Clock extends Applet implements Runnable { private volatile Thread timer; private int lastxs; private int lastys; private int lastxm; private int lastym; private int lastxh; private int lastyh; private SimpleDateFormat formatter; private String lastdate; private Font clockFaceFont; private Date currentDate; private Color handColor; private Color numberColor; private int xcenter = 80; private int ycenter = 55; public void init() { this.lastxs = (this.lastys = this.lastxm = this.lastym = this.lastxh = this.lastyh = 0); this.formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault()); this.currentDate = new Date(); this.lastdate = this.formatter.format(this.currentDate); this.clockFaceFont = new Font("Serif", 0, 14); this.handColor = Color.blue; this.numberColor = Color.darkGray; try { setBackground( new Color(Integer.parseInt(getParameter("bgcolor"), 16))); } catch (NullPointerException localNullPointerException) { } catch (NumberFormatException localNumberFormatException) { } try { this.handColor = new Color(Integer.parseInt(getParameter("fgcolor1"), 16)); } catch (NullPointerException localNullPointerException1) { } catch (NumberFormatException localNumberFormatException1) { } try { this.numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"), 16)); } catch (NullPointerException localNullPointerException2) { } catch (NumberFormatException localNumberFormatException2) { } resize(300, 300); } public void update(Graphics g) { int s = 0; int m = 10; int h = 10; this.currentDate = new Date(); this.formatter.applyPattern("s"); try { s = Integer.parseInt(this.formatter.format(this.currentDate)); } catch (NumberFormatException n) { s = 0; } this.formatter.applyPattern("m"); try { m = Integer.parseInt(this.formatter.format(this.currentDate)); } catch (NumberFormatException n) { m = 10; } this.formatter.applyPattern("h"); try { h = Integer.parseInt(this.formatter.format(this.currentDate)); } catch (NumberFormatException n) { h = 10; } int xs = (int)(Math.cos(s * 3.141592653589793D / 30.0D - 1.570796326794897D) * 45.0D + this.xcenter); int ys = (int)(Math.sin(s * 3.141592653589793D / 30.0D - 1.570796326794897D) * 45.0D + this.ycenter); int xm = (int)(Math.cos(m * 3.141592653589793D / 30.0D - 1.570796326794897D) * 40.0D + this.xcenter); int ym = (int)(Math.sin(m * 3.141592653589793D / 30.0D - 1.570796326794897D) * 40.0D + this.ycenter); int xh = (int)(Math.cos((h * 30 + m / 2) * 3.141592653589793D / 180.0D - 1.570796326794897D) * 30.0D + this.xcenter); int yh = (int)(Math.sin((h * 30 + m / 2) * 3.141592653589793D / 180.0D - 1.570796326794897D) * 30.0D + this.ycenter); this.formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy"); String today = this.formatter.format(this.currentDate); g.setFont(this.clockFaceFont); g.setColor(getBackground()); if ((xs != this.lastxs) || (ys != this.lastys)) { g.drawLine(this.xcenter, this.ycenter, this.lastxs, this.lastys); g.drawString(this.lastdate, 5, 125); } if ((xm != this.lastxm) || (ym != this.lastym)) { g.drawLine(this.xcenter, this.ycenter - 1, this.lastxm, this.lastym); g.drawLine(this.xcenter - 1, this.ycenter, this.lastxm, this.lastym); } if ((xh != this.lastxh) || (yh != this.lastyh)) { g.drawLine(this.xcenter, this.ycenter - 1, this.lastxh, this.lastyh); g.drawLine(this.xcenter - 1, this.ycenter, this.lastxh, this.lastyh); } g.setColor(this.numberColor); g.drawString(today, 5, 125); g.drawLine(this.xcenter, this.ycenter, xs, ys); g.setColor(this.handColor); g.drawLine(this.xcenter, this.ycenter - 1, xm, ym); g.drawLine(this.xcenter - 1, this.ycenter, xm, ym); g.drawLine(this.xcenter, this.ycenter - 1, xh, yh); g.drawLine(this.xcenter - 1, this.ycenter, xh, yh); this.lastxs = xs; this.lastys = ys; this.lastxm = xm; this.lastym = ym; this.lastxh = xh; this.lastyh = yh; this.lastdate = today; this.currentDate = null; } public void paint(Graphics g) { g.setFont(this.clockFaceFont); g.setColor(this.handColor); g.drawArc(this.xcenter - 50, this.ycenter - 50, 100, 100, 0, 360); g.setColor(this.numberColor); g.drawString("9", this.xcenter - 45, this.ycenter + 3); g.drawString("3", this.xcenter + 40, this.ycenter + 3); g.drawString("12", this.xcenter - 5, this.ycenter - 37); g.drawString("6", this.xcenter - 3, this.ycenter + 45); g.setColor(this.numberColor); g.drawString(this.lastdate, 5, 125); g.drawLine(this.xcenter, this.ycenter, this.lastxs, this.lastys); g.setColor(this.handColor); g.drawLine(this.xcenter, this.ycenter - 1, this.lastxm, this.lastym); g.drawLine(this.xcenter - 1, this.ycenter, this.lastxm, this.lastym); g.drawLine(this.xcenter, this.ycenter - 1, this.lastxh, this.lastyh); g.drawLine(this.xcenter - 1, this.ycenter, this.lastxh, this.lastyh); } public void start() { this.timer = new Thread(this); this.timer.start(); } public void stop() { this.timer = null; } public void run() { Thread me = Thread.currentThread(); while (this.timer == me) { try { Thread.currentThread(); Thread.sleep(100L); } catch (InterruptedException localInterruptedException) { } repaint(); } } public String getAppletInfo() { return "Title: A Clock \nAuthor: Rachel Gollub, 1995 \nAn analog clock."; } public String[][] getParameterInfo() { String[][] info = { { "bgcolor", "hexadecimal RGB number", "The background color. Default is the color of your browser." }, { "fgcolor1", "hexadecimal RGB number", "The color of the hands and dial. Default is blue." }, { "fgcolor2", "hexadecimal RGB number", "The color of the second hand and numbers. Default is dark gray." } }; return info; } }


 2、<jsp:params>:用来给Bean或者Applet传递参数,一般搭配<jsp:param>使用。语法格式:

<jsp:plugin> <jsp:params> <jsp:param name="name" value="value" /> </jsp:params> </jsp:plugin>


 例子在上面的JSPPlugin.jsp中。

3、<jsp:fallback>:用于指定当浏览器不支持或无法启动Bean或Applet时,在页面上打印输出的错误提示信息。其语法格式为:

<jsp:plugin> <jsp:fallback>错误提示信息</jsp:fallback> </jsp:plugin>


 

 

0
1
分享到:
评论

相关推荐

    JSP 动作元素

    动作元素基本上都是预定义的函数,JSP规范定义了一系列的标准动作,它用JSP作为前缀,可用的标准动作元素如下: 语法 描述 jsp:include 在页面被请求的时候引入一个文件。 jsp:useBean 寻找或者实例化一个...

    Bootstrap与jQuery UI框架设计(前端开发工程师系列)

    2.2 表单元素 2.2.1 文本标签和容器标签 2.2.2 文本输入框 2.2.3 textarea输入区域 2.2.4 HTML5新增标签 操作案例:制作信息收集页面 3 jQuery Mobile API 3.1 jQuery:Mobile API 3.2 jQuery Mobile事件 3.2.1 页面...

    java面试题

    jsp常用动作? 答:jsp:include 引入一个文件 jsp:useBean 实例化JavaBean jsp:setProperty 设置JavaBean属性 jsp:getProperty 输出JavaBean属性 jsp:forward 转发 CTS、CLS、CLR分别作何解释? 答:CTS 通用...

    超级有影响力霸气的Java面试题大全文档

     动态INCLUDE用jsp:include动作实现 &lt;jsp:include page="included.jsp" flush="true" /&gt;它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数。 静态INCLUDE用include伪码实现,定不会检查所含文件...

    java 面试题 总结

    动态INCLUDE用jsp:include动作实现 &lt;jsp:include page="included.jsp" flush="true" /&gt;它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数。 静态INCLUDE用include伪码实现,定不会检查所含文件的...

    jpivot学习总结.doc

    hideMemberIf 在什么时候不隐藏该成员,可选的值有三个: Never 、 IfBlankName 、 IfParentName approxRowCount 该属性可以用来提高性能,可以通过指定一个数值以减少判断级别、层次、维度基数的时间,该属性在...

    二十三种设计模式【PDF版】

    访问者在进行访问时,完成一系列实质性操作,而且还可以扩展. 设计模式引言 设计面向对象软件比较困难,而设计可复用的面向对象软件就更加困难。你必须找到相关的对象,以适当的粒度将它们归 类,再定义类的接口和...

Global site tag (gtag.js) - Google Analytics