Skip to content

Commit

Permalink
refactor: 알림 메시지 후 URL 변경 코드 개선
Browse files Browse the repository at this point in the history
- 알림 및 url 변경처리 부분을 담당하는 TextHtmlUtil을 만들어 쉽게 설정할 수 있도록 구현

- java warning 해결

- 가독성이 떨어지는 코드 재배치 및 명확한 변수명으로 변경
  • Loading branch information
hyeongsi committed Sep 23, 2023
1 parent d40336d commit e4a6189
Show file tree
Hide file tree
Showing 38 changed files with 587 additions and 638 deletions.
2 changes: 1 addition & 1 deletion board-servlet-jsp/board/WebContent/board/boardForm.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<div class="col-8 mb-3">
<form action="boardUI" method="get" class="row">
<div class="col-4" style="min-width:110px;">
<select name="searchType" class="form-select" id="select_searchType" data-searchType=${searchDTO.searchType}>
<select name="searchType" class="form-select" id="select_searchType" data-searchType="${searchDTO.searchType}">
<option value="title" id="opt_title">제목</option>
<option value="content" id="opt_content">내용</option>
<option value="name" id="opt_name">작성자</option>
Expand Down
16 changes: 8 additions & 8 deletions board-servlet-jsp/board/WebContent/board/postDetailForm.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<div class="mb-3 col-10">
<div class="border mb-3 p-3">
<div name="title" class="mb-3" id="title">${boardDTO.title}</div>
<div name="title" class="mb-3" id="title"><c:out value="${boardDTO.title}" /></div>
<div class="text-end" style="font-size: 0.7rem">
<span class="me-3">작성자 <c:out value="${boardDTO.name}" /></span> <span
class="me-3">조회 <c:out value="${boardDTO.viewcnt+1}" /></span> <span><c:out
Expand Down Expand Up @@ -68,8 +68,8 @@
<div class="flex-fill">
<div class="d-flex justify-content-between">
<div>
<span class="me-3"><b>${commentDTO.name}</b></<span>
<span class="text-secondary" style="font-size: 0.7rem">${commentDTO.writetime}</<span>
<span class="me-3"><b><c:out value="${commentDTO.name}" /></b></span>
<span class="text-secondary" style="font-size: 0.7rem"><c:out value="${commentDTO.writetime}" /></span>
</div>
<div>
<c:if test="${not empty login}">
Expand All @@ -84,18 +84,18 @@
onclick="toggleEditModeComment(${commentDTO.commentid})">수정하기</a>
<span class="vr"></span>
<a
href="CommentDelete?commentid=${commentDTO.commentid}&boardid=${commentDTO.boardid}&id=${commentDTO.id}"
href="CommentDeletion?commentid=${commentDTO.commentid}&boardid=${commentDTO.boardid}&id=${commentDTO.id}"
class="text-secondary"
style="font-size: 0.7rem; text-decoration: none">삭제하기</a>
</c:if>
</div>
</div>

<div class="p-2" style="white-space: pre-wrap" id="content${commentDTO.commentid}">${commentDTO.content}</div>
<div class="p-2" style="white-space: pre-wrap" id="content${commentDTO.commentid}"><c:out value="${commentDTO.content}" /></div>

<!-- 수정 입력창 -->
<div id="updateCmtForm${commentDTO.commentid}" class="d-none">
<form action="CommentUpdate" method="post">
<form action="CommentEditor" method="post">
<div class="input-group">
<input type="hidden" name="commentid"
value="${commentDTO.commentid}">
Expand All @@ -114,7 +114,7 @@

<!-- 대댓글 입력창 -->
<div id="inputReCmt${commentDTO.commentid}" class="d-none">
<form action="CommentUpload" method="post">
<form action="CommentUploader" method="post">
<div class="input-group">
<input type="hidden" name="parent_commentid"
value="${commentDTO.commentid}">
Expand All @@ -135,7 +135,7 @@

<c:choose>
<c:when test="${not empty login}">
<form action="CommentUpload" method="post">
<form action="CommentUploader" method="post">
<div class="input-group">
<input type="hidden" name="boardid" id="boardid"
value="${boardDTO.boardid}"> <input type="hidden"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="input-group">
<input type="hidden" name="boardid" value="<c:out value='${boardDTO.boardid}'/>" />
<span class="input-group-text">제목</span>
<input type="text" class="form-control" name="title" id="title" value="${boardDTO.title}" required />
<input type="text" class="form-control" name="title" id="title" value="<c:out value='${boardDTO.title}' />" required />
</div>
</div>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
</div>
<div class="col-12 mt-5">
<button class="btn btn-outline-success rounded-3" type="submit">가입하기</button>
<button class="btn btn-outline-warning rounded-3" type="reset">취소하기</button>
</div>
</form>
</section>
31 changes: 0 additions & 31 deletions board-servlet-jsp/board/src/com/common/AlertHref.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import com.dto.MemberDTO;

public class LoginUser {
public class LoginUtil {
final static String LOGIN_ATTR_STR = "login";

private LoginUser() {}
private LoginUtil() {}

// 로그인 여부 반환
public static boolean isLogin(final HttpServletRequest request) {
Expand Down
71 changes: 71 additions & 0 deletions board-servlet-jsp/board/src/com/common/TextHtmlUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.common;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.common.enums.Location;

public class TextHtmlUtil {

private TextHtmlUtil() {}

public static void initReturnTextHtml(final HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
}

public static void setAlertMessage(final PrintWriter printWriter, final String message) {

final StringBuilder printScriptBuilder = new StringBuilder();
printScriptBuilder.append("<script>alert(\'")
.append(message)
.append("\')</script>");

printWriter.println(printScriptBuilder.toString());
}

public static void setLocationHref(final PrintWriter printWriter, final String location) {

final StringBuilder printScriptBuilder = new StringBuilder();
printScriptBuilder.append("<script>location.href=\'")
.append(location)
.append("\'</script>");

printWriter.println(printScriptBuilder.toString());
}

public static void setMessageAndLocation(final PrintWriter printWriter,
final String message,
final String location) {
setAlertMessage(printWriter, message);
setLocationHref(printWriter, location);
}

public static void setNeedLoginLocation(final PrintWriter printWriter) {
setAlertMessage(printWriter, "로그인이 필요합니다.");
setLocationHref(printWriter, Location.UI_LOGIN_USER.toString());
}

public static void autoProcessNeedLoginLocation(final HttpServletResponse response) throws IOException {
initReturnTextHtml(response);

final PrintWriter printWriter = response.getWriter();
setNeedLoginLocation(printWriter);

printWriter.close();
}

public static void autoProcessMessageAndLocation(final HttpServletResponse response,
final String message,
final String location) throws IOException {
initReturnTextHtml(response);

final PrintWriter printWriter = response.getWriter();
setMessageAndLocation(printWriter, message, location);

printWriter.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ public enum AlertMessage {

SUCCESS_DELETE_POST("게시글 삭제 성공"),

FAILED_UPDATE_POST("게시글 수정 실패"),
SUCCESS_UPDATE_POST("게시글 수정 성공"),
FAILED_EDIT_POST("게시글 수정 실패"),
SUCCESS_EDIT_POST("게시글 수정 성공"),

NOT_EXIST_POST("게시글이 존재하지 않습니다."),

FAILED_UPLOAD_COMMENT("댓글 작성 실패"),
FAILED_UPDATE_COMMENT("댓글 수정 실패"),
FAILED_EDIT_COMMENT("댓글 수정 실패"),

FAILED_LOGIN("아이디 또는 비밀번호가 일치하지 않습니다."),

Expand All @@ -34,7 +34,7 @@ public enum AlertMessage {
this.message = message;
}

public String getMessage() {
public String toString() {
return message;
}
}
37 changes: 37 additions & 0 deletions board-servlet-jsp/board/src/com/common/enums/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.common.enums;

public enum Location {
BOARD_JSP("board.jsp"),
UI_BOARD("/boardUI"),
UI_BOARD_REDIRECT("/board/boardUI"),

SEARCH_POST("/postSearchServlet"),

POST_DETAIL_JSP("postDetail.jsp"),
UI_POST_DETAIL("/board/postDetailUI"),

POST_CREATION_JSP("postCreation.jsp"),
UI_POST_CREATION("/board/postCreationUI"),

POST_EDITOR_JSP("postEditor.jsp"),

LOGIN_USER_JSP("loginUser.jsp"),
UI_LOGIN_USER("/board/loginUserUI"),

MY_PROFILE("myProfile.jsp"),
UI_MY_PROFILE("/board/myProfileUI"),

MEMBER_REGISTRATION_JSP("memberRegistration.jsp"),
REGISTER_UI("/board/memberRegistrationUI"),
SUCCESS_REGISTER_MEMBER_JSP("registerSuccess.jsp");

private final String location;

Location(final String location) {
this.location = location;
}

public String toString() {
return location;
}
}
39 changes: 0 additions & 39 deletions board-servlet-jsp/board/src/com/common/enums/SitePath.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.TextHtmlUtil;
import com.dto.MemberDTO;
import com.service.MemberService;
import com.service.MemberServiceImpl;

@SuppressWarnings("serial")
@WebServlet("/duplicationNameCk")
public class DuplicationNameCkServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

final String name = request.getParameter("name");

final MemberService service = new MemberServiceImpl();
final MemberDTO dto = service.getSameNameMember(name);

response.setContentType("text/plain;charset=utf-8");
TextHtmlUtil.initReturnTextHtml(response);
final PrintWriter out = response.getWriter();

if(dto == null) {
out.print("success");
}else {
out.print("fail");
return;
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);

out.print("fail");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.TextHtmlUtil;
import com.dto.MemberDTO;
import com.service.MemberService;
import com.service.MemberServiceImpl;

@SuppressWarnings("serial")
@WebServlet("/duplicationUseridCk")
public class DuplicationUseridCkServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

final String userid = request.getParameter("userid");

final MemberService service = new MemberServiceImpl();
final MemberDTO dto = service.getSameUseridMember(userid);

response.setContentType("text/plain;charset=utf-8");
TextHtmlUtil.initReturnTextHtml(response);
final PrintWriter out = response.getWriter();

if(dto == null) {
out.print("success");
}else {
out.print("fail");
return;
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);

out.print("fail");
}

}
Loading

0 comments on commit e4a6189

Please sign in to comment.