页面输入的数据格式转换类:BaseAction(常用于Struts框架中)

在我们接收页面传来的数据时,这些数据都是以String类型接收的,所以要进行数据格式转换,这时候就可以统一为它们进行转换,而且在处理这些数据的类中可以继承ActionSupport类,然后让每一个接收数据并做业务处理的action类继承该类,并调用该类中的数据格式转换方法对接收的数据进行处理。

部分关键代码如下:


/**
 * 超类BaseAction 数据格式转换
 * @author 七录斋
 *
 */
public class BaseAction extends ActionSupport{


public void write(Object obj){
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
try {
response.getWriter().print(obj);
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 把相应的数据转换成整型数据
* @param value String类型  要转换的数据
* @return 整型  转换失败时返回null
*/
public Integer $int(String value){
try {
return Integer.parseInt(ServletActionContext.getRequest()
.getParameter(value));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 把相应的数据转换成Double类型的数据
* @param value String类型  要转换的数据
* @return Double类型  转换失败时返回null
*/
public Double $double(String value){
try {
return Double.parseDouble(ServletActionContext.getRequest()
.getParameter(value));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 把相应的数据转换成String类型的数据
* @param value String类型  要转换的数据
* @return String类型  转换失败时返回null
*/
public String $str(String value){
try {
return ServletActionContext.getRequest().getParameter(value);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 把相应的数据转换成BigDecimal类型的数据
* @param value String类型  要转换的数据
* @return BigDecimal类型  转换失败时返回null
*/
public BigDecimal $bigDecimal(String value){
try {
return new BigDecimal(ServletActionContext.getRequest()
.getParameter(value));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 把相应的数据转换成Date类型的数据
* @param value String类型  要转换的数据
* @return Date类型  转换失败时返回null
*/
public Date $date(String value){
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse(ServletActionContext.getRequest().getParameter(value));
} catch (Exception e) {
try {
return new SimpleDateFormat("yyyy-MM-dd")
.parse(ServletActionContext.getRequest().getParameter(value));
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}

/**
* 把相应的数据转换成Time类型的数据
* @param value String类型  要转换的数据
* @return Time类型  转换失败时返回null
*/
public Time $time(String value){
try {
return new Time(new SimpleDateFormat("HH:mm:ss")
.parse(ServletActionContext.getRequest()
.getParameter(value)).getTime());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 把相应的数据转换成Timestamp类型的数据
* @param value String类型  要转换的数据
* @return Timestamp类型  转换失败时返回null
*/
public Timestamp $timestamp(String value){
try {
return new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse(ServletActionContext.getRequest()
.getParameter(value)).getTime());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值