码迷,mamicode.com
首页 > 其他好文 > 详细

如何通过字典表来获取下拉数据的实现

时间:2017-04-23 23:17:42      阅读:780      评论:0      收藏:0      [点我收藏+]

标签:application 取值   设值   



①在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="${category}"/>
<c:if test="${not empty id}">
	<c:set var="htmlId" value="${id}"/>
</c:if>

<c:set var="htmlName" value="${htmlId}"/>
<c:if test="${not empty name}">
	<c:set var="htmlName" value="${name}"/>
</c:if>

<c:set var="htmlAttr" value=""/>
<c:if test="${not empty onChange}">
	<c:set var="htmlAttr" value="onChange=‘${onChange}‘"/>
</c:if>
<c:if test="${not empty html}">
	<c:set var="htmlAttr" value="${htmlAttr}${html}"/>
</c:if>
<c:set var="beside" value="${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="${bizCodeKeyValue.key}"<c:if test="${selected == bizCodeKeyValue.key}"> selected</c:if>>${bizCodeKeyValue.value}</option>
			</c:when>
			<c:otherwise>
				<c:set var="exist" value="0"/>
				<c:forEach var="item" items="${fn:split(beside,‘,‘)}">
					<c:if test="${bizCodeKeyValue.key==item}">
						<c:set var="exist" value="1"/>
					</c:if>
				</c:forEach>
				<c:if test="${exist == 0}">
					<option value="${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="${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

如何通过字典表来获取下拉数据的实现

标签:application 取值   设值   

原文地址:http://jianboli.blog.51cto.com/12075002/1918650

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!