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";
}
}
页面这样写,直接调相应的方法
<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: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.下拉菜单写法:
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);
}
}
页面可以直接这样写:
<p:selectOneMenu value="#{amoritizateVM.date}" converter="monthItemConverter" style="width:145px">
<f:selectItems value="#{amoritizateVM.monthItems}"></f:selectItems>
</p:selectOneMenu>
这里面用到了另外一个知识点Converter,用来转换类别,比如这里是用来Date和String的互转,所以要写上这个类
/**
只要实现它的接口,它会自动完成转换,还是很方便的
*/
@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">
眼镜蛇