JSF學習日志
1.Bean中這樣寫,頁面上直接調用@Named("bean1") // or @ManagedBean(name="bean1") or 不指定名字,默認Bean的名,sampleBean
@SessionScoped
public class SampleBean {
public int getLuckyNumber() { }
public void setLuckyNumber(int value) { }
public String login() {
if () return "success"; else return "error";
}
}
頁面這樣寫,直接調相應的方法@SessionScoped
public class SampleBean {
public int getLuckyNumber() { }
public void setLuckyNumber(int value) { }
public String login() {
if () return "success"; else return "error";
}
}
<h:commandButton value="press me" action="#{bean1.login}"/>
2.超鏈接
<h:link outcome="#{custVM.gotoDetail}" includeViewParams="true" target="_blank">
<f:param name="scmNo" value="#{warr.project_no}"/>
<f:param name="custNo" value="#{warr.cust_no}"/>
<f:param name="custName" value="#{warr.cust_name}"/>
<h:outputText value="#{warr.accrued_amt}">
<f:convertNumber currencySymbol="$" type="currency" />
</h:outputText>
</h:link>
在目標頁面寫下面這個,這樣就可以傳過來了<f:param name="scmNo" value="#{warr.project_no}"/>
<f:param name="custNo" value="#{warr.cust_no}"/>
<f:param name="custName" value="#{warr.cust_name}"/>
<h:outputText value="#{warr.accrued_amt}">
<f:convertNumber currencySymbol="$" type="currency" />
</h:outputText>
</h:link>
<f:metadata>
<f:viewParam name="scmNo" value="#{custVM.scmNo}" />
<f:viewParam name="custNo" value="#{custVM.custNo}" />
<f:viewParam name="custName" value="#{custVM.custName}" />
</f:metadata>
3.下拉菜單寫法:<f:viewParam name="scmNo" value="#{custVM.scmNo}" />
<f:viewParam name="custNo" value="#{custVM.custNo}" />
<f:viewParam name="custName" value="#{custVM.custName}" />
</f:metadata>
private List<SelectItem> monthItems; //它有自己的SelectItem 類,用來存鍵值對。
@PostConstruct
public void init() {
Calendar now = Calendar.getInstance();
date = now.getTime();
monthItems = new ArrayList<SelectItem>();
try {
List<Date> monthList = amoritizateService.getMonthList();
for(Date month:monthList){
monthItems.add(new SelectItem(month, DateUtil.format(month, "yyyy - MM")));
}
} catch (Exception e) {
LOG.error("ERROR!",e);
}
}
頁面可以直接這樣寫:@PostConstruct
public void init() {
Calendar now = Calendar.getInstance();
date = now.getTime();
monthItems = new ArrayList<SelectItem>();
try {
List<Date> monthList = amoritizateService.getMonthList();
for(Date month:monthList){
monthItems.add(new SelectItem(month, DateUtil.format(month, "yyyy - MM")));
}
} catch (Exception e) {
LOG.error("ERROR!",e);
}
}
<p:selectOneMenu value="#{amoritizateVM.date}" converter="monthItemConverter" style="width:145px">
<f:selectItems value="#{amoritizateVM.monthItems}"></f:selectItems>
</p:selectOneMenu>
這里面用到了另外一個知識點Converter,用來轉換類別,比如這里是用來Date和String的互轉,所以要寫上這個類<f:selectItems value="#{amoritizateVM.monthItems}"></f:selectItems>
</p:selectOneMenu>
/**
只要實現它的接口,它會自動完成轉換,還是很方便的
*/
@FacesConverter("monthItemConverter")
public class MonthItemConverter implements Converter {
private static final Logger LOG = LoggerFactory.getLogger(MonthItemConverter.class);
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
return DateUtil.parseDate(arg2);
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
return DateUtil.format((Date)arg2);
}
}
只要實現它的接口,它會自動完成轉換,還是很方便的
*/
@FacesConverter("monthItemConverter")
public class MonthItemConverter implements Converter {
private static final Logger LOG = LoggerFactory.getLogger(MonthItemConverter.class);
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
return DateUtil.parseDate(arg2);
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
return DateUtil.format((Date)arg2);
}
}
4.表單提交的話,用這個
<p:commandButton value="Query" update="dataForm"/>
這里面的update要對應這個頁面里的form的id<h:form id="dataForm">
眼鏡蛇
posted on 2014-11-26 13:38 眼鏡蛇 閱讀(210) 評論(0) 編輯 收藏 所屬分類: Java 、JSF