JSF 2.2: View Action

#JSF 2.2: View Action#

JSF 2.2 introduced a new view action feature, which had been existed in Seam 2 and Seam 3 for a long time.

In fact, JSF 2.2 copied the Seam 3 view action exactly.

##An example

We have preRenderView event listener to load resource for view, why we need view action?

An example is better than thousands of words.

<pre> @Model public class ViewActionBean { @Inject Logger log; private String flag="page1"; public String init(){ log.info("call init"); switch(flag){ case "page1": return "page1"; default: return "page2"; } } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } } </pre>

Create a facelets view and use a view action to invoke the init method.

<pre> &lt;!DOCTYPE html> &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> &lt;f:view> &lt;f:metadata> &lt;f:viewParam name="flag" value="#{viewActionBean.flag}"/> &lt;f:viewAction action="#{viewActionBean.init()}"/> &lt;/f:metadata> &lt;h:head> &lt;title>View Action&lt;/title> &lt;meta name="viewport" content="width=device-width" /> &lt;/h:head> &lt;h:body> &lt;p> View Action&lt;/p> &lt;/h:body> &lt;/f:view> &lt;/html> </pre>

I created two empty views, page1.xhtml and page2.xhtml for test purpose.

Run this project on Glassfish4, go to http://localhost:8080/ee7-sandbox/viewAction.faces. It will switch to "page1", and if you append a flag=page2 parameter, http://localhost:8080/ee7-sandbox/viewAction.faces?flag=page2, it will redirect to page2 view.

As you see, there are some difference between view action and preRenderView event listener, view action has some advantage which is not available in preRenderView event listener.

  1. view action is a generic JSF action, it has navigation capability, preRenderView has not.

  2. by default, view action will not be executed on postback phase, but preRenderView event listener you have to code and overcome it like the following in your method.

<pre> if(!FacesContext.getCurrentInstance().isPostback()){ } </pre>

  1. By default view action will be executed in INVOKE_APPLICATION phase, it has a immediate attribute as other actions in JSF, if it is true it can provides a shortcut of the lifecycle and executes the action method in APPLY_REQUEST_VALUES phase.

  2. view action also provides a phase attribute which can specify the JSF phase you are going to execute the action.

Before JSF 2.2

Before JSF 2.2, you have some alternatives for the view action.

  1. Seam 3 Faces invented view action, of course it provides the view action feature.

  2. Prettyfaces also provides similar features, which is called url action.

<pre> &lt;url-mapping id="viewAction" outbound="true"> &lt;pattern value="/viewAction" /> &lt;view-id value="/viewAction.xhtml" /> &lt;action>#{viewActionBean.init()}&lt;/action> &lt;/url-mapping> </pre>

By default, the action is executed after RESTORE_VIEW and before APPLY_REQUEST_VALUES, which is slightly different from the view action.

The sample codes

The sample codes is hosted on my github.com account, check out the codes and play it yourself.

https://github.com/hantsy/ee7-sandbox

I assume you have installed Glassfish 4 and the latest Oracle Java 7 and NetBeans IDE 7.4 which has good Java EE 7 development support.

转载于:https://my.oschina.net/hantsy/blog/145270

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值