Explorar el Código

Spring Boot 에러페이지 관련 viewName 로직 수정

gagamel hace 5 años
padre
commit
a8c0522d64

+ 2 - 46
src/main/java/com/style24/front/support/controller/TsfBaseController.java

@@ -3,7 +3,6 @@ package com.style24.front.support.controller;
 import java.util.Collection;
 import java.util.Set;
 
-import javax.servlet.http.HttpServletRequest;
 import javax.validation.ConstraintViolation;
 import javax.validation.Validation;
 import javax.validation.Validator;
@@ -12,25 +11,17 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.propertyeditors.StringTrimmerEditor;
 import org.springframework.core.env.Environment;
-import org.springframework.mobile.device.Device;
-import org.springframework.mobile.device.DeviceUtils;
-import org.springframework.mobile.device.site.SitePreference;
-import org.springframework.mobile.device.site.SitePreferenceUtils;
-import org.springframework.mobile.device.util.ResolverUtils;
-import org.springframework.util.Assert;
 import org.springframework.web.bind.WebDataBinder;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.InitBinder;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.context.request.RequestAttributes;
-import org.springframework.web.context.request.RequestContextHolder;
-import org.springframework.web.context.request.ServletRequestAttributes;
 
 import com.style24.core.support.message.TscMessageByLocale;
 import com.style24.front.support.env.TsfConstants;
 import com.style24.front.support.security.session.TsfSession;
 import com.style24.front.support.startup.TsfEnvsetInfo;
+import com.style24.front.support.util.ViewUtils;
 import com.style24.persistence.domain.Cate1;
 import com.style24.persistence.domain.Login;
 import com.style24.persistence.domain.Meta;
@@ -40,7 +31,6 @@ import lombok.extern.slf4j.Slf4j;
 import com.gagaframework.web.rest.exception.GagaRestException;
 import com.gagaframework.web.rest.server.GagaResponse;
 import com.gagaframework.web.rest.server.GagaResponseStatus;
-import com.gagaframework.web.util.GagaCookieUtil;
 
 /**
  * Controller Advice
@@ -277,41 +267,7 @@ public class TsfBaseController {
 	}
 
 	public String getDeviceViewName(String viewName) {
-		RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
-		Assert.isInstanceOf(ServletRequestAttributes.class, attrs);
-		HttpServletRequest request = ((ServletRequestAttributes)attrs).getRequest();
-
-		Device device = DeviceUtils.getCurrentDevice(request);
-		SitePreference sitePreference = SitePreferenceUtils.getCurrentSitePreference(request);
-		String resolvedViewName = viewName;
-
-		if (ResolverUtils.isNormal(device, sitePreference)) {
-			resolvedViewName = "web/" + viewName + "Web";
-		} else if (ResolverUtils.isMobile(device, sitePreference)) {
-			if (GagaCookieUtil.getCookie(request, TsfConstants.CK_PREFIX + "_site_preference").equals("normal")) {
-				resolvedViewName = "web/" + viewName + "Web";
-			} else {
-				resolvedViewName = "mob/" + viewName + "Mob";
-			}
-		} else if (ResolverUtils.isTablet(device, sitePreference)) {
-			if (GagaCookieUtil.getCookie(request, TsfConstants.CK_PREFIX + "_site_preference").equals("normal")) {
-				resolvedViewName = "web/" + viewName + "Web";
-			} else {
-				resolvedViewName = "mob/" + viewName + "Mob";
-			}
-		}
-
-		log.info("resolvedViewName: {}", resolvedViewName);
-
-		return stripTrailingSlash(resolvedViewName);
-	}
-
-	private String stripTrailingSlash(String viewName) {
-		if (viewName.endsWith("//")) {
-			return viewName.substring(0, viewName.length() - 1);
-		}
-
-		return viewName;
+		return ViewUtils.getDeviceViewName(viewName);
 	}
 
 }

+ 7 - 4
src/main/java/com/style24/front/support/controller/TsfCustomErrorController.java

@@ -13,10 +13,13 @@ import org.springframework.boot.web.servlet.error.ErrorAttributes;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
+import org.springframework.mobile.device.Device;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.servlet.ModelAndView;
 
+import com.style24.front.support.util.ViewUtils;
+
 import lombok.extern.slf4j.Slf4j;
 
 /**
@@ -51,7 +54,7 @@ public class TsfCustomErrorController extends AbstractErrorController {
 	 * @throws IOException
 	 */
 	@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
-	public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) throws IOException {
+	public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response, Device device) throws IOException {
 		HttpStatus status = this.getStatus(request);
 		log.error("status.value(): {}", status.value());
 
@@ -61,7 +64,7 @@ public class TsfCustomErrorController extends AbstractErrorController {
 		ModelAndView mav = this.resolveErrorView(request, response, status, model);
 
 		if (status.value() == HttpStatus.NOT_FOUND.value()) {
-			return (mav != null) ? mav : new ModelAndView("mob/error/404Mob", model);
+			return (mav != null) ? mav : new ModelAndView(ViewUtils.getDeviceViewName("error/404"), model);
 		}
 
 		Object oExceptionType = request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE);
@@ -71,11 +74,11 @@ public class TsfCustomErrorController extends AbstractErrorController {
 
 			// Thymeleaf의 HTML 파일을 못 찾는 에러는 "org.thymeleaf.exceptions.TemplateInputException"임.
 			if (exceptionType.endsWith("org.thymeleaf.exceptions.TemplateInputException")) {
-				return (mav != null) ? mav : new ModelAndView("mob/error/404Mob", model);
+				return (mav != null) ? mav : new ModelAndView(ViewUtils.getDeviceViewName("error/404"), model);
 			}
 		}
 
-		return (mav != null) ? mav : new ModelAndView("mob/error/500Mob", model);
+		return (mav != null) ? mav : new ModelAndView(ViewUtils.getDeviceViewName("error/500"), model);
 	}
 
 	/**

+ 68 - 0
src/main/java/com/style24/front/support/util/ViewUtils.java

@@ -0,0 +1,68 @@
+package com.style24.front.support.util;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.springframework.mobile.device.Device;
+import org.springframework.mobile.device.DeviceUtils;
+import org.springframework.mobile.device.site.SitePreference;
+import org.springframework.mobile.device.site.SitePreferenceUtils;
+import org.springframework.mobile.device.util.ResolverUtils;
+import org.springframework.util.Assert;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import com.style24.front.support.env.TsfConstants;
+
+import lombok.extern.slf4j.Slf4j;
+
+import com.gagaframework.web.util.GagaCookieUtil;
+
+/**
+ * 디바이스 뷰 관련 Util Class
+ * 
+ * @author gagamel
+ * @since 2021. 3. 30
+ */
+@Slf4j
+public class ViewUtils {
+
+	public static String getDeviceViewName(String viewName) {
+		RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
+		Assert.isInstanceOf(ServletRequestAttributes.class, attrs);
+		HttpServletRequest request = ((ServletRequestAttributes)attrs).getRequest();
+
+		Device device = DeviceUtils.getCurrentDevice(request);
+		SitePreference sitePreference = SitePreferenceUtils.getCurrentSitePreference(request);
+		String resolvedViewName = viewName;
+
+		if (ResolverUtils.isNormal(device, sitePreference)) {
+			resolvedViewName = "web/" + viewName + "Web";
+		} else if (ResolverUtils.isMobile(device, sitePreference)) {
+			if (GagaCookieUtil.getCookie(request, TsfConstants.CK_PREFIX + "_site_preference").equals("normal")) {
+				resolvedViewName = "web/" + viewName + "Web";
+			} else {
+				resolvedViewName = "mob/" + viewName + "Mob";
+			}
+		} else if (ResolverUtils.isTablet(device, sitePreference)) {
+			if (GagaCookieUtil.getCookie(request, TsfConstants.CK_PREFIX + "_site_preference").equals("normal")) {
+				resolvedViewName = "web/" + viewName + "Web";
+			} else {
+				resolvedViewName = "mob/" + viewName + "Mob";
+			}
+		}
+
+		log.info("resolvedViewName: {}", resolvedViewName);
+
+		return stripTrailingSlash(resolvedViewName);
+	}
+
+	private static String stripTrailingSlash(String viewName) {
+		if (viewName.endsWith("//")) {
+			return viewName.substring(0, viewName.length() - 1);
+		}
+
+		return viewName;
+	}
+
+}

+ 8 - 1
src/main/webapp/WEB-INF/views/mob/error/404Mob.html

@@ -57,10 +57,17 @@
 				</p>
 			</div>
 			<div class="btn_group">
-				<button class="btn btn_dark" onclick="cfnGoToPage(_PAGE_MAIN);"><span>홈으로 이동</span></button>
+				<button class="btn btn_dark" onclick="fnGoToHome();"><span>홈으로 이동</span></button>
 			</div>
 		</div>
 	</div>
 	<!-- // container -->
+
+<script type="text/javascript">
+	let fnGoToHome = function() {
+		document.location.href = '/';
+	}
+</script>
+
 </body>
 </html>

+ 9 - 2
src/main/webapp/WEB-INF/views/web/error/404Web.html

@@ -51,16 +51,23 @@
 			<div class="contGrp">
 				<h2>죄송합니다.<br/> 요청하신 페이지를 찾을 수 없습니다.</h2>
 				<p class="none_m">방문하시려는 페이지의 주소가 잘못 입력되었거나,<br/>페이지의 주소가 변경 혹은 삭제되어 요청하신 페이지를 찾을 수 없습니다. </p>
-				<p>입력하신 주소가 정확한지 다시한번 확인해주시기 바랍니다</p>
+				<p>입력하신 주소가 정확한지 다시  번 확인해 주시기 바랍니다.</p>
 				<p class="none_m">
 					서비스 이용 중 문의사항이나 불편하셨던 점은 <a class="none_m"  href="javascript:void(0);" onclick="cfnGoToPage(_PAGE_FAQ);">고객센터</a>를 이용해주세요. 감사합니다.
 				</p>
 			</div>
 			<div class="btn_group">
-				<button class="btn btn_dark" onclick="cfnGoToPage(_PAGE_MAIN);"><span>홈으로 이동</span></button>
+				<button class="btn btn_dark" onclick="fnGoToHome();"><span>홈으로 이동</span></button>
 			</div>
 		</div>
 	</div>
 	<!-- // container -->
+
+<script type="text/javascript">
+	let fnGoToHome = function() {
+		document.location.href = '/';
+	}
+</script>
+
 </body>
 </html>