最近在看网上一些视频和代码例子的时候,发现我们可以在自定义一个EL函数的时候使用spring来管理,具体配置是这样的:自定义函数类Function.java
public class Functions {
private static SessionFactory sessionFactory;
public static List getItemCategoryList() {
List itemCategoryList = null;
try {
HibernateTemplate ht = new HibernateTemplate(sessionFactory);
return ht.find("from ItemCategory a order by a.id");
}catch(Exception e) {
e.printStackTrace();
}
return itemCategoryList;
}
}Spring配置文件applicationContent.xml
<bean id="myfunction" class="org.ld.util.Functions">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>配置好tld文件后就可以在页面中使用:
页面index.jsp <%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="my" uri="http://www.ld.org/functions"%> ${my:getItemCategoryList()} 我就是想知道下,这el函数获取类实例怎么就和spring挂上勾了呢?真的想不通,是不是我的出发点本身就是错的呢?谢谢大家
public class Functions {
private static SessionFactory sessionFactory;
public static List getItemCategoryList() {
List itemCategoryList = null;
try {
HibernateTemplate ht = new HibernateTemplate(sessionFactory);
return ht.find("from ItemCategory a order by a.id");
}catch(Exception e) {
e.printStackTrace();
}
return itemCategoryList;
}
}Spring配置文件applicationContent.xml
<bean id="myfunction" class="org.ld.util.Functions">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>配置好tld文件后就可以在页面中使用:
页面index.jsp <%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="my" uri="http://www.ld.org/functions"%> ${my:getItemCategoryList()} 我就是想知道下,这el函数获取类实例怎么就和spring挂上勾了呢?真的想不通,是不是我的出发点本身就是错的呢?谢谢大家
* JSTL函数,主要功能是可以完成权限的即时认证
* @author Administrator
*
*/
public class SecurityFunctions {
private static AclManager aclManager;
public static boolean hasPermission(int userId,String resourceSn,int permission){
return aclManager.hasPermissionByResourceSn(userId, resourceSn, permission);
} //这个方法不能定义为static,因为这将导致spring无法注入
public void setAclManager(AclManager aclManager) {
SecurityFunctions.aclManager = aclManager;
}
}
貌似视频和代码中都没有涉及到applicationContext,可能是我看视频看糊涂了?我再回头复习下
<%@ taglib prefix="my" uri="http://www.ld.org/functions"%> 是一个很有意思的东西,你可以上一下这个网址,是一个国际的残疾人相关的教育机构,实际上,EL只是JEE提供的一种机制对吧,你可以用这个机制将任何数据封装成标签在JSP中用,
而在你给出的信息中,前台的“my”自定义标签是指向http://www.ld.org/functions的,
而http://www.ld.org/functions和你的Functions类并没有任何直接的关系,
我相信http://www.ld.org/functions这个自定义标签的URI一定是定义在项目的某个jar包中,
尝试找一下,找到这个缺失的环节,你的问题才有解。但我可以负责的告诉你,EL和SPRING一分钱关系都没有,所以EL不可能自动去SPRING的container里拿bean。