`

JSF的commandButton、commandLink、outputLink用法小结

    博客分类:
  • Jsf
阅读更多

comanndButton和commandLink:

h:commandButton
可以提交表单,但不能传递参数
h:commandLink
可以提交表单,又能传递参数,但是以链接的形式展现

commandLink必须要在一个from中。

comanndButton和commandLink要在一个from中才能提交表单内容。

会发送回本页面,并触发JSF的生命周期,比如:重建组件树、应用请求值等,因此,允许设置actionListener和action属性,这样他们可以很轻松完成强大的功能。

Java代码 复制代码
  1. <h:commandButton actionListener="#{actionListener.check}"; value="送出"; action="#{user.check}" />  
<h:commandButton actionListener="#{actionListener.check}"; value="送出"; action="#{user.check}" />



actionListener 响应的一个事件,当然这个和js中的事件不一样.actionListener="#{actionListener.check}"响应的是服务器端的事件actionListener类的check方法.

action 用过STRUTS的都知道,提交后执行的方法.当然在SRTUTS中action="URL",而这里是一个user类的check方法.

不足在于:如果重建组件树的成本比较高(比如:当前页面显示一个数据表格),而这些组件对于即将跳转到的页面没什么用时,就应该考虑使用outputLink了。

Java代码 复制代码
  1. <h:commandLink action="#{user.testLink}"><f:verbatim>增加</f:verbatim></h:commandLink>  
<h:commandLink action="#{user.testLink}"><f:verbatim>增加</f:verbatim></h:commandLink>



另外:如果需要传递参数<f:param.../>,使用commandLink
在action或actionListener中获取<f:param.../>:

Java代码 复制代码
  1. FacesContext ctx = FacesContext.getCurrentInstance();    
  2. int productId = Integer.parseInt((String)ctx.getExternalContext().getRequestParameterMap().get("productId"));   
FacesContext ctx = FacesContext.getCurrentInstance(); 
int productId = Integer.parseInt((String)ctx.getExternalContext().getRequestParameterMap().get("productId")); 



outputLink

比起前两个来说,他相当的轻量级了。他会直接产生一个<a href=""></a>链接,跳转到相应的页面,因此没有进入JSF生命周期的额外开销,跟我们直接写一个html的链接没什么区别。

如果需要传递参数,嵌入<f:param name="a" value="b"/>就可以了,当然这里的value可以用表达式来表示,比如value="#{param.productId}",用起来是相当方便的。

Java代码 复制代码
  1. <h:outputLink value="productEdit.faces">    
  2. <h:outputText value="编辑"/>    
  3. <f:param name="productId" value="#{item.productId}"/>    
  4. </h:outputLink>  
<h:outputLink value="productEdit.faces"> 
<h:outputText value="编辑"/> 
<f:param name="productId" value="#{item.productId}"/> 
</h:outputLink>



其效果为<a href="..jsf?productId=..."></a>

h:commandButton、h:commandLink 和h:outputLink的差别在于:h:outputLink没有进入JSF的生命周期,而h:commandLink和h:commandButton都要进入JSF的生命周期.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics