Java Tools

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            83 隨筆 :: 0 文章 :: 16 評(píng)論 :: 0 Trackbacks
          大家總是不喜歡看到<%和%>在jsp頁(yè)面上,其實(shí)我們可以使用比較優(yōu)雅的xml方式來(lái)表達(dá)
          1,隱含的注釋
          JSP語(yǔ)法:<%-- 內(nèi)容 --%>
          XML語(yǔ)法:無(wú)
          2,變量或函數(shù)的聲明
          JSP語(yǔ)法:<%! 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語(yǔ)法:
          <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,表達(dá)式
          JSP語(yǔ)法:<%=expression%>
          例如:
          <%=Math.round(Math.random*100)%>
          XML語(yǔ)法:
          <jsp:expression>
             expression
          </jsp:expression>
          例如:
          <jsp:expression>
             Math.round(Math.random*100)
          </jsp:expression>

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

          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"
            顯示時(shí)間:<jsp:expression>dateStr</jsp:expression>
          </div>
          posted on 2007-07-29 10:39 和田雨 閱讀(266) 評(píng)論(0)  編輯  收藏 所屬分類: JSP
          主站蜘蛛池模板: 通山县| 阳新县| 建瓯市| 湾仔区| 油尖旺区| 龙里县| 图木舒克市| 饶阳县| 安达市| 洪湖市| 炉霍县| 余庆县| 米脂县| 甘谷县| 韩城市| 惠东县| 新巴尔虎左旗| 郓城县| 高唐县| 交城县| 宜兰市| 青州市| 南陵县| 都兰县| 常山县| 礼泉县| 大洼县| 永年县| 深圳市| 宁夏| 莱芜市| 大名县| 宁都县| 宣威市| 盐山县| 铜陵市| 扎鲁特旗| 当涂县| 衢州市| 凌海市| 扎赉特旗|