愚僧

赢与输的差别通常是--不放弃

BlogJava 首页 新随笔 联系 聚合 管理
  23 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks

2013年2月27日 #



步骤:
1. 定义tld标签描述文件
2. 新建class继承SimpleTagSupport或者BodyTagSupport
3. taglib命令声明
4. 使用自定义标签

1. 定义tld标签描述文件custom_tag.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version
="2.0">
    <description>JSTL 1.1 core library</description>
    <display-name>JSTL core</display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>ct</short-name>
    <!-- 与 taglib 的 uri 对应 -->
    <uri>http://www.customtag.com/custom_tag</uri>
    <!-- 定义一个标签 -->
    <tag>
        <!-- 标签的名称 -->
        <name>date</name>
        <!-- 标签类 -->
        <tag-class>com.customtag.tags.DateTag</tag-class>
        <!-- 标签体 -->
        <body-content>empty</body-content>
        <attribute>
            <!-- 属性名称 -->
            <name>format</name>
            <!-- 是否必选 true:必选 -->
            <required>false</required>
            <!-- 是否允许使用表达式(EL), false:不能使用 -->
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>
注:
可参考jstl-[version].jar中META-INF下的c.tld文件

2. 新建DateTag继承SimpleTagSupport或者BodyTagSupport
package com.customtag.tags;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class DateTag extends SimpleTagSupport {
    
    @Override
    public void doTag() throws JspException, IOException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        PageContext pc = (PageContext) getJspContext();
        JspWriter out = pc.getOut();
        try{
            if(null != this.getValue()){
                out.print(sdf.format(new Date(this.getValue())));
            }else{
                out.print(sdf.format(new Date()));
            }
        }catch(IOException e){
            throw e;
        }catch(Exception e){
            out.print("");
        }
    }
    
    private String format="yyyy-MM-dd HH:mm:ss";
    
    private Long value = null;
    
    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public Long getValue() {
        return value;
    }

    public void setValue(Long value) {
        this.value = value;
    }
    
}

3. taglib命令声明
<%@taglib prefix="ct" uri="http://www.customtag.com/custom_tag"%>

4. 使用自定义标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    
String path = request.getContextPath();
    
String basePath = request.getScheme() + "://"
            
+ request.getServerName() + ":" + request.getServerPort()
            
+ path + "/";
%>
<%@taglib prefix="ct" uri="http://www.customtag.com/custom_tag"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        
-->
    </head>

    <body>
        自定义标签测试 : 
        <br>
        <ct:date/><br/>
        <ct:date format="MM/dd/yyyy"/><br/>
        <ct:date format="yyyy年MM月dd日 HH时mm分ss秒" value="<%=new Date().getTime() %>"/><br/>
    </body>
</html>

[运行结果]
自定义标签测试:
2013-03-04 16:18:29
03/04/2013
2013年03月04日 16时18分29秒
posted @ 2013-03-04 16:35 ywm 阅读(107) | 评论 (0)编辑 收藏


以下情况被认为 false :
  • boolean类型的FALSE
  • int类型的0
  • 浮点类型的0.0
  • 字符串"" 或者 "0"
  • 空的数组
  • 空的对象null
其他情况都认为是true






posted @ 2013-03-01 17:51 ywm 阅读(118) | 评论 (0)编辑 收藏


用于执行控制台命令

$output = `ls -al`;
echo "<pre>$output</pre>";

有对平台依赖性, 降低了php跨平台能力
posted @ 2013-03-01 16:30 ywm 阅读(85) | 评论 (0)编辑 收藏

用于忽略掉错误信息

<?php
//忽略包含文件时产生的错误
@include("inc.php");
//忽略连接mysql数据库出错产生的错误信息
$conn = @mysql_connect("localhost","username","password");
//忽略打开文件产生的错误信息
$fp  = @fopen("user.xml","w");
function test(){
return 10;
}
//忽略调用函数失败产生的错误信息
$number = @test();
?>
posted @ 2013-03-01 16:25 ywm 阅读(95) | 评论 (0)编辑 收藏


serialize : 序列表表格内容为字符串, 返回的是一个字符串
var serializeStr = $("form").serialize();
result : username=forrest&passwd=1234&gender=0&interest=swimming&interest=running&interest=readBook

serializeArray : 序列化表格元素 (类似 '.serialize()' 方法) 返回 JSON 数据结构数据
var fields = $("select, :radio").serializeArray();
jQuery.each( fields, function(i, field){
  $("#results").append(field.name + "=" +field.value + "; ");
});
result : username=forrest; passwd=1234; gender=0; interest=swimming; interest=running; interest=readBook; 
posted @ 2013-03-01 15:42 ywm 阅读(630) | 评论 (2)编辑 收藏


Class.forName("binary name");
加载并对类变量进行初始化
等价cl.loadClass("binary name").newInstance();

ClassLoader.loadClass("binary name");
ClassLoader cl = this.getClass().getClassLoader();
Class c = cl.loadClass("binary name");
Object obj = c.newInstance();
在newInstance时初始化
初始化时会比forName多产生一个obj对象

From : http://waryist.iteye.com/blog/131983
posted @ 2013-02-27 15:57 ywm 阅读(109) | 评论 (0)编辑 收藏


escape/unescape : 不能对uri编码, 会把 / ? @ 进行编码
encodeURIComponet/decodeURIComponent : 对URI参数编码, 会把 / 进行编码
encodeURI/decodeURI : 对URI编码

From : http://blog.163.com/free_for_all/blog/static/6871811201192441843281/
From : http://www.cnblogs.com/qiantuwuliang/archive/2009/07/19/1526687.html
posted @ 2013-02-27 11:44 ywm 阅读(104) | 评论 (0)编辑 收藏


uri : Uniform Resource Identifier 统一资源标识
url : Uniform Resource Locator   统一资源定位

异:
url是uri的一个子集
url可以用相对路径表示, url 只能用绝对路径表示

同:
url,uri 都能定位唯一资源

注:
[scheme:][//authority][path][?query][#fragment]
authority为[user-info@]host[:port]
相对路径和绝对路径看是否使用"scheme:"开头

From : http://www.cnblogs.com/gaojing/archive/2012/02/04/2413626.html
From : http://rebecca.iteye.com/blog/234724


posted @ 2013-02-27 10:59 ywm 阅读(113) | 评论 (0)编辑 收藏