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.

@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;
    }

}

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

 
        
 
  
 
   
    
    
  
        
 
  
 
   
   View Action 
    
  
        
 
  
 
   
   

View Action

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.

    if(!FacesContext.getCurrentInstance().isPostback()){
    
    }
  3. 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.

  4. 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.

       
    
     
        
     
        
    
     
        
     
        
    
     
        
     
        
          #{viewActionBean.init()} 
        
    
    
       
    
       

    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.

你可能感兴趣的:(JSF,Glassfish,JSF22,Prettyfaces,Java-ee7)