FRAMEWORK/SITEMESH

sitemesh 예제

적외선 2014. 9. 25. 10:38

코드

공통 설정 및 자바 코드

 - web.xml 추가 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<filter>
       <filter-name>sitemesh</filter-name>
       <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
  </filter>
  <filter-mapping>
       <filter-name>sitemesh</filter-name>
       <url-pattern>/*</url-pattern>
  </filter-mapping>
   
  <jsp-config>
      <taglib>
       <taglib-uri>sitemesh-page</taglib-uri>
       <taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location>
      </taglib>
      <taglib>
       <taglib-uri>sitemesh-decorator</taglib-uri>
       <taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location>
      </taglib>
  </jsp-config>

 

- sitemesh.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
  <property name="decorators-file" value="/WEB-INF/decorators.xml"/>
  <excludes file="${decorators-file}"/>
  <page-parsers>
    <parser content-type="text/html"
        class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
  </page-parsers>
  <decorator-mappers>
    <mapper
        class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
      <param name="property.1" value="meta.decorator" />
      <param name="property.2" value="decorator" />
    </mapper>
    <!-- Mapper for localization -->
    <mapper
        class="com.opensymphony.module.sitemesh.mapper.LanguageDecoratorMapper">
      <param name="match.en" value="en" />
      <param name="match.zh" value="zh" />
    </mapper>
    <!-- Mapper for browser compatibility -->
    <mapper
        class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
      <param name="match.MSIE" value="ie" />
      <param name="match.Mozilla/" value="ns" />
    </mapper>
    <mapper
        class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
      <param name="decorator" value="printable" />
      <param name="parameter.name" value="printable" />
      <param name="parameter.value" value="true" />
    </mapper>
    <mapper
        class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
      <param name="decorator.parameter" value="decorator" />
      <param name="parameter.name" value="confirm" />
      <param name="parameter.value" value="true" />
    </mapper>
    <mapper
        class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
      <param name="config" value="${decorators-file}" />
    </mapper>
  </decorator-mappers>
</sitemesh>

 

- decorators.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/views/deco">
 
    <decorator name="top" page="top.jsp"/>
    <decorator name="left" page="left.jsp"/>
    <decorator name="right" page="right.jsp"/>
    <decorator name="bottom" page="bottom.jsp"/>
     
    <decorator name="layout" page="decolayout.jsp">
        <pattern>*layout*</pattern>
    </decorator>
     
</decorators>

 

- SiteMeshContoller.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.fbwotjq;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
public class SiteMeshContoller {
     
    @RequestMapping(value = "/applyDecorator" , method = RequestMethod.GET)   
    public String applyDecorator(){
        return "applyDecorator";
    }
     
    @RequestMapping(value = "/layoutCall" , method = RequestMethod.GET)   
    public String notApplyDecorator(){
        return "layoutCall";
    }
     
}

 


첫번쨰 방법 화면 관련 코드 

 - applyDecorator.jsp

 



 

 - bottom.jsp

 



 

 - left.jsp

 



 

 - right.jsp

 



 

 - top.jsp

 



 

두번째 방법 화면 관련 코드  

-  layoutCall.jsp(호출 페이지)

 



 

- decolayout.jsp(레이아웃페이지)

 




결과

- 첫번쨰 결과

 



- 두번쨰 결과

 



 


출처 - http://wannastop.tistory.com/409



sitemesh.xml

sitemesh.xml은 decorator mapper 리스트가 설정되어 있다. 만일 Sitemesh가 해당 경로에서 설정 파일을 찾는데 실패하면sitemesh.jar 에 패키징 되어 있는 sitemesh-default.xml 파일로 대체된다.

sitemesh.xml
<sitemesh>
    <property name="decorators-file" value="/WEB-INF/decorators.xml" />

    <page-parsers>
         <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
    </page-parsers>

    <excludes file="${decorators-file}" />

    <decorator-mappers>
	<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
	    <param name="property.1" value="meta.decorator" />
	    <param name="property.2" value="decorator" />
	</mapper>

	<mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper">
	</mapper>

	<mapper class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
		<param name="match.MSIE" value="ie" />
		<param name="match.Mozilla [" value="ns" />
		<param name="match.Opera" value="opera" />
		<param name="match.Lynx" value="lynx" />
	</mapper>

	<mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
		<param name="decorator" value="printable" />
		<param name="parameter.name" value="printable" />
		<param name="parameter.value" value="true" />
	</mapper>

	<mapper class="com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper">
		<param name="decorator" value="robot" />
	</mapper>

	<mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
		<param name="decorator.parameter" value="decorator" />
		<param name="parameter.name" value="confirm" />
		<param name="parameter.value" value="true" />
	</mapper>

	<mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper">
	</mapper>

	<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
		<param name="config" value="${decorators-file}" />
	</mapper>

	</decorator-mappers>
</sitemesh>
  • page parsers
    <page-parsers> 엘리먼트는 content-type에 따라 적용할 파서를 정의한다. SiteMesh는 현재 text/html을 파싱할 때 사용되는 FastPage Parser만 구현되어 있다. 나머지에 대해서는 디폴트로 DefaultPageParser가 사용가능하다.
    <page-parsers>
    	<parser default="true" class="com.opensymphony.module.sitemesh.parser.DefaultPageParser" />
    	<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
    </page-parsers>
  • decorator mapper
    decorator mapper는 Sitemesh에게 장식에 사용될 적합한 decorator를 설정해주는 역할을 한다. <decorator-mapper> 엘리먼트는 decorator mapper들을 정의한다. decorator mapper 추상 클래스는 링크드 리스트로 정의되어 있고, 적혀진 순서에 따라 decorator 체인으로 만들어진다.
    <decorator-mappers>
    	<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
    		<param name="property.1" value="meta.decorator" />
    		<param name="property.2" value="decorator" />
    	</mapper>
    </decorator-mappers>
    ....
  • excludes
    특정 웹 페이지(jsp)들을 모아 decoration이 적용되지 않도록 하는 설정파일을 지정한다.
    <excludes file="/WEB-INF/decorators.xml"/>
  • property
    변수화하여 쉽게 사용할 수 있도록 한다.
    <property name="decorators-file" value="/WEB-INF/decorators.xml" />
    ...
    <excludes file="${decorators-file}" />
    ...
    <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
    	<param name="config" value="${decorators-file}" />
    </mapper>

ConfigDecoratorMapper는 decorator 파일들을 config로 지정할 수 있음을 의미하며, 여러 개의 config 파일을 포함할 수 있다.



SiteMesh 구조와 흐름

  1. request 단계
    모든 request는 PageFilter에서 가로채어진다. PageFilter는 Context 초기화 시점에 decorator.xml에 정의된 <exclude> 태그에 따라 요청된 request의 url-pattern에 따라 장식을 수행할지 여부를 결정한다.
  2. paring 단계
    PageFilter는 요청된 자원에 대한 웹 어플리케이션의 수행결과를 do(request, respose) 메소드를 호출하여 받아낸다. 응답 파라미터에는 ServletResponse가 아닌 HttpServletResponseWrapper를 상속받아 확장한 PageResponseWrapper 객체를 사용한다.
    따라서 페이지 아웃풋은 writer로 보내지 않고 content-type에 따라 선택된 파서가 PageResponseWrapper에서 캡처된 page 아웃풋 데이터를 파싱한다. 현재는 반환 page의 content-type이 text/html일 경우만 FastPageParser가 작동한다.
  3. decorator 선정 단계
    적합한 decorator를 결정한다. 이 과정은 mapper 클래스의 getDecorator()에 의해서 이뤄진다. 처음 decorator 선정을 시도한 mapper 클래스가 적합한 decorator를 얻어낸다면 리턴하고, 아니면 decorator 체인상의 상위 링크의 getDecorator() 메소들르 호출하게 된다. 정확한 decorator가 결정될 때까지 반복된다.
  4. decorator 단계
    decorator를 얻오고, decorator로부터 decorator page를 구해 올 수 있다면, PageFilter는 applyDecorator()내에서 decorator 페이지에 대한 요청을 생성하고, decorator page는 FastPageParser에 의해 파싱된 결과 page의 적절한 부분들을 커스텀 태그를 통해 가져와 출력 페이지를 작성한다.

출처 - http://knight76.tistory.com/entry/30021375355