ဒါ့ကြောင့် JSF ရဲ့ Backing Bean အနေနဲ့အသုံးပြုနိုင်ဖို့အတွက် လက်ရှိအနေအထားအရ ၂ မျိုးရေးသားနိုင်ပါတယ်။ ဒါပေမဲ့ Java EE 7 အရောက်မှာ CDI Named Beans တွေကိုသာ အသုံးပြုသွားမယ်လို့ တရားဝင် ထုတ်ပြန်ပြောကြားထားတဲ့အတွက် CDI Named Beans တွေကို ဦးစားပေးဖေါ်ပြသွားပါမယ်။
Writing Managed Beans
မည်သည့် POJO Class ကို မဆို JSF ရဲ့ Managed Bean အနေနဲ့ အသုံးပြုနိုင်မှာ ဖြစ်ပါတယ်။ Managed Bean အနေနဲ့ အသုံးပြုနိုင်ရန် အောက်ပါ အချက်အလက်များကို လိုက်နာဖို့လိုအပ်ပါတယ်။
- javax.faces.bean.ManagedBean Annotation နဲ့ JSF Scope Annotation တစ်ခုခုကို တပ်ဆင်ရေးသားထားရပါမယ်
- Java Beans Technology ကို လိုက်နာပြီး ရေးသားထားရပါမယ်
- Final Class တစ်ခု မဟုတ်ရပါဘူး
- Default Constructor တစ်ခု မဖြစ်မနေပါဝင်ရပါမယ်
@ManagedBean
@RequestScope
public class StudentBean {
private String name;
private int english;
private int burmese;
private int maths;
private int chemistory;
private int biology;
private int physics;
// getters & Setters of Properties
public int getTotal() {
return english + burmese + maths + chemistory + biology + physics;
}
public int getAverage() {
return getTotal() / 6;
}
}
အထက်ပါအတိုင်း ရေးသားထားရင်တော့ Backing Bean အနေနဲ့ အသုံးပြုနိုင်မှာ ဖြစ်ပါတယ်။ တကယ်လို့ Properties တွေကို View ထဲမှာရှိတဲ့ Component Value တွေနဲ့ Bind လုပ်ချင်ရင်တော့ Java Beans Naming Rule ကိုလိုက်နာပြီးရေးသားထားတဲ့ getter / setter method တွေ ရေးသားထားဖို့လိုအပ်ပါတယ်။
getter / setter မပါပဲ View ထဲကနေ Reference လုပ်ထားမယ်ဆိုပါက PropertyNotFoundException ကို ဖြစ်ပေါ်စေမှာ ဖြစ်ပါတယ်။ ဒီထက်ပိုပြီး Properties ထဲမှာ မရှိပေမဲ့ getter method တစ်ခုကို ရေးသားထားမယ်ဆိုရင်လဲ View ထဲကနေ Reference လုပ်လို့ရမှာ ဖြစ်ပါတယ်။
အထက်ပါနမူနာအရ total နဲ့ average တို့ဟာ Beans ရဲ့ Property တွေ မဟုတ်ကြပါဘူး။ ဒါပေမဲ့ getTotal() နဲ့ getAverage() method တို့ကို ရေးသားထားတဲ့အတွက် EL Expression ထဲကနေ total တို့ average တို့အမည်နဲ့ Reference လုပ်လို့ရမှာ ဖြစ်ပါတယ်။
Writing Named Beans
CDI Beans တစ်ခုခုမှာ @Named Annotation ကို တပ်ဆင်ရေးသားလိုက်တာနဲ့ JSF ရဲ့ View ထဲကနေ ဆက်သွယ်ရေးသားနိုင်မှာ ဖြစ်ပါတယ်။ ဒါ့ကြောင့် Named Beans တွေကို အသုံးပြုတဲ့နေရာမှာ CDI Scoped Beans တွေကို အသုံးပြုတဲ့ပုံစံနဲ့ Produce လုပ်ထားတဲ့ Beans တွေကို အသုံးပြုတဲ့ပုံစံ ဆိုပြီး ၂မျိုးရှိနိုင်ပါတယ်။
CDI Scope Beans
CDI Scoped Beans တွေမှာ @Named Annotation ကို ရေးပြီး Backing Beans အနေနဲ့ အသုံးပြုနိုင်ပါတယ်။ တဖန် @Named နဲ့ @RequestScope တို့ကို ရေးသားရလွယ်ကူစေနရန် @Model ဆိုတဲ့ Annotation ကိုလဲ ပြင်ဆင်ထားပါတယ်။ ဒါ့ကြောင့် @Model လို့ရေးလိုက်တာနဲ့ @Named နဲ့ @RequestScope တို့ကိုတွဲရေးတာနဲ့ အတူတူပဲ ဖြစ်ပါလိမ့်မယ်။
@Model
public class StudentBean {
private String name;
private int english;
private int burmese;
private int maths;
private int chemistory;
private int biology;
private int physics;
// getters & Setters of Properties
public int getTotal() {
return english + burmese + maths + chemistory + biology + physics;
}
public int getAverage() {
return getTotal() / 6;
}
}
Properties တွေကို Access လုပ်တာကတော့ Managed Beans တွေနဲ့ အတူတူပဲ ဖြစ်ပါတယ်။
Produced CDI Beans
@Named Annotation ကို အသုံးပြုပြီး Produce လုပ်ထားတဲ့ Beans တွေကိုလဲ EL Expression ကို အသုံးပြုပြီး Access လုပ်လို့ရပါတယ်။
@ApplicationScope
public class CategoryProducer {
@Named
@Produces
private List<Category> categories;
@Inject
private CategoryService service;
@PostConstruct
private void load() {
categories = service.getAll();
}
}
အထက်ပါ နမူနာထဲမှာ categories ကို Produce လုပ်ထားပြီး @Named ကို ရေးသားထားပါတယ်။ ထိုသို့ရေးသားထားပါက View ထဲကနေ EL Expression ကို အသုံးပြုပြီး Access လုပ်နိုင်မှာ ဖြစ်ပါတယ်။
Managed Beans Vs Named Beans
အထက်တွင် လေ့လာခဲ့သလိုပဲ JSF ရဲ့ Managed Beans တွေကော CDI ရဲ့ Named Beans တွေကိုပါ Backing Beans တွေ အနေနဲ့ အသုံးပြုနိုင်တာကို သိခဲ့ရပါပြီ။ ဒီတစ်ခေါက်တော့ Managed Beans တွေနဲ့ Named Beans တွေဟာ ဘယ်လိုကွာခြားလဲ ဆိုတာကို လေ့လာသွားပါမယ်။
ရေးလို့ရသော ပတ်ဝန်းကျင်ဘက်ကနေကြည့်မယ်ဆိုရင် CDI Named Beans တွေကို CDI ကို သုံးလို့ရတဲ့ Java EE Container တွေရှိမှသာ အသုံးပြုနိုင်မှာ ဖြစ်ပါတယ်။ Tomcat လို Web Container သာပါတဲ့ Application Server တွေပေါ်မှာဆိုရင် Default အတိုင်းဆိုရင် CDI Named Beans တွေကို အသုံးပြုနိုင်မှာ မဟုတ်ပါဘူး။ Light Weight CDI Container တွေနဲ့ တွဲသုံးမယ်ဆိုရင်တော့ ဖြစ်နိုင်ပါတယ်။
JSF Managed Beans တွေကိုတော့ JSF Library ကို ထည့်သွင်းပြီး အသုံးပြုမယ်ဆိုရင် Tomcat လိုပတ်ဝန်းကျင်မှာလဲ အသုံးပြုနိုင်မှာ ဖြစ်ပါတယ်။ တဖန် CDI ကို သုံးလို့ရတာကလဲ JSF 2 ရောက်မှ ဖြစ်တဲ့အတွက် JSF 2 မတိုင်ခင်က Version တွေမှာ ဆိုရင် CDI Named Beans တွေကို ရေးသားလို့ရမှာ မဟုတ်ပါဘူး။
ဒါ့အပြင် @ManagedProperty ဆိုတဲ့ Annotation ဟာ JSF ရဲ့ Managed Bean တွေမှာသာ သုံးလို့ရတာ ဖြစ်ပြီး Named Bean တွေမှာ အသုံးပြုလို့မရပါဘူး။ Scope တွေနဲ့ပတ်သက်လာရင် CDI ရဲ့ Scope တွေဟာ JSF ရဲ့ Scope တွေထက်ပိုပြီး Flexable ဖြစ်ပါတယ်။ @FlowScope နဲ့ @ConversationScope တို့ကို CDI Named Beans တွေမှာသာ အသုံးပြုလို့ရမှာ ဖြစ်ပြီး Managed Beans တွေမှာ အသုံးပြုလို့ရမှာ မဟုတ်ပါဘူး။
မိမိရဲ့ ပတ်ဝန်းကျင် နဲ့ Application ရဲ့ Requirements အပေါ်မှာ မူတည်ပြီး ဘယ်လို Beans ကို သုံးမလဲဆိုတာကို ရွေးချယ် အသုံးပြုသင့်ပါတယ်။
Backing Beans တွေထဲမှာ ဘာတွေရေးသင့်သလဲ
Backing Beans တွေလို့ပြောပေမဲ့ ဒီ Beans တွေအားလုံးဟာ POJO တွေ ဖြစ်ကြပါတယ်။ ဒါ့ကြောင့် ဘာတွေရေးလို့ရလဲဆိုရင် Java မှာ ရေးလို့ရတာတွေကို ရေးလို့ရမှာ ဖြစ်ပါတယ်။ ဥပမာအားဖြင့် DataSource တွေ EntityManager တို့ကို Inject လုပ်ပြီးရင် Database ကို Access လုပ်တာတွေလဲ ရေးလို့ရတယ်ဗျာ။ JMS တွေကို Inject လုပ်ပြီး ရေးမယ်ဆိုလဲ ဖြစ်တာပဲ။ ဒါပေမဲ့ ဒါတွေဟာ ဖြစ်သင့်သလားဆိုတာကို ပြန်ပြီး လေ့လာကြည့်ကြရအောင်။
Backing Beans တွေဟာ JSF ရဲ့ Model နေရာမှာ တာဝန်ယူထားပါတယ်။ အဲ့ဒီအတွက် Model ရဲ့ တာဝန်တွေကို Backing Beans တွေမှာ ရေးခိုင်းတာဟာ သဘာဝကျပါတယ်။ ဒါဖြင့် MVC ထဲမှာ Model ရဲ့ တာဝန်က ဘာလဲ။ Model တွဟာ View တွေ အတွက် Data တွေကို Support လုပ်ပေးနိုင်ရမယ်။ View ထဲကနေပေးလာတဲ့ Input တွေကို လက်ခံယူနိုင်ရမယ်။ ပြီးတော့ Business Logic တွေကို Encapsulate လုပ်ပေးနိုင်ရပါမယ်။ဒါဆိုရင် Backing Beans တွေထဲမှာ ဘာတွေ ရေးသင့်သလဲ ဆိုတာကို မြင်လာပါလိမ့်မယ်။ View အတွက် Data တွေကို Properties တွေအနေနဲ့ ရေးသားသင့်ပါတယ်။ ပြီးတော့ Business Logic တွေကို အကောင် အထည်ဖေါ်ဖို့အတွက် Delegate လုပ်မည့် Business Modules တွေဖြစ်တဲ့ EJB Beans တွေ CDI Beans တွေကို ရေးသား သင့်ပါတယ်။ ပြီးတော့ View ကနေ Business Logic တွေကို Invoke လုပ်နိုင်ဖို့အတွက် Action Method တွေ Action Listener Method တွေကို လိုအပ်သလို ရေးသားသင့်ပါတယ်။
@Model
public class CustomerRegistrationBean {
private Customer customer;
@Inject
private CustomerService service;
@PostConstruct
private void init() {
customer = new Customer();
}
public void getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public String saveCustomer() {
service.save(customer);
return "customer-list?faces-redirect=true";
}
}
အထက်ပါ နမူနာမှာတော့ customer ဟာ View နဲ့ Bind လုပ်မည့် property ဖြစ်တဲ့ အတွက် မဖြစ်မနေ getter / setter ကို ရေးသားထားဖို့လိုအပ်ပါတယ်။ တဖန် service object ဟာ Business Logic တွေကို Delegate လုပ်မည့် Bean ဖြစ်တဲ့ အတွက် Inject လုပ်ထားရင်ရပါပြီ။ getter / setter မလိုအပ်ပါဘူး။
saveCustomer method ကတော့ Action Method ဖြစ်ပါတယ်။ Return Type ကို String ကို သတ်မှတ်ပေးရန်လိုအပ်ပြီး ဘယ် Page ကို Navigate လုပ်မလဲဆိုတဲ့ Logical View Name ကို Return ပြန်ပေးရမှာ ဖြစ်ပါတယ်။
ဒီတစ်ခေါက်တော့ Backing Beans ဆိုတာဘာလဲ ဘယ်လိုရေးသင့်သလဲ ဆိုတာကို ဖေါ်ပြခဲ့ပါတယ်။ နောက်တစ်ခေါက်မှ View ထဲကနေ Backing Beans ကို ဘယ်လို Access လုပ်မယ်ဆိုတာကို ဖေါ်ပြသွားပါမယ်။
ဆက်ပါဦးမယ်
မင်းလွင်
မင်းလွင်
No comments:
Post a Comment