스프링 기본 프로젝트는 UTF-8로 만들어지지 않기 때문에 기본 프로젝트를 생성하면
한글이 ? 로 나오거나 이상한 문자로 나옵니다.
그러므로 web.xml 맨아래에 아래와 같은 코드를 추가해줍니다.
<!-- encoding UTF-8로 맞춰주기 위한 설정 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<servlet-name>appServlet</servlet-name>
</filter-mapping>
</web-app>
Tiles Framework란 웹 페이지의 상단, 하단, Header, Footer, left 등 반복적으로 사용되는 부분들에 대한 정보를 모아서 관리해줍니다.
내정보 창에서는 body부분에만 변화를주기위해 Tiles Framework를 사용할 것 입니다.
Tiles Framework를 사용하기위해서는 아래와 같은 설정들을 해주어야 합니다.
<!-- tiles 설정 -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>3.0.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-core -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>3.0.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-api -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>3.0.7</version>
</dependency>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
<beans:property name="order" value="2" />
</beans:bean>
<!-- Tiles -->
<beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<beans:property name="definitions">
<beans:list>
<beans:value>/WEB-INF/views/tiles/tiles-layout.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
<beans:property name="order" value="1" />
</beans:bean>
<!-- /Tiles -->
기존의 ViewResolver보다 우선순위가 높아야하므로 order을 1로 set해준다.
기존의 ViewResolver는 우선순위를 2로 해준다.
<beans:value>/WEB-INF/views/tiles/tiles-layout.xml</beans:value>
위 경로에 tiles 폴더안에 타일들을 정의하기 위한 tiles-layout.xml을 만들어준다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="layout" template="/WEB-INF/views/tiles/layout.jsp">
<put-attribute name="header" value="/WEB-INF/views/tiles/header.jsp"/>
<put-attribute name="menu" value="/WEB-INF/views/tiles/menu.jsp"/>
<put-attribute name="content" value=""/>
<put-attribute name="footer" value="/WEB-INF/views/tiles/footer.jsp"/>
</definition>
<definition name="/tiles/*" extends="layout">
<put-attribute name="content" value="/WEB-INF/views/tiles/{1}.jsp"/>
</definition>
</tiles-definitions>
template인 jsp에 페이지 모듈을 붙여준다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="/resources/css/styles.css" rel="stylesheet" />
<link href="/resources/css/user/myinfo.css" rel="stylesheet" />
<title>내 정보</title>
</head>
<body>
<tiles:insertAttribute name="header"/>
<div id="wrap" class="wrap shoppingid">
<tiles:insertAttribute name="menu"/>
<div id = "bodyTile">
<tiles:insertAttribute name="content"/>
</div>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script src="/resources/js/scripts.js"></script>
</html>