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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值