日期:2014-05-16 浏览次数:20542 次
/** * 将spring context 中定义的bean放到Web的Application中. */ public class SpringCtxLoader extends WebApplicationObjectSupport { /** * Logger for this class */ private static final Logger logger = Logger .getLogger(CodeTableLoader.class); /** * Web application 中的常量集合Map的名字. */ private static final String SPRING_CONTEXT = "springctx"; /** * initialize application context . */ @SuppressWarnings("unchecked") protected void initApplicationContext() { try { final WebApplicationContext ctx = this.getWebApplicationContext(); HashMap springServices = new HashMap() { private static final long serialVersionUID = -1759893921358235848L; public Object get(Object key) { return ctx.getBean((String) key); }; public boolean containsKey(Object key) { return true; } }; logger.info("load code table (spring beans"); // 放入到Web全局变量中,供页面使用. this.getServletContext().setAttribute(SPRING_CONTEXT, springServices); } catch (IllegalStateException e) { logger.warn("not web app application context ,can't be load " + e.getLocalizedMessage()); } } }
<!-- web application 全局常量列表 对象 --> <bean name="springCtxLoader" class="utils.SpringCtxLoader" lazy-init="false"> </bean> <!-- UserAccountService 事务管理 --> <bean name="userAccountService" parent="baseTransactionProxy"> <property name="target"> <bean class="service.UserAccountService"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </property> </bean>
<%@ taglib prefix="ct" uri="http://devsphere.com/articles/calltag/CallTag.tld"%>
<ct:call object="userAccountService" method="getUserList" return="USER_LIST" /> 所有的用户列表:<br> <c:forEach var="vo" items="${USER_LIST}"> 用户ID:${vo.userID},姓名:${vo.userName}<br> </c:forEach> <ct:call object="userAccountService" method="getUserMapByRole" param1="admin" return="USER_LIST" /> 遍历管理员列表:<br> <c:forEach var="vo" items="${USER_LIST}"> 用户ID:${vo.key},姓名:${vo.value}<br> </c:forEach