頁面輸入的資料格式轉換類:BaseAction(常用於Struts架構中)

來源:互聯網
上載者:User

標籤: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;
}
}


頁面輸入的資料格式轉換類:BaseAction(常用於Struts架構中)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.