顯示具有 javascript 標籤的文章。 顯示所有文章
顯示具有 javascript 標籤的文章。 顯示所有文章

2012年3月21日 星期三

entity converter Value is not a valid 問題

問題描述:

在一個查詢頁,有 select 、button ,
select 項目用 做 view 和 controller 的值轉換。
範例如下:



但是在網頁很久沒有動作後,再去改變下拉選單的內容,會出現 Value is not a valid 錯誤訊息。

參考網路上說明後,推測是因為 過很久再去點選畫面時,view 經由entity converter 傳成的物件和 emtityManager 重新 query 的list 在比較時,發現沒有符合。所以認為選擇的值是 Value is not a valid 。

解決方法:overwrite equals 方法。 真是夠簡單的。

The scenario

JSF runs the converter and loads the entity from the persistence context; JSF runs any validators, and adds it's own - that the selected object must be in the original list; the validation fails as a new object has been loaded from the persistence context.

The solution

You've got two options:

Ensure that you are in a long-running conversation that spans the both the select list creation and the submission of the form.
Use key equality rather than object equality (make equals return true if both objects have the same natural or business key). It's important to note that basing equality on surrogate key identity isn't recommended. You can read more here
Which approach should you take? The second solution is the simplest to understand but sometimes it is hard (or not possible) to define a natural key so you'll need to use the first solution.
參考來源

2011年5月10日 星期二

jQuery UI datepicker 1.8.12 日期輸入限制

jQuery UI datepicker 很方便的讓一個input element 可以有calendar做為協助輸入的ui,不過有時候,要validate 輸入的日期資料時,理想的方式之一就是不要讓不想輸入的資料出現。
舉例來說,如果只想讓使用者輸入 星期一 的日期,其它的日期都不要出現的話。該怎麼做?

jQuery UI datepicker 1.8.12 顯示問題

jQuery datepicker 1.8.12 使用發現一個問題 就是 z-index 每次都會被自動設成 1,如果畫面上有 z-index 大於1的,就有可能被蓋住。

解決方法:一種是設一個很大的數字,另一方式,就是找出目前頁面中z-index 最大的element 然後再設比它大一點。
看起來後者是比較完善的解決方法。要做到此點,請參考下列步驟:

2011年5月1日 星期日

javascript 取得 月天數

function daysInMonth(iMonth, iYear)
{
    return 32 - new Date(iYear, iMonth, 32).getDate();
}

iMonth:0~11
iYear:西元年 yyyy

原理說明:
new Date(iYear, iMonth, 32) ,會依 年月日,產生一個日期,但是日期超過該月的部份會使產生的日期變成下個月的日期
舉例:
new Date(2011, 0, 32) ,是指要產生一個日期是 2011年1月32號的,但是因為1月只有31天,所以產生出來的時間變成是2011年2月1號,也就是自動幫你進位了。
產生出來的日期再用 .getDate()取得 "日" 的部份,最後用 32 去減。 換句話說,用32代入產生的日期不管是代到哪個月都會進位,進位後的日數,可能是1 ~ 4 不等,也就是說原本的月份日數是比32 少 1~4不等。 所以用32減去,就是所求。

變通一下,把function 內的兩個32 改成是 35或是其它 >31 但 <60 的數也是可以,只要不要讓它再次進位到下個月執行的意義和原做法就是一樣的。

參考:http://snippets.dzone.com/posts/show/2099