HTML부분
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html>
 
<head>
    <meta charset="UTF-8">
    <title>jquery를 이용한ajax json</title>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
</head>
 
<body>
    <input type="email" id="email" placeholder="email" />
    <button id="chkemail">중복확인</button>
</body>
<script>
var data = {}; //data 변수를 배열로 생성합니다.
  //중복확인 클릭시
  //jquery에서는 #은 아이디 .은 클래스를 표현합니다. $("#ID명") $(".CLASS명")
    $("#chkemail").click(function () {    // 아이디값이 chkemail인 것을 클릭했을시 function을 실행합니다.
        data.email = $("#email").val();    // data 변수에 email항목을 만듭니다. 그리고 id값이 email의 value값을 대입합니다.
        data.url = "./emailAuth";        // data변수에 url항목을 생성하고 그 값에 emailAuth를 대입합니다.
        sendServer(data);                // sendServer함수에 data를 담아서 보냅니다.
    });
 
    function sendServer(data) {            
/*        
        $.ajax({
            type: "get",                // type에는 "get || post"를 사용할 수 있습니다.
            url : data.url,                // url은 서버에 보내질 url위치를 적어줍니다.
            data : data,                // 서버에 보낼 data를 입력합니다. data : data, 라고 적혀있지만 앞에는 ajax의 형식이고 뒤에는 피라메터 값 입니다.
            dataType: "JSON",            // json을 사용할 것이기에 json을 적어줍니다.
            success: function(success) {
                console.log(success);    // 성공시 서버에서 가져온 값을 콘솔에 보여줍니다.
            },
            error: function(error) {
                console.log(error);        // 실패시 에러값을 보여줍니다.
            }
        
        });
*/                
        $.ajax({
            type: "get",
            url: data.url,
            data: data,
            dataType: "JSON",
            success: function (data) {
                if(url == "./rest/emailAuth"){
                    if (data.emailChk < 0) {
                        alert('이미 사용중인 이메일 입니다.');
                    } else {
                        alert('사용 가능한 이메일 입니다.');
                    }                      
                }
            },
            error: function (error) {
                console.log(error);
            }
        });
    }
 
</script>
 
</html>

cs

@ResponseBody가 오류가 날때
pom.xml에 아래 항목을 추가합니다.
1
2
3
4
5
6
<!-- JACKSON -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>
cs

Controller 부분
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Controller("restController")    //컨트롤러 명 지정
@RequestMapping(value = "/rest"// 컨트롤러의 위치를 정해줍니다. 
public class restController {
 
    @Autowired
    OverlayService service;
 
    private Logger logger = LoggerFactory.getLogger(this.getClass());
 
    // 중복확인(이메일)
    @RequestMapping(value = "/emailAuth")
    public @ResponseBody Map<StringString> emailAuth(@RequestParam("email"String email) {
        return service.emailAuth(email);
    }
 
}
cs


Service 부분
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Service
public class OverlayService {
 
    @Autowired
    SqlSession sqlSession;
 
    private Logger logger = LoggerFactory.getLogger(this.getClass());
 
    OverlayInterface inter = null;
 
// 이메일 중복확인
    public Map<StringString> emailAuth(String email) {
        Map<StringString> jsonObj = new HashMap<StringString>();
 
        inter = sqlSession.getMapper(OverlayInterface.class);
        String emailAuth = inter.emailAuth(email);
 
        if (emailAuth != null) {
            jsonObj.put("chk""-1");
        } else {
            jsonObj.put("chk""1");
        }
        return jsonObj;
    }
    
}
cs


DAO 부분
1. Interface
1
2
3
4
5
6
public interface OverlayInterface {
 
    //이메일 중복확인
    String emailAuth(String email);
 
}
cs

2. sql.xml부분
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 
<mapper namespace="com.rest.dao.OverlayInterface">
    <!--중복체크-->
    <select id="emailAuth" resultType="String">
        SELECT mm_email FROM members WHERE mm_email = #{param1}
    </select>
</mapper>
cs


DTO 부분
1
2
3
4
5
6
7
8
9
10
11
12
13
public class UserInfoDTO {
 
    private String mm_email; // 이메일
 
    public String getMm_email() {
        return mm_email;
    }
 
    public void setMm_email(String mm_email) {
        this.mm_email = mm_email;
    }
 
}
cs


'프로그래밍 > Spring' 카테고리의 다른 글

회원가입  (0) 2017.08.24
MyBatis 세팅  (0) 2016.06.15
STS설정 및 스프링 한글깨짐 방지 설정  (0) 2016.06.13

+ Recent posts