Tiles(타일즈)
○ 필요한 라이브러리
commons-beanutils-1.7.0.jar
commons-digester-2.0.jar
struts2-tiles-plugin-2.3.1.1.jar
tiles-api-2.0.6.jar
tiles-core-2.0.6.jar
tiles-jsp-2.0.6.jar
○ 타일즈는 다이렉트 주소로 움직이는 것이 아닌 가 상의 주소로 이동
- 타일즈를 사용하면 다이렉트로 이동하는 주소는 사용 불가
1.tiles.xml
○ WEB-INF 파일 안에 생성
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="mainLayout" template="/layout/layout.jsp">
<!-- header에 layout/header.jsp를 넣어라 -->
<put-attribute name="header" value="/layout/header.jsp" />
<put-attribute name="body" value="/main.jsp" />
<put-attribute name="footer" value="/layout/footer.jsp" />
</definition>
<!-- name은 사용자 정의 -->
<!-- mainLayout의 확장판 -->
<!-- list.bbsLayout이 오면 mainLayoyt을 사용해라 -->
<!-- body부분에 들어가는 파일은 demo/bbs/list.jsp -->
<definition name="list.bbsLayout" extends="mainLayout">
<put-attribute name="body" value="/demo/bbs/list.jsp"/>
</definition>
<definition name="write.bbsLayout" extends="mainLayout">
<put-attribute name="body" value="/demo/bbs/write.jsp"/>
</definition>
<definition name="guest.guestLayout" extends="mainLayout">
<put-attribute name="body" value="/demo/guest/guest.jsp"/>
</definition>
</tiles-definitions>
2. dispatcher-servlet.xml
○ viewResolver 밑에다가 타일즈를 작성하면 작동하지 않는다.
○ 타일즈 설정
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="ViewClass" value="org.springframework.web.servlet.tiles2.TilesView"/>
</bean>
○ 타일즈 설정 파일의 위치를 알려주는 부분
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
3. jsp파일
▶ 별도의 코딩없이 html부분을 지우고 <h1>□□□ 화면입니다</h1> 작성
① demo > bbs
○ list.jsp
○ write.jsp
② demo > guest
○ guest.jsp
③ layout
○ footer.jsp
○ header.jsp(예외)
<a href="<%=cp%>/main.action">메인</a> |
<a href="<%=cp%>/demo/bbs/list.action">게시판</a> |
<a href="<%=cp%>/demo/bbs/write.action">글쓰기</a> |
<a href="<%=cp%>/demo/guest/guest.action">방명록</a>
○ layout.jsp (예외)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>스프링 타일즈</title>
</head>
<body>
<table width="600" align="center">
<tr height="50">
<td><tiles:insertAttribute name="header"/></td>
</tr>
<tr height="400">
<td><tiles:insertAttribute name="body"/></td>
</tr>
<tr height="50">
<td><tiles:insertAttribute name="footer"/></td>
</tr>
</table>
</body>
</html>
④ main.jsp(예외)
<body>
<h1>메인입니다</h1>
<hr/><br/>
1. <a href="<%=cp%>/demo/write.action">create</a><br/>
2. <a href="<%=cp%>/demo/save.action">save</a><br/>
3. <a href="<%=cp%>/demo/demo.action">demo</a><br/>
</body>4. Controller 작성
○ 각각 controller를 작성
① MainController.java : 메인을 보여줄 컨트롤러
@Controller("demo.mainController")
public class MainController {
@RequestMapping(value="/main.action", method={RequestMethod.GET,RequestMethod.POST})
public String method(){
//tiles.xml의 mainLayout을 찾아가도록 설정
return "mainLayout";
}
}
② BoardController.java : 게시판을 보여줄 컨트롤러(/demo/bbs~~)
@Controller("demo.boardController")
public class BoardController {
//return으로 가는 곳은 tiles.xml의 list.~~
@RequestMapping(value="/demo/bbs/list.action",method={RequestMethod.GET,RequestMethod.POST})
public String list() throws Exception {
/* 원래 가는 곳 : return "/demo/bbs"; */
return "list.bbsLayout";
}
@RequestMapping(value="/demo/bbs/write.action",method={RequestMethod.GET,RequestMethod.POST})
public String write() throws Exception {
/* 원래 가는 곳 : return "/demo/bbs"; */
return "write.bbsLayout";
}
}
③ GuestController.java : 방명록을 보여줄 컨트롤러(/demo/guest~~)
@Controller("demo.guestController")
public class GuestController {
@RequestMapping(value="/demo/guest/guest.action",method={RequestMethod.GET,RequestMethod.POST})
public String guest() throws Exception {
/* 원래 가는 곳 : return "/guest/guest"; */
return "guest.guestLayout";
}
}
'STUDY > SPRING' 카테고리의 다른 글
스프링(3.0) 프로젝트 생성 (0) | 2019.04.15 |
---|---|
스프링(3.0) 다운로드 (0) | 2019.04.15 |
[spring 2.5]파일 업로드 (0) | 2019.04.09 |
[spring 2.5]게시판 (0) | 2019.04.08 |
[spring 2.5]어노테이션을 사용한 스프링 기초예제 (0) | 2019.04.08 |
댓글