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),就可以對時間的處理操作自如了。

f:convertDateTime 轉換時間格式

f:convertDateTime 通常會被夾在 h:inputText 或者 h:outputText 中,做為轉換日期時間之用。
最好用的屬性就是 pattern 用來指定日期時間格式,把本來的日期長整數,依我們所想要的格式顯示出來。當然,在上例中指出時區,可以更精準的顯示出當地時間。
在未使用 f:convertDateTime  之前 #{log.when}是表示毫秒的長整數

1367407097037

使用後則是 

2013-05-01 07:42:20

很方便不是嗎?

JSF 頁面基本組成

JSF 的頁面基本上是一個 xhtml 檔。和一般 Html 檔類似,html 的namespace (什麼是namespace?)指出了這個xhtml 檔會使用哪些 jsf 元件。
在以下的例子中,我們看到該頁可能會用到 xhtml 基本的 html 標籤元素,f: ,h: , ui: 等基本的 JSF 標籤元素,以及 primefaces 的 p: 標籤元素。

在 <html></html> 標籤下夾了<h:head></h:head> 和<h:body></h:body>兩組標籤。
當然,<h:head></h:head> 中會夾著 title ,meta , link 的相關標籤。而<h:body></h:body>內夾的就如同 html 的 body 一樣是網頁的主體。
了解了這個主要架構後,接下來學習各個 jsf 的 tag 使用會比較清楚。

h:outputText 單純的文字輸出

<h:outputText> 是最常用到的文字輸出標籤之一。雖然在 JSF 2.0 不一定要用它才能輸出文字,但是它還是有一些有用的屬性方便我們做各種變化輸出。
比較常會用到屬性列表如下:
h:outputText 屬性
屬性 說明
value實際上輸出的文字
rendered輸出的條件,當整個運算為true 時會輸出;為 false 時不會輸出。
id元件的 id 。ajax 或 css 操作元件時會使用。
stylecss 樣式。直接使用css 樣式
styleClasscss 樣式類別。
escape是否要避掉 XML 或 HTML  敏感的字元。如(< , > )等。
title(滑鼠滑過時)提示用的 title 和 html 的 title 相同。 

以下的程式 我們就來示範這些屬性的使用。
示範程式:
Java Managed Bean:

/**
 * 示範 outputText 用
 */
package jason.blogger.jsf;

import javax.faces.bean.ManagedBean;

/**
 * @author jason 2013/5/13
 */
@ManagedBean
public class Person
{
 private String firstName;
 private String lastName;
 private String tag;

 public Person()
 {
  super();
  firstName="三";
  lastName="張";
  tag="
"; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } }

xhtml :


CSS:
@CHARSET "UTF-8";
.name
{
  background:pink;
}

#first
{
 color:yellow;
}
輸出結果: