在页面中,我们经常会碰到用EL表达式来获取某个变量的值,这个变量值的获取代码可能比较多,如果每个页面都写这么一个函数的话,就会导致代码的严重冗余,
为解决这个问题,我们可以定义一个函数,以此来大大减少页面代码,提高开发效率。
下面我以页面中获取Referer变量值为例(注:该方法只适用于公司内部框架,其它人无法直接使用,不过思想还是可以学习的)
如下图所示,我们在plugins包下的com.zving.dynamic.plugin插件下的扩展项中添加一个GetReferer,扩展详情见右侧。
1.添加扩展项
2.GetReferer方法所在的类的位置如下图所示:
3.GetReferer类代码
/*
* Copyright (c) 2007-2016. ZVING and/or its affiliates. All rights reserved.
* ZVING PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.zving.dynamic.function;
* Copyright (c) 2007-2016. ZVING and/or its affiliates. All rights reserved.
* ZVING PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.zving.dynamic.function;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import com.zving.framework.Current;
import com.zving.framework.RequestData;
import com.zving.framework.expression.AbstractFunction;
import com.zving.framework.expression.ExpressionException;
import com.zving.framework.expression.IFunction;
import com.zving.framework.expression.IVariableResolver;
import com.zving.framework.utility.StringUtil;
import com.zving.framework.RequestData;
import com.zving.framework.expression.AbstractFunction;
import com.zving.framework.expression.ExpressionException;
import com.zving.framework.expression.IFunction;
import com.zving.framework.expression.IVariableResolver;
import com.zving.framework.utility.StringUtil;
/**
*
* @author Pong.Lee <a href="http://www.leepeng.com.cn/">Pong.Lee</a>
* @mail lp@zving.com/leepeng@leepeng.com.cn
* @date 2016年8月1日 下午5:53:05
*
*/
public class GetReferer extends AbstractFunction implements IFunction {
*
* @author Pong.Lee <a href="http://www.leepeng.com.cn/">Pong.Lee</a>
* @mail lp@zving.com/leepeng@leepeng.com.cn
* @date 2016年8月1日 下午5:53:05
*
*/
public class GetReferer extends AbstractFunction implements IFunction {
@Override
public Object execute(IVariableResolver arg0, Object... args) throws ExpressionException {
String referer = null;
boolean urlEncode = false;
if (args != null && args.length > 0) {
Object o = args[0];
if(args.length>=1){
urlEncode = (Boolean)args[1];
}
if (o instanceof RequestData) {
RequestData request = (RequestData) o;
referer = request.getString("Referer");
if (StringUtil.isEmpty(referer)) {
referer = request.getHeaders().getString("Referer");
}
public Object execute(IVariableResolver arg0, Object... args) throws ExpressionException {
String referer = null;
boolean urlEncode = false;
if (args != null && args.length > 0) {
Object o = args[0];
if(args.length>=1){
urlEncode = (Boolean)args[1];
}
if (o instanceof RequestData) {
RequestData request = (RequestData) o;
referer = request.getString("Referer");
if (StringUtil.isEmpty(referer)) {
referer = request.getHeaders().getString("Referer");
}
} else if (o instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest) o;
referer = request.getParameter("Referer");
if (StringUtil.isEmpty(referer)) {
referer = request.getHeader("Referer");
}
}
} else {
RequestData request = Current.getRequest();
referer = request.getString("Referer");
if (StringUtil.isEmpty(referer)) {
referer = request.getHeaders().getString("Referer");
}
}
try {
if(StringUtil.isNotEmpty(referer)&&urlEncode){
return URLEncoder.encode(referer,"UTF-8");
}
return referer;
} catch (UnsupportedEncodingException e) {
return new ExpressionException(e);
}
}
HttpServletRequest request = (HttpServletRequest) o;
referer = request.getParameter("Referer");
if (StringUtil.isEmpty(referer)) {
referer = request.getHeader("Referer");
}
}
} else {
RequestData request = Current.getRequest();
referer = request.getString("Referer");
if (StringUtil.isEmpty(referer)) {
referer = request.getHeaders().getString("Referer");
}
}
try {
if(StringUtil.isNotEmpty(referer)&&urlEncode){
return URLEncoder.encode(referer,"UTF-8");
}
return referer;
} catch (UnsupportedEncodingException e) {
return new ExpressionException(e);
}
}
@Override
public Class<?>[] getArgumentTypes() {
return new Class<?>[] { Object.class };
}
public Class<?>[] getArgumentTypes() {
return new Class<?>[] { Object.class };
}
@Override
public String getFunctionName() {
return "getReferer";
}
public String getFunctionName() {
return "getReferer";
}
@Override
public String getFunctionPrefix() {
return "";
}
public String getFunctionPrefix() {
return "";
}
}
4.页面中使用(下面红色字体部分)
document.write("<a id='baokandenglu' href='${var("ssoURL")}login?Referer=${getReferer(Request,true)}'>登录</a> ");