코드를 메모리에 올려 사용하자
개발환경 : 스프링 3.0
툴 : 이클립스
DB : 오라클 (상관없음) - 구조는 코드마스터(GRP_CD)와 코드상세 테이블(GRP_CD, CODE)로 이루어져있음
참고 : http://welchsy.tistory.com/289
프로젝트를 진행하다 보면 코드성 데이터를 매번 불러오기가 번거롭게 느껴질 때가 많았다. 예전에 사용했던 것이라 조금 지져분 할 수 있으나 커스터마이징해서 사용할 사람만 사용하시길 바란다.
1. *-context 에서 공통코드 조회하는 Bean 세팅
xml : <beans:bean id="codeList" class="서비스경로" />
Java
import java.util.HashMap;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.XXX.model.common.CodeDetail;
import com.XXX.model.common.CodeMaster;
import com.XXX.service.CodeService;
@Controller
public class CodeController {
public static HashMap<String, ArrayList<CodeDetail>> CODE_MAP;
public static String codeResult;
public static ArrayList<CodeMaster> CODE_LIST;
@Autowired
private CodeService codeService;
public CodeController() {}
@PostConstruct
public HashMap<String, ArrayList<CodeDetail>> setCodeList(){
CODE_MAP = new HashMap<>();
CODE_LIST = codeService.getCodeList();
if(CODE_LIST != null && CODE_LIST.size() > 0){
for(CodeMaster cm : CODE_LIST){
CODE_MAP.put(cm.getGrpCd(), cm.getCodeDetailList());
}
}
return CODE_MAP;
}
/**
* 코드값에 의한 코드명가져오기
* @param grpCd
* @param code
* @return
*/
public String getCodeNameByCode(String grpCd, String code){
codeResult = "";
if(CODE_MAP != null){
ArrayList<CodeDetail> codeList = CODE_MAP.get(grpCd);
if(codeList != null && codeList.size() > 0){
for(CodeDetail bean : codeList){
if(bean.getCode() != null && bean.getCode().equals(code)){
codeResult = bean.getCodeNm();
}
}
}
}
return codeResult;
}
/**
* 코드명에 의한 코드값가져오기
* @param grpCd
* @param codeName
* @return
*/
public String getCodeByCodeName(String grpCd, String codeName){
codeResult = "";
if(CODE_MAP != null){
ArrayList<CodeDetail> codeList = CODE_MAP.get(grpCd);
if(codeList != null && codeList.size() > 0){
for(CodeDetail bean : codeList){
if(bean.getCodeNm() != null && bean.getCodeNm().equals(codeName)){
codeResult = bean.getCode();
}
}
}
}
return codeResult;
}
public ArrayList<String> getComboBoxByCodeList(String grpCd, String value){
ArrayList<String> resultList = new ArrayList<>();
ArrayList<CodeDetail> codeList = CODE_MAP.get(grpCd);
StringBuffer sb = new StringBuffer();
String selChk = "";
if(codeList != null && codeList.size() > 0){
for(CodeDetail bean : codeList){
selChk = "";
if(value != null && value.length() > 0){
if(bean.getCode().equals(value)){
selChk = " selected";
}
}
sb.append("<option value=\""+bean.getCode()+"\""+selChk+" >");
sb.append(bean.getCodeNm());
sb.append("</option>");
resultList.add(sb.toString());
}
}
return resultList;
}
}
이렇게 세팅하고 서버를 start 하면 공통코드가 static으로 설정된 변수에 담기게 된다.
2. 태그라이브러리를 이용하여 커스템태그를 View에서 사용하기
태그라이브러리를 사용하기 위하여 view 상단에 <%@ taglib prefix="tag" tagdir="/WEB-INF/tags"%> 를 삽입한다.
커스텀태그를 생성한다
<%@ tag language="java" pageEncoding="UTF-8" body-content="empty"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ attribute name="type" type="java.lang.String" required="true" description="combo, radio, code, codeNm"%>
<%@ attribute name="grpCd" type="java.lang.String" required="true" description="그룹코드"%>
<%@ attribute name="id" type="java.lang.String" required="false"%>
<%@ attribute name="name" type="java.lang.String" required="false"%>
<%@ attribute name="style" type="java.lang.String" required="false" description="임의의 style"%>
<%@ attribute name="cls" type="java.lang.String" required="false" description="style의 class"%>
<%@ attribute name="value" type="java.lang.String" required="false" description="콤보박스, 라디오버튼의 선택된 값"%>
<%@ attribute name="defaultTxt" type="java.lang.String" required="false" description="콤보박스 : 초기값"%>
<%@ attribute name="defaultCheck" type="java.lang.Integer" required="false" description="라디오버튼 : default 체크되는 값(1,2,3,4,5...)"%>
<%@ attribute name="code" type="java.lang.String" required="false"%>
<%@ attribute name="codeNm" type="java.lang.String" required="false"%>
<c:set var="codeMap" value="<%=CodeController.CODE_MAP %>" />
<c:choose>
<c:when test="${type eq 'combo'}">
<select id="${id}" name="${name}" class="${cls}" style="${style}">
<c:if test="${defaultTxt != null && fn:length(defaultTxt) gt 0 }">
<option value="" >${defaultTxt}</option>
</c:if>
<c:if test="${fn:length(codeMap[fn:trim(grpCd)]) gt 0}">
<c:forEach var="result" items="${codeMap[fn:trim(grpCd)]}" varStatus="i">
<c:set var="selChk" value="" />
<c:if test="${result.code eq value}">
<c:set var="selChk" value="selected" />
</c:if>
<option value="${result.code}" ${selChk}>${result.codeNm}</option>
</c:forEach>
</c:if>
</select>
</c:when>
<c:when test="${type eq 'radio'}">
<c:if test="${fn:length(codeMap[fn:trim(grpCd)]) gt 0}">
<c:forEach var="result" items="${codeMap[fn:trim(grpCd)]}" varStatus="i">
<c:set var="selChk" value="" />
<c:if test="${i.count eq defaultCheck}">
<c:set var="selChk" value="checked" />
</c:if>
<c:if test="${result.code eq value}">
<c:set var="selChk" value="checked" />
</c:if>
<input type="${type}" id="${id}${i.count}" name="${name}" class="${cls}" style="${style}" value="${result.code}" ${selChk} /> ${result.codeNm}
</c:forEach>
</c:if>
</c:when>
<c:when test="${type eq 'codeNm'}">
<c:if test="${fn:length(codeMap[fn:trim(grpCd)]) gt 0}">
<c:forEach var="result" items="${codeMap[fn:trim(grpCd)]}" varStatus="i">
<c:if test="${fn:trim(result.code) eq fn:trim(code)}">
${result.codeNm}
</c:if>
</c:forEach>
</c:if>
</c:when>
<c:when test="${type eq 'code'}">
<c:if test="${fn:length(codeMap[fn:trim(grpCd)]) gt 0}">
<c:forEach var="result" items="${codeMap[grpCd]}" varStatus="i">
<c:if test="${fn:trim(result.codeNm) eq fn:trim(codeNm) }">
${result.code}
</c:if>
</c:forEach>
</c:if>
</c:when>
</c:choose>
해당 태그를 만들고 화면에서 그냥 사용하면 된다.
사용예
1. COMBO BOX
<tag:code type="combo" grpCd="<%=PengtaiConstants.DISPLAY_STATUS %>" id="sch_dispStatus" name="sch_dispStatus" cls="select_entry01" defaultTxt="전체" value="${noticeParam.sch_dispStatus }" style="width:200px"/>
2. CODE 명
<tag:code type="codeNm" grpCd="<%=PengtaiConstants.DISPLAY_STATUS %>" code="${result.dispStatus }"/>
3. RADIO
<tag:code type="radio" grpCd="<%=PengtaiConstants.DISPLAY_STATUS %>" id="dispStatus" name="dispStatus" style="" cls="" defaultCheck="1"/>
즐거운 개발하시길 바랍니다~
'Java & Html' 카테고리의 다른 글
Eclipse에 FindBugs 설치 (0) | 2016.11.01 |
---|---|
Java Google Style Guide (0) | 2016.10.12 |
Spring Transaction (0) | 2015.12.27 |
JSP 커스텀 태그(Custom Tag) (0) | 2015.07.07 |
java 1.8 특징 (0) | 2015.06.29 |