页面输入的数据格式转换类:BaseAction(经常使用于Struts框架中)
2017-05-16 21:01
183 查看
在我们接收页面传来的数据时,这些数据都是以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 数据格式转换
* @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框架中)
- 有部分经常变化的数据的页面怎么使用页面缓存
- 一步步学习SPD2010--第十四章节--在Web页面使用控件(3)--验证用户数据输入
- struts2使用ajax实现页面和action之间通过json数据交互
- 输入int类型数据,返还A....AA....BC等,在使用POI导出Excel表格时导出公式时经常使用。
- 一步步学习SPD2010--第十四章节--在Web页面使用控件(3)--验证用户数据输入
- 在Action中查询出SQL SERVER表的数据,返回一个list。怎么在JSP页面中使用Struts标签,显示出表中的内容?(转)
- android intent和intent action大全 android intent和intent action大全 android 中intent是经常要用到的。不管是页面牵转,还是传递数据
- 点击按钮弹出新窗口,输入数据后返回并刷新页面
- 如何点击按钮弹出新窗口,输入数据后返回并刷新页面?
- JavaScript在页面间数据传输的使用
- Spring 架构下使用jstl在页面上展示map或者list(包括类型嵌套的数据)
- 使用Server.Transfer在页面之间传递数据
- 如何点击按钮弹出新窗口,输入数据后返回并刷新页面?
- 如何点击按钮弹出新窗口,输入数据后返回并刷新页面?(转自孟子E章)
- 在struts框架中使用Action作为资源的访问控制设计考虑
- JavaScript在页面间数据传输的使用
- 如何点击按钮弹出新窗口,输入数据后返回并刷新页面?
- 使用struts 怎么才能在执行完js校验页面的信息,发现不全的情况下,不让页面提交到Action中
- 在SQL语句insert into中,数据来自用户页面的输入和另一个表中的命令行写法!