File này được tạo để lưu trữ 1 tập các cặp giá trị key=value cho ứng dụng. Các key này được sử dụng trong ứng dụng và nó sẽ được thay thế bởi các value tương ứng. Lợi ích của việc này là rất lớn, ta có thể thay đổi các value trong ứng dụng mà không cần phải thay đổi bất cứ dòng code nào của ứng dụng. Ứng dụng của chúng ta sử file propertie có tên LRAppResourceBundle.properties.
Nội dung của nó như sau:
errorFirstName=Only [a-z][A-Z] characters allowed
errorFirstNameLength=Last Name must be of 1-25 characters
errorLastName=Only [a-z][A-Z] characters allowed
errorLastNameLength=Last Name must be of 1-25 characters
errorUserNameBlank=User Name cannot be blank
errorUserNameLength=User Name must be of 6-20 characters
errorUserName=Only (.) special symbol is allowed
errorValidUserName=User is already registered
errorPasswordLength=Password must be of 6-20 characters
errorPassword= Only alphanemeric characters allowed
errorConfirmPasswordLength=Password must be of 6-20 characters
errorConfirmPasswordValid= Only alphanemeric characters allowed
errorConfirmPassword=Please enter confirm password correct
errorEmail=Invalid Email Address
errorValidEmail=Email is already registered
errorAddressBlank=Address cannot be blank
errorCountryBlank=Country cannot be blank
errorStateBlank=State cannot be blank
errorContactNumberBlank=Contact Number cannot be blank
errorPasswordConfirm=Please enter confirm password correct
errorEmailMsg=Email Address cannot be blank
|
Bạn có thể định nghĩa thêm để sử dụng cho các trường hợp khác. resource bunle này sẽ được load bởi java file có tên MessageFactory.java. Code load nó như sau:
locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
bundle = ResourceBundle.getBundle("vovanhai.wordpress.bean.LRAppResourceBundle", locale);
|
Lưu ý là chúng ta phải chỉ rõ package chứa nó để yêu cầu load cho chính xác.
Like this:
Be the first to like this page.
Quynh said
Thay oi sao em viet class MessageFactory.java ma no cu bi loi hoai a thay, thay giup em voi
vovanhai said
Lỗi thế nào bạn phải nói rõ chứ!
Loc said
chao thay! em khong bik cach tao internationalization. thay co the giup em dc khong ah?
long said
Thưa thầy! Em có lỗi này mong thầy xem xét giùm em vs ạ.
Chươngtrình của em sử dụngJSF 2.0.Em tạo file .LRAppResourceBundle.properties để cùng package chứa MessageFactory.java nhưng khi:
MessageFactory mf = new MessageFactory();
ở một managerBean thì lại bị lỗi:
java.util.MissingResourceException: Can’t find bundle for base name LRAppResourceBundle, locale en_US
Class MessageFactory của em :
public class MessageFactory {
ResourceBundle bundle;
Locale locale;
public MessageFactory() {
locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
bundle = ResourceBundle.getBundle(“LRAppResourceBundle”, locale);
}
public String getMessage(String key) {
return bundle.getString(key);
}
}
Khi debug em có thấy mấy dòng đỏ:
No current context (stack frame) <= nó có nghĩa là gì ạ?
tumy1 said
Em chào thầy!
Thầy có thể giúp em hướng dẫn cách add file vào resource được không thầy… tình hình là em muốn add file kml vào source lun … để khi chạy nó tích hợp vào file apk lun… chứ đọc file kml từ sdcard thì bất tiện nếu sdcard k có file đó…
em có làm 1 cách thấy cũng ok là bỏ file kml vào thư mục assets trong source… rồi vào code gọi hàm getAssets().open(“…kml”) ra…
mà em biết là còn 1 cách là save file kml vào trong thư mục raw trong source res/raw …Ví dụ file kml : ATM.kml
Em có làm thử mà không được… Em sữ dụng InputStream inputStream = this.getResources().openRawResource(R.raw.ATM); tới đây báo lổi raw… em không hiểu lắm… mong thầy giúp em chỉnh như thế nào… em cám ơn thầy