jsp自省将string转换为date
程序员文章站
2024-02-03 12:49:10
...
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); } }
在web.xml中配置
<servlet> <servlet-name>editorRegister</servlet-name> <servlet-class>com.xuguang.af.web.servlet.RegisterEditorServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet>
上一篇: 脚本牛人
推荐阅读