2013年5月18日 星期六

h:inputText 的一般文字輸入

雖然只是一個簡單的文字輸入,它是一般表格填寫最常用的元件。有許多的變化使用情形:
第一部份我們先來看最簡單的使用(僅供教學參考,不適合在實際案例中使用):
JSF xhtml


結果頁 java bean 內容
/**
 * 表單輸入程式示範
 */
package jason.blogger.jsf;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;

/**
 * @author jason
 * 2013/5/19
 */
@ManagedBean(name = "appForm")
public class ApplyForm implements Serializable
{
 private static final long serialVersionUID = 1L;
    private String userId;   // 使用者代號
    private String userName; // 使用者名稱
    private String password; // 密碼
    private String gender;   // 性別
    private String city;     // 城市
 /**
  * @return the userId
  */
 public String getUserId()
 {
  return userId;
 }
 /**
  * @param userId the userId to set
  */
 public void setUserId(String userId)
 {
  this.userId = userId;
 }
 /**
  * @return the userName
  */
 public String getUserName()
 {
  return userName;
 }
 /**
  * @param userName the userName to set
  */
 public void setUserName(String userName)
 {
  this.userName = userName;
 }
 /**
  * @return the password
  */
 public String getPassword()
 {
  return password;
 }
 /**
  * @param password the password to set
  */
 public void setPassword(String password)
 {
  this.password = password;
 }
 /**
  * @return the gender
  */
 public String getGender()
 {
  return gender;
 }
 /**
  * @param gender the gender to set
  */
 public void setGender(String gender)
 {
  this.gender = gender;
 }
 /**
  * @return the city
  */
 public String getCity()
 {
  return city;
 }
 /**
  * @param city the city to set
  */
 public void setCity(String city)
 {
  this.city = city;
 }
 
}


在這個 h:inputText 案例中,呈現了一個完整而簡單的 JSF 概念。有以下幾點值得說明:
1. outputText 與 outputLabel 的不同:
  outputLabel 轉換成 html 時會變成 <label> 標籤,而 outputText 會直接輸出文字,這在意義上是不同的。

2.ManagedBean的使用:
   JSF 2.0 在 Java Class 中上方加上 @ManagedBean 就可以讓這個 Bean 變成 ManagedBean,並且指定 ManagedBean 的名字。(預設未指定 ManagedBean 名稱,則以 class 名稱第一個字母小寫為名,如上例若未指定 name 則 bean name 為 appForm)
未特別指定 Bean 的 Scope ,它預設為 request scoped。

3.commandButton 的基本頁面導向:
   JSF 2.0 CommandButton 的 action 屬性可以直接用字串指定要前往的頁面。 指定 /xhtml/resultPage 時,按下 commandButton 會被導到 xhtml 目錄下的 resultPage.jsf 頁面。

2013年5月11日 星期六

Calendar,Date與時間

程式中經常會處理到時間的問題。例如:要存取本月份的交易資料,如何指定本月一日零時零分到月底的23時59分59秒之間的資料,光是一個月有幾天就是個大問題。如果用字串不僅格式是問題,時間與時區換算都是問題。只用 Date()也很難處理。
Calendar 再配合 DateFormat 就是一個比較理想的解決方式。

        Calendar cal=Calendar.getInstance();
 int yr=cal.get(Calendar.YEAR);
 int mon=cal.get(Calendar.MONTH);
 int dt=cal.get(Calendar.DAY_OF_MONTH);
  
 cal.set(yr, mon,1,0,0,0);
 startTime=cal.getTimeInMillis();
 cal.add(Calendar.MONTH, 1);
 endTime=cal.getTimeInMillis()-1000;
 SimpleDateFormat format =
 new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");    
 System.out.println(format.format(startTime));
 System.out.println(format.format(endTime));

先用 Calendar 取得現在的時間,找出年月日,再利用它易於處理各種時間換算的特點。
最終,轉成毫秒(或Date )再利用DateFormat( SimpleDateFormat) 轉換輸出您想要的時間格式。

上例的執行結果如下:

 2013/05/01 00:00:00
 2013/05/31 23:59:59

Calendar 的 set 可以很容易的指定時間,如上例中的 cal.set(yr,mon,1,0,0,0); 直接設為當月一日的零時零分零秒。

Calendar 還有 add 可以很快的加(減)一段指定的時間。
   如上例中的 cal.add(Calendar.MONTH, 1);
  
而 SimpleDateFormat 可以輕易指定日期輸出格式,如
SimpleDateFormat format =
 new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");    
 System.out.println(format.format(startTime));
深入研究了解這幾個 Class  之後(Calendar,SimpleDateFormat,Date),就可以對時間的處理操作自如了。