Struts ActionForm中自定义转换器Converter(简单易懂)
来源:互联网 发布:凤姐绿卡怎么拿的 知乎 编辑:程序博客网 时间:2024/05/19 03:16
步骤:
--实现Converter接口
--重写convert方法
--注册转换器(servlet方式或plugin方式)
----servlet方式
------web.xml文件配置servlet
----plugin方式
------struts-config.xml中配置plugin
-------------------------------------------------------------------------------------------
实例如下:
为java.util.Date自定义一个转换器
实现Converter接口的TestUtilDateConverter.java
package com.yulin.struts;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
public class TestUtilDateConverter implements Converter {
//重写convert方法
public Object convert(Class type, Object value) {
if(value == null){
return new Date();
}
if(value instanceof Date){
return value;
}
if(value instanceof String){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse((String)value);
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
}
注册转换器(servlet方式或plugin方式)
以servlet方式注册转换器
package com.yulin.struts;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.commons.beanutils.ConvertUtils;
public class TestUtilDateConverterServlet extends HttpServlet {
@Override
public void init() throws ServletException {
//注册转换器
ConvertUtils.register(new TestUtilDateConverter(), java.util.Date.class);
}
}
----servlet方式--web.xml文件配置servlet
<servlet>
<servlet-name>TestUtilDateConverterServlet</servlet-name>
<servlet-class>com.yulin.struts.TestUtilDateConverterServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
以plugin方式注册转换器
package com.yulin.struts;
import javax.servlet.ServletException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
public class TestUtilDateConverterPlugin implements PlugIn {
public void destroy() {
}
public void init(ActionServlet arg0, ModuleConfig arg1)
throws ServletException {
//注册转换器
ConvertUtils.register(new TestUtilDateConverter(), java.util.Date.class);
}
}
----plugin方式--struts-config.xml中配置plugin
<plug-in className="com.yulin.struts.TestUtilDateConverterPlugin"/>
- Struts ActionForm中自定义转换器Converter(简单易懂)
- struts ActionForm 类型转换器
- 转换器(Converter)Struts 2.0中的魔术师
- 自定义Struts1转换器Converter
- 转换器(Converter)——Struts 2.0中的魔术师
- 转换器(Converter)——Struts 2.0中的魔术师
- 转换器(Converter)——Struts 2.0中的魔术师
- 转换器(Converter)——Struts 2.0中的魔术师
- 转换器(Converter)——Struts 2.0中的魔术师
- 转换器(Converter)——Struts 2.0中的魔术师
- 转换器(Converter)——Struts 2.0中的魔术师
- Struts ActionForm简单理解
- Struts自定义Converter
- springmvc的 自定义转换器(converter)实现方法
- 转换器(Converter)——Struts 2.0中的魔术师(Struts2中数据批量保存)[转别人的]
- 转换器(Converter)—Struts 2.0中的魔术师
- Struts中ActionForm小结
- Struts中的转换器:自定义转换器
- 前途迷惘
- Asp.net 利用Div固定表头+异步显示动态加载
- Web 设计师的 50 个超便利工具(下)
- 菜鸟入门:Java语言学习六大要点
- 今天在使用commons-dbutils时的,BeanListHandler处理赋值,有些值为空的错误
- Struts ActionForm中自定义转换器Converter(简单易懂)
- 敏捷编码
- Hudson – 搜索条
- 球迷的心声
- 快速排序 优化 (QuickSort)Java数据结构与算法
- Top Ten Habits of Successful Programmers
- 真正的程序员,请你站出来
- IT人士必去的10个网站
- 优秀程序员的十个习惯