📄 datepickertag.java
字号:
package net.java.workeffort.infrastructure.jsptags;import java.util.Locale;import javax.servlet.jsp.JspException;import net.java.htmltag.BaseInputTag;import org.apache.commons.lang.StringUtils;import org.springframework.context.MessageSource;import org.springframework.context.NoSuchMessageException;import org.springframework.ui.context.Theme;import org.springframework.web.util.ExpressionEvaluationUtils;/** * Custom date picker tag. * <p> * Uses the CalendarPopup.js javascript code written by Matt Kruse * (www.mattkruse.com). The javascript has been modified to produce xhtml * compliant code for the calendar and hide the select boxs which show through. * The tag supports i18n. * <p> * See workeffort.tld for attribute list. * <p> * The 'formName' attribute should *match* the 'name' attribute of the 'form' * tag in your jsp and is required for the javascript to set the value on the * appropriate text field. * <p> * The date format is picked up from message resources. The message resource * file should have an entry for 'dataFormat'. * <p> * The date picker image is picked up from the theme properties file. The file * should have an entry for 'datePicker.img' * <p> * Select boxes have a nasty way of showing through when overlayed by other * layers.The 'hideSelect' attribute hides these select boxes using javascript. * The default value for this attribute is 'true'. * <p> * I18n: Creates the javascript to internationalize date picker from information * in the message resources file. Sets month names, day headers and translation * for 'Today' Make sure that in the message resources file * datePicker.monthNames datePicker.dayHeaders and datePicker.todayText are set * appropriately if you want an Non English version. * <p> * Note: When the tag is used in multi row forms you may want to set the * 'hideSelect' parameter to 'false'. Other wise, the rendering of the form will * be slow due to the amount of work the javascript has to do. When the * 'hideSelect' parameter is false, design the layout in such a way that the * datePickers do not overlay 'select' boxes. * @author Antony Joseph */public class DatePickerTag extends BaseInputTag { // TAG ATTRIBUTES /** dateFormat. the date format */ private String formName; private String hideSelect = "true"; /** * @see javax.servlet.jsp.tagext.Tag#doStartTag() */ public int doStartTagInternal() throws JspException { formName = ExpressionEvaluationUtils.evaluateString("formName", formName, pageContext); hideSelect = ExpressionEvaluationUtils.evaluateString("hideSelect", hideSelect, pageContext); setReadonly(ExpressionEvaluationUtils.evaluateString("readonly", getReadonly(), pageContext)); setDisabled(ExpressionEvaluationUtils.evaluateString("disabled", getDisabled(), pageContext)); // set the type of the input element. setType("text"); StringBuffer js = new StringBuffer(); MessageSource messageSource = getMessageSource(); String div = createDivId(); String calendar = div + "Calendar"; String anchor = div + "Anchor"; // create the javacript snippet needed for each date picker. js.append("<script type=\"text/javascript\"> \n"); js.append("// <![CDATA[\n"); // for xhtml compliance js.append("var "); js.append(calendar); js.append("= new CalendarPopup('"); js.append(div); // The default is any select boxes which falls within the area of // the calendar will be hidden. if ("false".equals(hideSelect)) js.append("', 'false'); \n"); else js.append("'); \n"); datePickerI18n(js, messageSource, calendar); js.append(" // ]]>\n"); js.append("</script>\n"); if (getMaxlength() == null) setMaxlength(getSize()); js.append(renderInputElement()); js.append("\n<input type=\"image\" src=\""); js.append(getImageSrc()); js.append("\" onclick=\""); js.append(calendar); js.append(".select(document."); js.append(formName); js.append("[\'"); js.append(getProperty()); js.append("']"); js.append(",'"); js.append(anchor); js.append("','"); js.append(getDateFormat()); js.append("'); return false; \" id=\""); js.append(anchor); js.append("\""); // disable the button as needed if ("true".equals(getReadonly()) || "true".equals(getDisabled())) { js.append(" disabled=\"disabled\" "); } js.append("/>\n"); js.append("<div id=\""); js.append(div); js .append("\" style=\"position:absolute;visibility:hidden;background-color:white;layer-background-color:white;\"></div> \n"); write(js.toString()); return SKIP_BODY; } /** * Resets attribute values for tag reuse. */ public void release() { super.release(); formName = null; hideSelect = "true"; } /** * Creates the javascript to internationalize date picker from information * in the MessageResources bundle file. Sets month names, day headers and * translation for 'Today' Make sure that in the properties file * datePicker.monthNames datePicker.dayHeaders and datePicker.todayText are * set appropriately if you want an Non English version. * @param messageResources The message resource * @param calendar The calendar javascript variable name. * @return js The javascript code to internationalize the date picker. */ private void datePickerI18n(StringBuffer buffer, MessageSource messageSource, String calendar) { Locale locale = getRequestContext().getLocale(); try { // If the month names are present than we need to do i18n String monthNames = messageSource.getMessage( "datePicker.monthNames", null, locale); buffer.append(calendar); buffer.append(".setMonthNames("); buffer.append(monthNames); buffer.append(");"); buffer.append(calendar); buffer.append(".setDayHeaders("); buffer.append(messageSource.getMessage("datePicker.dayHeaders", null, locale)); buffer.append(");"); buffer.append(calendar); buffer.append(".setTodayText(\""); buffer.append(messageSource.getMessage("datePicker.todayText", null, locale)); buffer.append("\");"); } catch (NoSuchMessageException e) { // Not i18n for calender. Do nothing. } } /** * @return Returns the formName. */ public String getFormName() { return formName; } /** * @param formName The formName to set. */ public void setFormName(String formName) { this.formName = formName; } /** * @return Returns the hideSelect. */ public String getHideSelect() { return hideSelect; } /** * @param hideSelect The hideSelect to set. */ public void setHideSelect(String hideSelect) { this.hideSelect = hideSelect; } private String createDivId() { String property = getProperty(); return StringUtils.replaceChars(property, "[].", null) + "Div"; } private String getDateFormat() { return getMessageSource().getMessage("dateFormat", null, getRequestContext().getLocale()); } private String getImageSrc() { Locale locale = getRequestContext().getLocale(); Theme theme = getRequestContext().getTheme(); if (theme != null) return theme.getMessageSource().getMessage("datePicker.img", null, locale); else return null; } /** * @return Returns the errorStyleClass. */ public String getErrorStyleClass() { return (getStyleClass() == null) ? null : getStyleClass() + "Error"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -