Java Tools

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            83 隨筆 :: 0 文章 :: 16 評論 :: 0 Trackbacks
          大家總是不喜歡看到<%和%>在jsp頁面上,其實我們可以使用比較優(yōu)雅的xml方式來表達
          1,隱含的注釋
          JSP語法:<%-- 內(nèi)容 --%>
          XML語法:無
          2,變量或函數(shù)的聲明
          JSP語法:<%! declaration;[declaration;]+... %>
          例如:
          <%! 
             int i=0;int a,b,c;
             Color red=new Color(255,0,0);
             private static final String OK="ok";
             public String getAnswer(){
                 return OK;
             }
          %>
          XML語法:
          <jsp:declaration>
              declartion;[declaration;]+...
          </jsp:declaration>
          例如:
          <jsp:declaration>
             int i=0;int a,b,c;
             Color red=new Color(255,0,0);
             private static final String OK="ok";
             public String getAnswer(){
                 return OK;
             }
          </jsp:declaration>
          3,表達式
          JSP語法:<%=expression%>
          例如:
          <%=Math.round(Math.random*100)%>
          XML語法:
          <jsp:expression>
             expression
          </jsp:expression>
          例如:
          <jsp:expression>
             Math.round(Math.random*100)
          </jsp:expression>

          4,代碼片斷
          JSP語法:<% your java code %>
          例如:
          <%
             String username=request.getParameter("username").trim();
             String password=request.getParameter("password").trim();
          %>
          XMl語法:
          <jsp:scriptlet>
             your java code
          </jsp:scriptlet>
          例如:
          <jsp:scriptlet>
             String username=request.getParameter("username").trim();
             String password=request.getParameter("password").trim();
          </jsp:scriptlet>
          5,指令:include
          JSP語法:<%@include file="relativeURL"%>
          例如:<%@include file="static_head.html"%>
          XML語法:<jsp:directive.include file="relativeURL"/>
          例如:<jsp:directive.include file="static_head.html"/>
          6,指令:page
          JSP語法:<%@page attribtes%>
          例如:<%@page import="java.util.HashMap"%>
          XML語法:<jsp:directive.page attribute/>
          例如:<jsp:directive.page import="java.util.HashMap"/>
          7,指令:taglib
          JSP語法:<%@taglib uri="URIForLibrary" prefix="tagPrefix"%>
          XML語法:無
          對于jsp操作而言,本來就是xml格式。
          下面使用xml表達方式書寫一個jsp頁面

          example.jsp

          <jsp:directive.page contentType="text/html;charset=GBK" />
          <jsp:directive.page import="java.util.*,java.text.*" />

          <jsp:declartion>
              public String getCustomDate(Date,date,String pattern){
                  SimpleDateFormat format=new SimpleDateFormat();
                  format.applyPattern(pattern);
                  return format.format(date);
              }
          </jsp:declartion>
          <jsp:scriptlet>
              Date date=(Date)request.getAttribute("date");
              String dateStr=getCustomDate(date,"yyyy-MM-dd,hh:mm:ss");
          </jsp:scriptlet>
          <div style="font-size:11pt"
            顯示時間:<jsp:expression>dateStr</jsp:expression>
          </div>
          posted on 2007-07-29 10:39 和田雨 閱讀(266) 評論(0)  編輯  收藏 所屬分類: JSP
          主站蜘蛛池模板: 扶余县| 西充县| 邢台县| 河东区| 巨野县| 密山市| 青河县| 阳信县| 绍兴市| 永修县| 墨竹工卡县| 鄂州市| 高要市| 双流县| 那坡县| 孟村| 宝山区| 土默特左旗| 铜川市| 云南省| 新龙县| 宣恩县| 颍上县| 顺平县| 大竹县| 南川市| 固镇县| 陇西县| 长沙县| 集安市| 大埔县| 呼图壁县| 桃源县| 定兴县| 胶南市| 石棉县| 澄城县| 庆城县| 大化| 黑龙江省| 台州市|