JSP - 아이디 중복 체크
java - jsp에서 아이디 중복체크에 대한 제가했던 예제 첨부해드립니다.
JAVA - controller
@RequestMapping(value="/admin/idDuplChk.do")
public ModelAndView idDuplChk(@RequestParam Map<String, Object> commandMap,ModelAndView model,HttpServletRequest request,
HttpServletResponse response) throws Exception{
try {
String duplChk = commandMap.get("duplChk").toString();
int result = 0;
result = TotalmngService.idDuplChk("manager.idDuplChk", commandMap);
if(result == 0){
model.addObject("result","available_id");
}else{
model.addObject("result","unavailable_id");
}
return model;
} catch (Exception e) {
model.addObject("Msg",DataException.getExceptionMsg(e));
e.printStackTrace();
return model;
}
}
JAVA - service
int idDuplChk(String sqlId,Map<String, Object> commandMap) throws Exception;
JAVA - serviceImpl
@Override
public int idDuplChk(String sqlId,Map<String, Object> commandMap ) throws Exception {
return TotalmngDAO.idDuplChk(sqlId,commandMap);
}
JAVA - DAO
public int idDuplChk(String sqlId,Map<String, Object> commandMap) {
return selectOne(sqlId, commandMap);
}
SQL
<!-- ID 중복 체크 -->
<select id="idDuplChk" parameterType="hashmap" resultType="int">
SELECT
COUNT(*)AS CNT
FROM
<if test="duplChk != null and duplChk != ''">
table_admin
WHERE 1=1
AND a_id = #{a_id}
</if>
</select>
JSP - 자바스크립트
var idDupChk = "";
/* 아이디 중복확인 */
function fn_idDuplChk(data) {
if(data == 'admin'){
if($("#a_id").val() == '' || $("#a_id").val() == null){
alert('중복체크할 아이디를 입력하세요');
return;
}
document.form.duplChk.value="admin";
}
var formData = new FormData(document.form);
$.ajax({
type: 'POST',
data: formData,
url: "<c:url value='/admin/idDuplChk.do'/>",
dataType :'json',
processData: false,
contentType: false,
success: function(data){
//alert(data.result);
if(data.result === 'available_id'){
alert("사용 가능한 아이디 입니다.");
idDupChk = "Y";
}
else if(data.result === 'unavailable_id'){
alert("이미 사용중인 아이디입니다.");
$("#a_id").val('');
idDupChk = "";
}
},
error: function (xhr, ajaxOptions, thrownError) {}
});
}
JSP - HTML
<input type="hidden" id="duplChk" name="duplChk">
<button class="sr_id" type="button" onclick="fn_idDuplChk('admin')">중복확인</button>
'IT > JSP' 카테고리의 다른 글
JSP - JSTL 데이터 포맷 (0) | 2017.11.15 |
---|---|
JSTL - c:forEach 리스트 합계구하기 (0) | 2017.11.02 |
JSTL - c:forEach문, c:if문, c:choose문(else문) (0) | 2017.10.27 |
JSP - 페이징처리 (0) | 2017.10.20 |