?? weekdaystag.java
字號:
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.taglibs.datetime;
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* JSP Tag <b>weekdays</b>, used to loop through the days of the week
* so that weekday names can be accessed by using the standard
* JSP <jsp:getProperty> tag.
* <p>
* The script variable of name <b>id</b> is availble only within the
* body of the <b>weekdays</b> tag.
* <p>
* Loops through all the weekdays.
* <p>
* If the optional attribute <b>locale</b> is true, the weekday names
* are formatted for the clients locale if known.
* <p>
* The optional attribute <b>localeRef</b> can be used to specify
* the name of a page, session, application, or request scope attribute
* of type java.util.Locale to use.
* <p>
* JSP Tag Lib Descriptor
* <p><pre>
* <name>weekdays</name>
* <tagclass>org.apache.taglibs.datetime.WeekdaysTag</tagclass>
* <teiclass>org.apache.taglibs.datetime.WeekdaysTEI</teiclass>
* <bodycontent>JSP</bodycontent>
* <info>Loop through all the days of the week.</info>
* <attribute>
* <name>id</name>
* <required>true</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* <attribute>
* <name>locale</name>
* <required>false</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* <attribute>
* <name>localeRef</name>
* <required>false</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* </pre>
*
* @author Glenn Nielsen
*/
public class WeekdaysTag extends BodyTagSupport
{
// Static constants
private static String PATTERN = "yyyy";
// weekdays tag attributes
private boolean locale_flag = false;
private String localeRef = null;
private String [] short_weekdays = null;
private String [] long_weekdays = null;
private int day = 0;
private int day_num = 1;
/**
* Initializes tag so it can loop through the weekdays of the year.
*
* @return EVAL_BODY_TAG
*/
public final int doStartTag() throws JspException
{
// Initialize variables
day = 0;
day_num = 1;
SimpleDateFormat sdf;
if( localeRef != null ) {
Locale locale = (Locale)pageContext.findAttribute(localeRef);
if( locale == null ) {
throw new JspException(
"datetime amPms tag could not find locale for localeRef \"" +
localeRef + "\".");
}
sdf = new SimpleDateFormat(PATTERN,locale);
} else if( locale_flag ) {
sdf = new SimpleDateFormat(PATTERN,
(Locale)pageContext.getRequest().getLocale());
} else {
sdf = new SimpleDateFormat(PATTERN);
}
DateFormatSymbols dfs = sdf.getDateFormatSymbols();
short_weekdays = dfs.getShortWeekdays();
long_weekdays = dfs.getWeekdays();
// Make sure we skip any blank weekday array elements
while( day < long_weekdays.length &&
(long_weekdays[day] == null || long_weekdays[day].length() == 0) )
day++;
if( day >= short_weekdays.length )
return SKIP_BODY;
pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
return EVAL_BODY_TAG;
}
/**
* Method called at end of each weekdays tag.
*
* @return EVAL_BODY_TAG if there is another weekday, or SKIP_BODY if there are no more weekdays
*/
public final int doAfterBody() throws JspException
{
// See if we are done looping through weekdays
day++;
day_num++;
if( day >= short_weekdays.length )
return SKIP_BODY;
// Make sure we skip any blank weekday array elements
while( day < long_weekdays.length &&
(long_weekdays[day] == null || long_weekdays[day].length() == 0) )
day++;
if( day >= short_weekdays.length )
return SKIP_BODY;
// There is another weekday, so loop again
return EVAL_BODY_TAG;
}
/**
* Method called at end of Tag
* @return EVAL_PAGE
*/
public final int doEndTag() throws JspException
{
pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
try
{
if(bodyContent != null)
bodyContent.writeOut(bodyContent.getEnclosingWriter());
} catch(java.io.IOException e)
{
throw new JspException("IO Error: " + e.getMessage());
}
return EVAL_PAGE;
}
/**
* Locale flag, if set to true, use weekday names
* for client's preferred locale if known.
*
* @param boolean either <b>true</b> or <b>false</b>
*/
public final void setLocale(boolean flag)
{
locale_flag = flag;
}
/**
* Provides a key to search the page context for in order to get the
* java.util.Locale to use.
*
* @param String name of locale attribute to use
*/
public void setLocaleRef(String value)
{
localeRef = value;
}
/**
* Returns the short name of the weekday.
* <p>
* <jsp:getProperty name=<i>"id"</i> property="shortWeekday"/>
*
* @return String - short name of the weekday
*/
public final String getShortWeekday()
{
return short_weekdays[day];
}
/**
* Returns the long name of the weekday.
* <p>
* <jsp:getProperty name=<i>"id"</i> property="weekday"/>
*
* @return String - long name of the weekday
*/
public final String getWeekday()
{
return long_weekdays[day];
}
/**
* Returns the number of the day of the week.
* <p>
* <jsp:getProperty name=<i>"id"</i> property="dayOfWeek"/>
*
* @return String - number of the day of the week
*/
public final String getDayOfWeek()
{
return "" + day_num;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -