日期:2014-05-16 浏览次数:20370 次
package com.xuguang.af.util; import java.beans.PropertyEditorSupport; import java.text.SimpleDateFormat; import java.util.Date; /** * jsp useBean 自省时将string转换为date * 使用PropertyEditorManager.registerEditor(Date.class,DatePropertyEditor)注册 * @author ztw * */ public class DatePropertyEditor extends PropertyEditorSupport { private Date date; private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); public void setAsText(String str) { try { date = format.parse(str); } catch(Exception e) { date = null; } } public Date getValue() { return date; } }
package com.xuguang.af.web.servlet; import java.beans.PropertyEditorManager; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import com.xuguang.af.util.DatePropertyEditor; /** * 注册string转换为Date的方法 * @author ztw * */ public class RegisterEditorServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public void init() throws ServletException { PropertyEditorManager.registerEditor(Date.class, DatePropertyEditor.class); } }
<servlet> <servlet-name>editorRegister</servlet-name> <servlet-class>com.xuguang.af.web.servlet.RegisterEditorServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet>