在我们接收页面传来的数据时,这些数据都是以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;

}

}

页面输入的数据格式转换类:BaseAction(经常使用于Struts框架中)的更多相关文章

  1. struts框架中OGNL表达式的使用之jsp页面获取action中的属性值

    在jsp页面中获取action中的值: 1.写一个action类OgnlAction类: 需要注意的地方: 如果在aciton中直接使用ognl表达式,将值存储的值栈中,是不能通过跳转将值传到jsp页 ...

  2. 完善GDAL与OpenCV间的数据格式转换与影像分块读写

    本博客为原创内容,未经博主允许禁止转载,商用,谢谢. 一.前言 关于GDAL与openCV间的数据格式转换,在我之前的博客中已有简要说明,这里,由于最近工作上经常用到openCV里的函数进行图像处理, ...

  3. (转载)OC学习篇之---Foundation框架中的NSDirctionary类以及NSMutableDirctionary类

    昨天学习了Foundation框架中NSArray类和NSMutableArray类,今天来看一下Foundation框架中的NSDirctionary类,NSMutableDirctionary类, ...

  4. C#日期转换类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te ...

  5. php 网站中文简体繁体转换类

    php 网站中文简体繁体转换类 <?php /* * define zh convert functions * 2017-4-28 use str_replace for speed * zh ...

  6. JavaBean组件<jsp:forward>动作<jsp:param>动作登录页面输入用户名和密码,然后进入检查页面判断是否符合要求,符合要求跳转到成功界面,不符合要求返回登录界面,显示错误信息。

    JavaBean组件 JavaBean组件实际是一种java类.通过封装属性和方法成为具有某种功能或者处理某个业务的对象. 特点:1.实现代码的重复利用.2.容易编写和维护.3.jsp页面调用方便. ...

  7. DataTable与DTO对象的简易转换类

    在web开发过程中,有时候为了数据传输的方便,比如:后台需要更新前端的ViewModel,此时我们定义一个与前端ViewModel结构一样的DTO对象,从数据层获取数据后,将数据封装成DTO然后序列化 ...

  8. 【转】【WPF】WriteableBitmap应用及图片数据格式转换

    使用 WriteableBitmap 类基于每个框架来更新和呈现位图.这对于生成算法内容(如分形图像)和数据可视化(如音乐可视化工具)很有用. WriteableBitmap 类使用两个缓冲区.“后台 ...

  9. zw版【转发·台湾nvp系列Delphi例程】Delphi 使用 HALCON库件COM控件数据格式转换

    zw版[转发·台湾nvp系列Delphi例程]Delphi 使用 HALCON库件COM控件数据格式转换 Delphi 使用 HALCON库件COM控件数据格式转换,与IHObjectX接口有关 va ...

随机推荐

  1. outline

    a标签 两种button按钮  默认带有一个虚线 outline  当他们被单击 和 激活以后   outline和border 很类似 ,但是有不同 1.outline 不能针对特定的边赋值 ,也就 ...

  2. 解决Cordova安装Cannot find module 'bplist-parser'问题

    安装完cordova出现找不到必须的模块 出现原因不明,不过如果出现这个问题,我们遇到缺什么模块就安装什么模块就可以了.如图所示

  3. Java 核心内容相关面试题【4】

    spingmvc 和 structs的区别 我们用struts2时采用的传统的配置文件的方式,并没有使用传说中的0配置. spring3 mvc可以认为已经100%零配置了(除了配置spring mv ...

  4. python相关资料

    http://blog.jobbole.com/59535/ http://www.nryoung.org/blog/2013/2/28/python-threading/ http://blog.j ...

  5. 基于MyBatis的数据服务接口

    背景 作为软件系统开发,数据操作是系统开发不可避免的一个重要组成部分.因为其重要性围绕着数据操作也出现了众多框架.成熟框架是为了普适众多数据操作要求的,因此为了更好的实现技术落地,需要对框架进行丰富和 ...

  6. 裴波那契查找详解 - Python实现

    裴波那契查找(Fibonacci Search)是利用黄金分割原理实现的查找方法. 斐波那契查找的核心是: 1.当key == a[mid]时,查找成功: 2.当key < a[mid]时,新的 ...

  7. Fedora 23建立wifi热点(Android手机可用)

    在ubuntu14.04下使用ap-hotspot,速度还不错.但是在15.04下就用不了了,不知为啥.现在使用fedora23,在学校还是挺需要给手机连wifi的,于是google看看ap-hots ...

  8. 照虎画猫写自己的Spring——依赖注入

    前言 上篇<照虎画猫写自己的Spring>从无到有讲述并实现了下面几点 声明配置文件,用于声明需要加载使用的类 加载配置文件,读取配置文件 解析配置文件,需要将配置文件中声明的标签转换为F ...

  9. params SqlParameter[] commandParameters(转)

    C#代码  ExecuteReader(string connectionString, CommandType commandType, string commandText, params Sql ...

  10. gulp 小坑一个

    学习gulp的时候,看到很多人写到 gulp.task('styles', function() { return gulp.src('src/styles/main.scss') .pipe(sas ...