JSP日期格式化源代碼

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://class3g.com" prefix="class3g" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!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>
 <class3g:rq1>2011-11-11</class3g:rq1>
 <class3g:rq2>2011-11-11</class3g:rq2>
  </body>
</html>


package class3g.web.zy;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.BodyContent;import javax.servlet.jsp.tagext.BodyTagSupport;public class RQ2 extends BodyTagSupport{@Overridepublic int doEndTag() throws JspException {BodyContent bc = this.getBodyContent();String content = bc.getString().toString();String[] results = content.split("-");for(String each:results){JspWriter out = this.pageContext.getOut();try {out.write(each);out.write("<br>");} catch (IOException e) {// TODO Auto-generated catch blockthrow new RuntimeException();}}return this.EVAL_PAGE;}@Overridepublic int doStartTag() throws JspException {return this.EVAL_BODY_BUFFERED;}}

<?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>there are custom tags of class3g</description>
    <tlib-version>1.0</tlib-version>
    <short-name>class3g</short-name>
    <uri>http://class3g.com</uri>
    
    <tag>
        <description>demo1</description>
        <name>rq1</name>
        <tag-class>class3g.web.zy.RQ1</tag-class>
        <body-content>scriptless</body-content>
    </tag>
    
    <tag>
        <description>demo1</description>
        <name>rq2</name>
        <tag-class>class3g.web.zy.RQ2</tag-class> 
        <body-content>scriptless</body-content>
    </tag>
   
</taglib>


 
package class3g.web.zy;

import java.io.IOException;
import java.io.StringWriter;

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


public class RQ1 extends SimpleTagSupport{

	@Override
	public void doTag() throws JspException, IOException {
		JspFragment jf = this.getJspBody();
		StringWriter sw = new StringWriter();
		jf.invoke(sw);
		
		String str = sw.toString();
		
		String[] results = str.split("-");
		for(String each:results){
			JspWriter out = this.getJspContext().getOut();
			out.write(each);
			out.write("<br>");
		}
	}	
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章