티스토리 뷰

1. 자바

자바/스프링/2020-06-25/ 타일즈 설정

패스트코드블로그 2020. 6. 25. 15:05

다른 스프링 설정은 완성된 상태에서 타일즈만 추가하는 설정법이다.

 

 

메이븐에서 다음과 같이 타일즈를 DI 한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
       <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>${tiles-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-servlet</artifactId>
            <version>${tiles-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-extras</artifactId>
            <version>${tiles-version}</version>
        </dependency>
cs

 

 

1. root-context.xml 을 오픈한 뒤 doc-type 부분을 카피한다

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- Root Context: defines shared resources visible to all other web components -->
        
</beans>
cs

 

2. 해당 내용을 servlet-context.xml 에 덮어쓴다.

(에러 발생하는 부분은 일단 패스)

아래 네임스페이스 탭을 누르고 이동한 후 jee 를 제외한 모두 체크한다.

3. 다시 servlet-context.xml 로 온다. 아래와 같이 <beans:beans> 부분 같은 에러나는 태그를 정리한다.

 

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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
 
    <mvc:annotation-driven /> 
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/WEB-INF/views"/>
                    <property name="suffix" value=".jsp"/>
    </bean>
    <context:component-scan base-package="com.parksrazor.web" />
 
</beans>
cs

 

viewResolver 빈을 아래와 같이 3개의 타일즈 포함한 여러 리졸브로 바꾼다. (나중에 이미지 파일, 동영상 파일 처리때 필요하므로 지금 설정을 같이 한다.

ContentNegotiatingViewResolver 빈객체다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                    <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
                </bean>
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
                <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/WEB-INF/views"/>
                    <property name="suffix" value=".jsp"/>
                </bean>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean>
            </list>
        </property>
    </bean>
cs

ContentNegotiationManager 빈객체다.

1
2
3
4
5
6
7
8
9
10
11
12
13
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManager">
           <constructor-arg>
                <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                     <constructor-arg>
                     <map>
                        <entry key="atom">
                             <util:constant static-field="org.springframework.http.MediaType.APPLICATION_ATOM_XML" />  
                        </entry>
                     </map>
                     </constructor-arg>
                </bean>
           </constructor-arg>
      </bean>
cs

TilesConfigurer 빈객체다.

1
2
3
4
5
6
7
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>
cs

 

ContentNegotiatingViewResolver 빈객체 설정할 때 6번라인에서 TilesView를 설정하는 것에 주의 한다.

TilesViewResolver 로 자동완성된 상태로 진행하다가 많은 시간 삽질을 하게 된다.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/

 

Spring Framework 5.2.7.RELEASE API

 

docs.spring.io

 

servlet-context.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
 
    <mvc:annotation-driven /> 
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                    <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
                </bean>
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
                <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/WEB-INF/views"/>
                    <property name="suffix" value=".jsp"/>
                </bean>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean>
            </list>
        </property>
    </bean>
      <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManager">
           <constructor-arg>
                <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                     <constructor-arg>
                     <map>
                        <entry key="atom">
                             <util:constant static-field="org.springframework.http.MediaType.APPLICATION_ATOM_XML" />  
                        </entry>
                     </map>
                     </constructor-arg>
                </bean>
           </constructor-arg>
      </bean>
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>
    <context:component-scan base-package="com.parksrazor.web" />
 
</beans>
cs

 

다음은 위에서 설정한 대로 /WEB-INF/tiles.xml 를 작성해야 한다.

WEB-INF 에서 우클릭 > New > 아래와 같이 선택한 후 tiles.xml 이라고 네이밍한 후 생성한다.

 

일단 tiles.xml 에 다음과 같이 입력한다.

1
2
3
4
5
6
7
8
9
10
11
12
<?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="noTiles" template="/WEB-INF/views/layouts/Public.jsp">
           <put-attribute name="noTemplate" value="" />
    </definition>
    <definition name="*.jsp" extends="noTiles">
        <put-attribute name="noTemplate" value="/WEB-INF/views/{1}.jsp" />
    </definition>
</tiles-definitions>
cs

 

Public.jsp 는 다음과 같다.

 

1
2
3
4
5
6
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<div>
    <tiles:insertAttribute name="noTemplate"/>
</div>
 
cs

 

Home.jsp 입니다. 간단하게 홈페이지 표시만 한다.

1
2
3
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<h1>타일즈의 홈페이지 입니다</h1>
 
cs

HomeController.java 이다.

 

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
package com.parksrazor.web.controllers;
 
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
public class HomeController {
    
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    @GetMapping("/")
    public String home() {
        logger.info("홈컨트롤러 진입");
    
        return "Home.jsp";
    }
    
}
 
cs

 

 

톰캣을 돌려서 다음과 같이 나오면 성공이다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함