tiles개념은 이전 포스팅 참고 Struts2 - tiles : https://wiper2019.tistory.com/entry/Struts2-tiles
필요 라이브러리 추가
1. layout.jsp 작성
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %> <% request.setCharacterEncoding("UTF-8"); String cp = request.getContextPath(); %> <!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>Insert title here</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> |
2. header.jsp 작성
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% request.setCharacterEncoding("UTF-8"); String cp = request.getContextPath(); %> <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>| |
3. footer.jsp 작성
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% request.setCharacterEncoding("UTF-8"); String cp = request.getContextPath(); %> <margue> <h3>스프링 타일즈</h3> </margue> |
4. dispatcher servlet.xml 작성
프론트 컨트롤러에서 tiles 작성. 뷰리졸버 다음으로 작성하게 되면 처리가 진행되지 않는다. tiles를 뷰 리졸버보다 먼저 설정해야함을 유의하자.
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.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> 타일즈를 사용할 것이라면 viewResolver 객체를 생성하지 않는게 나음. 주석처리!! <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> |
5. tiles.xml (web.xml 경로와 동일해야 함)
tiles는 우리가 지정한 가상의 주소로 접근한다. 직접 접근하는 컨트롤러의 내용은 처리가 안된다는 점을 유의하자.
<?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"> <put-attribute name="header" value="/layout/header.jsp" /> <put-attribute name="body" value="/main.jsp"/> <put-attribute name="footer" value="/layout/footer.jsp"/> </definition> <!-- 확장판 --> <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> |
6. jsp페이지 생성
레이아웃에 html 태그가 들어가므로 본문내용만 기입하였다. guest.jsp <h1>방명록입니다.</h1> write.jsp <h1>글쓰기입니다.</h1> list.jsp <h1>리스트입니다.</h1> |
7. 컨트롤러 생성
프로젝트 진행시 팀원들 각각이 만든 컨트롤러들을 하나로 합치기 때문에 많은 컨트롤러가 생기게 된다.
① MainController
package com.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller("demo.mainController") public class MainController { @RequestMapping(value="/main.action",method={RequestMethod.GET,RequestMethod.POST}) public String method(){ return "mainLayout"; //원래는 특정 jsp페이지나 redirect되는 메소드를 기재했음. tiles를 적용할 것이므로 <tiles-definitions>으로 정의한 tiles이름으로 작성 } } |
② BoardController
package com.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller("demo.boardController") public class BoardController { @RequestMapping(value="/demo/bbs/list.action",method={RequestMethod.GET,RequestMethod.POST}) public String list() throws Exception{ return "list.bbsLayout"; }
@RequestMapping(value="/demo/bbs/write.action",method={RequestMethod.GET,RequestMethod.POST}) public String write() throws Exception{ return "write.bbsLayout"; } } |
③ GuestController
package com.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller("demo.guestController") public class GuestController {
@RequestMapping(value="/demo/guest/guest.action",method={RequestMethod.GET,RequestMethod.POST}) public String guest() throws Exception{ return "guest.guestLayout"; } } |
8. 출력 페이지
'Dev > Spring' 카테고리의 다른 글
Spring3.0 - 제어의역전,의존성주입 복습 (0) | 2019.04.15 |
---|---|
Spring 3.0 셋팅 (0) | 2019.04.15 |
Spring2.5 - 파일게시판(업로드/다운로드/삭제) (0) | 2019.04.08 |
Spring2.5 - 게시판 만들기 (0) | 2019.04.08 |
Spring2.5 - Annotation (0) | 2019.04.08 |