728x90
2021/05/25(화)
Spring 공유 캘린더 프로젝트 비밀번호 찾기 알고리즘 참고
https://cordingdoah.tistory.com/115
로그인 후 메인화면 출력
<LoginController.java>
- 컨트롤러 전체 코드
package org.zerock.controller;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import org.alpreah.domain.member;
import org.alpreah.persistence.member_dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.AbstractMappingJacksonResponseBodyAdvice;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
@Controller
public class LoginController {
@Autowired
private member_dao m_dao;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String login() {
return "login";
}
@ResponseBody
@RequestMapping(value = "Login", method = RequestMethod.POST)
public int login(member m, HttpSession session) {
return m_dao.Login(m, session);
}
/* 로그인 화면 --> 비밀번호 찾기 버튼 */
@RequestMapping("/pwFind")
public String pwFind() {
return "pwFind";
}
/* 비밀번호 찾기 화면 --> 확인 버튼 */
@ResponseBody
@RequestMapping(value = "PwFind", method = RequestMethod.POST)
public int pwFind(member m, HttpSession session) {
return m_dao.pwFind(m, session);
}
/* 아이디와 힌트, 답이 일치하면 전환되는 화면 */
@RequestMapping("/pw")
public String pw() {
return "pw";
}
/* 로그인 화면 --> 회원가입 버튼 클릭 */
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register() {
return "register";
}
/* 회원가입 화면 --> 확인 버튼 */
@ResponseBody
@RequestMapping(value = "/register", method = RequestMethod.POST)
public int register(member m) {
return m_dao.Register(m);
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(HttpSession session) {
session.removeAttribute("m");
return "redirect:/";
}
@RequestMapping(value = "/show", method = RequestMethod.GET)
public String show() {
return "show";
}
@ControllerAdvice
public class JsonpAdviceController extends AbstractMappingJacksonResponseBodyAdvice {
public JsonpAdviceController() {
super();
}
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
// TODO Auto-generated method stub
}
}
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
}
메인화면 관련코드
/* 메인화면 */
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
<index.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.css"
rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<meta charset="UTF-8">
<title>메인 페이지</title>
</head>
<body>
<div id="login">
<h3 class="text-center text-white pt-5">Main form</h3>
<div class="container">
<div id="login-row"
class="row justify-content-center align-items-center">
<div class="form-group">
<label for="text" class="text-info"><h1>Main Page</h1></label><br>
<label for="text" class="text-info"><h3>
${sessionScope.m.m_name}님 접속중!</h3></label><br>
<button a href="/logout" type="button"
class="btn btn-info btn-md" onclick="location.href = '/'">Logout</button>
<button a href="/schedule/calendar" type="button" class="btn btn-info btn-md"
onclick="location.href = '/calendar.do'">Go Calendar</button>
</div>
</div>
</div>
</div>
<script
src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
</body>
</html>
<login.jsp>
아이디와 비밀번호가 일치하면 /index?m_id=" + $("#m_id").val() 값으로 아이디값을 주소창으로 넘겨줌
<script>
$(document).ready(function() {
$("#login_process").click(function() {
var json = {
m_id : $("#m_id").val(),
m_pw : $("#m_pw").val()
};
for ( var str in json) {
if (json[str].length == 0) {
alert($("#" + str).attr("placeholder") + "를 입력해주세요.");
$("#" + str).focus();
return;
}
}
$.ajax({
type : "post",
url : "Login",
data : json,
success : function(data) {
switch (Number(data)) {
case 0:
alert("아이디 또는 비밀번호가 일치하지 않습니다.");
break;
case 1:
window.location.href = "/index?m_id=" + $("#m_id").val();
default:
break;
}
},
error : function(error) {
alert("오류 발생" + error);
}
});
});
});
</script>
--> 결과화면
728x90
'코딩 연습장 > Spring' 카테고리의 다른 글
Spring 공유 캘린더 프로젝트 - 7 (캘린더 일정 추가) (0) | 2021.06.28 |
---|---|
Spring 공유 캘린더 프로젝트 - 6 (캘린더 화면) (0) | 2021.06.25 |
Spring 공유 캘린더 프로젝트 - 4 (비밀 번호 찾기) (0) | 2021.06.24 |
Spring 공유 캘린더 프로젝트 - 3 (회원 가입) (0) | 2021.06.24 |
Spring 공유 캘린더 프로젝트 - 2 (로그인) (0) | 2021.06.23 |