首页 > 代码库 > 如何通过字典表来获取下拉数据的实现
如何通过字典表来获取下拉数据的实现
①在web.xml中添加监听,启动的时候初始化。
<!--Web ApplicationContext 载入,继承处Spring的ApplicationContextListener --> <listener> <listener-class>cn.sccl.common.web.StartupListener</listener-class> </listener>
②我们需要在启动Tomcat的时候,初始化bizCode数据
package cn.sccl.common.web; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationContext; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.WebApplicationContextUtils; import cn.sccl.common.service.BizCodeManager; import cn.sccl.common.web.util.Log4jWebConfigurer; import cn.sccl.pms.model.Division; import cn.sccl.pms.model.DivisionQuery; import cn.sccl.pms.service.DivisionManager; public class StartupListener extends ContextLoaderListener implements ServletContextListener { private final Log log = LogFactory.getLog(getClass()); @Override public void contextInitialized(ServletContextEvent event){ super.contextInitialized(event); log.debug("contextInitialized"); ServletContext context = event.getServletContext(); setupContext(context); // init : location of the log4j config file & refresh interval for // checking the log4j config Log4jWebConfigurer.initLogging(event.getServletContext()); } protected void setupContext(final ServletContext context) { ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context); BizCodeManager bizCodeManager = (BizCodeManager) ctx.getBean(WebNames.BIZ_CODE_MANAGER); Map<String, Map<String, String>> allBizCodes = bizCodeManager.list2map(); context.setAttribute(WebNames.BIZ_CODES, allBizCodes); // 行政区划 DivisionManager divisionManager = (DivisionManager) ctx.getBean("divisionManager"); List<Division> divisions = divisionManager.query(new DivisionQuery()); context.setAttribute("divisions", divisions); log.debug(allBizCodes); } @Override public void contextDestroyed(ServletContextEvent event) { Log4jWebConfigurer.shutdownLogging(event.getServletContext()); } }
③我们在jsp页面中自定义下拉标签来获取字典。
<sccl:selectBizCodes category="OrderOfDevRepair_Status" selected="${orderOfDevRepairQuery.status}" id="status" html="style=‘table_text‘"/>
④selectBizCodes.tag
<%@ tag pageEncoding="GBK" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <%@ attribute name="category" required="true" %> <%@ attribute name="id"%> <%@ attribute name="name"%> <%@ attribute name="onChange"%> <%@ attribute name="html"%> <%@ attribute name="selected"%> <%@ attribute name="beside" %> <c:set var="htmlId" value="http://www.mamicode.com/${category}"/> <c:if test="${not empty id}"> <c:set var="htmlId" value="http://www.mamicode.com/${id}"/> </c:if> <c:set var="htmlName" value="http://www.mamicode.com/${htmlId}"/> <c:if test="${not empty name}"> <c:set var="htmlName" value="http://www.mamicode.com/${name}"/> </c:if> <c:set var="htmlAttr" value=""/> <c:if test="${not empty onChange}"> <c:set var="htmlAttr" value="http://www.mamicode.com/onChange=‘${onChange}‘"/> </c:if> <c:if test="${not empty html}"> <c:set var="htmlAttr" value="http://www.mamicode.com/${htmlAttr}${html}"/> </c:if> <c:set var="beside" value="http://www.mamicode.com/${beside}"></c:set> <select class="form-control" id="${htmlId}" name="${htmlName}" ${htmlAttr}> <option value="">--请选择--</option> <c:forEach var="bizCodeKeyValue" items="${applicationScope.bizCodes[category]}"> <c:choose> <c:when test="${empty beside}"> <option value="http://www.mamicode.com/${bizCodeKeyValue.key}"<c:if test="${selected == bizCodeKeyValue.key}"> selected</c:if>>${bizCodeKeyValue.value}</option> </c:when> <c:otherwise> <c:set var="exist" value="http://www.mamicode.com/0"/> <c:forEach var="item" items="${fn:split(beside,‘,‘)}"> <c:if test="${bizCodeKeyValue.key==item}"> <c:set var="exist" value="http://www.mamicode.com/1"/> </c:if> </c:forEach> <c:if test="${exist == 0}"> <option value="http://www.mamicode.com/${bizCodeKeyValue.key}"<c:if test="${selected == bizCodeKeyValue.key}"> selected</c:if>>${bizCodeKeyValue.value}</option> </c:if> </c:otherwise> </c:choose> </c:forEach> </select> <!-- <select id="${htmlId}" class="select" name="${htmlName}" ${htmlAttr}> <option value="">--请选择--</option> <c:forEach var="bizCodeKeyValue" items="${applicationScope.bizCodes[category]}"> <c:if test="${bizCodeKeyValue.key != beside }"> <option value="http://www.mamicode.com/${bizCodeKeyValue.key}"<c:if test="${selected == bizCodeKeyValue.key}"> selected</c:if>>${bizCodeKeyValue.value}</option> </c:if> </c:forEach> </select> -->
总结:
下拉标签的实现:在Tomcat启动的时候就将所有的字典表数据全部查询出来放到ServletContext中,然后在jsp中采用自定义标签,在标签中通过applicationScope来获取。
附录:
取session: session.setAttribute("data",data); 然后el这样取:${sessionScope.data} 取application: application.setAttribute("data", data); 然后el这样取:${applicationScope.data}
本文出自 “JianBo” 博客,请务必保留此出处http://jianboli.blog.51cto.com/12075002/1918650
如何通过字典表来获取下拉数据的实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。