JSF1.1和JSF1.2之间表达式语言的变化

JavaServer Faces 1.1和1,.2版之间最基本的区别在于1.2版引入了统一的EL规范。在JSF 1.1中,创建了一种特定于JSF的EL实现,但是随着表达式语言的流行,JSP2.1规范提供了统一EL!它纳入了JSF、JSP EL和JSTL。
所以在JSF1.2中javax.faces.el包被废弃,转而由javax.el包代替,这个包可以在TOMCAT6中的LIB文件夹下找找到。
新的统一EL和JSF1.1中的EL用法不太一样。下面给个例子:
(这个例子是JSF Complete Reference书中的,书中是1.1版的,我改了下)

package com.jsfcompref.register.backing;

import com.jsfcompref.register.UserBean;
import com.jsfcompref.register.UserInfo;

import javax.faces.context.FacesContext;
import javax.el.ELContext;
import javax.el.ELException;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
//这3个被注释掉的包就是废弃的包!
//import javax.faces.el.EvaluationException;
//import javax.faces.el.MethodBinding;
//import javax.faces.el.ValueBinding;
import javax.faces.event.ActionEvent;

public class SportsInterests {

    public SportsInterests() {
    }
    
    
   public void updateUserid(ActionEvent ae){
       FacesContext context = FacesContext.getCurrentInstance( );
       ELContext elContext = context.getELContext();
       ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();       
       ValueExpression ve = expressionFactory.createValueExpression(elContext, "#{userBean.userid}", String.class);
       String userid = (String) ve.getValue(elContext);
       System.out.println("Updating userBean's userid"+userid+" to newUserId");
       ve.setValue(elContext, "newUserId");
   }

    public void getUserBeanObject(ActionEvent actionEvent) {
         FacesContext context = FacesContext.getCurrentInstance( );
         ELContext elContext = context.getELContext();
         ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();         
         ValueExpression ve = expressionFactory.createValueExpression(elContext, "#{userBean}", UserBean.class);
         UserBean myUserBean = (UserBean) ve.getValue(elContext);        
         
         //旧的做法是:
         //FacesContext context = FacesContext.getCurrentInstance( );
         //ValueBinding currentBinding = context.getApplication().createValueBinding("#{userBean}");
         //UserBean myUserBean = (UserBean) currentBinding.getValue(context);         
         //Or alternatively, uncomment the code below
         //FacesContext context = FacesContext.getCurrentInstance( );
         //UserBean myUserBean = (UserBean)context.getApplication().getVariableResolver().resolveVariable(context, "userBean");
         
         System.out.println("Getting #{userBean} from managed bean facility..");
         System.out.println("userBean.FirstName is:" + myUserBean.getFirstName() );
         System.out.println("userBean.LastName is:" + myUserBean.getLastName() );            
    }
    
    
    
    public void getUserBeanObject12(ActionEvent actionEvent) {
    
        //Important Note: This will only compile on a 1.2 environment
    
//        ELContext elContext = context.getELContext( );
//        Application application = context.getApplication( );
//        String userid = (String) application.evaluateValueExpressionGet(context, "#{userBean.userid}",String.class);
//        System.out.println("userid in 1.2 env is: " + userid);
//         
         
    }
    


public void callAddConfirmedUser(ActionEvent ae){
      FacesContext context = FacesContext.getCurrentInstance();      
      ELContext elContext = context.getELContext();
      ExpressionFactory ef = context.getApplication().getExpressionFactory();
      MethodExpression me = ef.createMethodExpression(elContext, "#{userBean.addConfirmedUser}", Void.TYPE, null);
      
      try{
        me.invoke(elContext, null);
      }catch(ELException elexp){
        Throwable wrapped = elexp.getCause();
        wrapped.printStackTrace();
      }
      
      /*
       * 旧的做法如下:
      MethodBinding mb = application.createMethodBinding("#{userBean.addConfirmedUser}", null );
      try {
       mb.invoke(context, null);
      }
      catch (EvaluationException e)
      {
       Throwable wrapped = e.getCause( );
      }
      */
}

    public void callAddConfirmedUserWithArg(ActionEvent ae){
        FacesContext context = FacesContext.getCurrentInstance();      
        ELContext elContext = context.getELContext();
        ExpressionFactory ef = context.getApplication().getExpressionFactory();
        MethodExpression me = ef.createMethodExpression(elContext, 
                            "#{userBean.addConfirmedUserWithArg}", 
                                                        Void.TYPE, 
                        new Class[] {UserInfo.class, String.class});
        
        try{
          me.invoke(elContext, new Object[] { new UserInfo(), "joe.shmoe"} );
        }
        catch(ELException elexp){
          Throwable wrapped = elexp.getCause();
          wrapped.printStackTrace();
        }
        
        /*
        * 旧的做法如下:
        FacesContext context = FacesContext.getCurrentInstance();
        
        Application application = FacesContext.getCurrentInstance().getApplication();
        Object result = null;
        MethodBinding mb = application.createMethodBinding("#{userBean.addConfirmedUserWithArg}",
        new Class [] { UserInfo.class, String.class} );
        try
        {
         result = mb.invoke(context, new Object [] {new UserInfo("123456"),"joe.shmoe" });
        }
        catch (EvaluationException e)
        {
         Throwable wrapped = e.getCause( );
        }
        */
        
    }
} 


附:  ValueExpression ve = expressionFactory.createValueExpression(elContext, "#{sessionScope.userBean}", UserBean.class);
可以设置scope范围,requestScope ;sessionScope ;applicationScope

你可能感兴趣的:(jsp,bean,JSF)