티스토리 뷰

spring

spring5 vue webpack

개몽구리 2019. 1. 24. 18:07

spring5 vue.js webpack 구성 프로젝트 설정


step 1 ) maven 프로젝트 생성

Webcontent 구조가 아닌 이전 처럼 디렉토리 구조를 생성



step 2 ) pom.xml 설정


webpack을 통해 빌드된 html로 뷰를 설정하기 위해 thymeleaf 를 추가 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    <!-- thymeleaf -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.9.RELEASE</version>
     </dependency>
     
     <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.9.RELEASE</version>
    </dependency>
     
     
     <!-- Thymeleaf lagacy html5를 위한 라이브러리 -->
     <!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml -->
     <dependency>
        <groupId>net.sourceforge.nekohtml</groupId>
        <artifactId>nekohtml</artifactId>
        <version>1.9.22</version>
     </dependency>
cs


step 3 ) servlet-context.xml 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    <!-- Thymeleaf view 설정  -->
    <beans:bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".html" />
        <beans:property name="templateMode" value="HTML5" />
        <beans:property name="characterEncoding" value="UTF-8" />
        <beans:property name="cacheable" value="false" />
    </beans:bean>
    
    <beans:bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
        <beans:property name="templateResolver" ref="templateResolver" />
        <beans:property name="enableSpringELCompiler" value="true" />
    </beans:bean>
    
    <beans:bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <beans:property name="characterEncoding" value="UTF-8" />
        <beans:property name="templateEngine" ref="templateEngine" />
    </beans:bean>
cs

prefix 경로가 webpack으로 빌드된 html이 저장 될 위치 



step 4 ) frontend 설정 


node.js 설치 https://nodejs.org/ko/


front와 관련한 내용들을 담을 frontend 디렉토리를 src와의 같은 경로에 생성 후

프로젝트 루트 경로에서 npm install -- g vue-cli  실행 (vue-cli 설치)


다음 vue init webpack frontend 실행 (프론트 디렉토리명) 


frontend 디렉토리로 이동 후 

패키지들을 설치하기 위해 npm install 실행 


아래와 같이 경고나는 부분은 파악 못함


step 5 ) webpack config 설정

frontend / config 디렉토리의 index.js 수정


1
2
3
4
5
6
7
8
  build: {
    // Template for index.html
    index: path.resolve(__dirname, '../../src/main/webapp/WEB-INF/views/index.html'),
 
    // Paths
    assetsRoot: path.resolve(__dirname, '../../src/main/webapp/resources'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/cms/resources/',
cs

index : 빌드될 index.html 경로 위치

assetsRoot : 빌드될 css js 의 위치

assetPublicPath : 빌드될 html에서의 css js의 경로 (보통 static루트에 html이 위치함으로 기본설정은 '/')


step 6 )  run build 빌드


frontend 위치에서 npm run build 실행



main.do 의 경로 맵핑 controller

1
2
3
4
5
6
7
8
9
10
11
@Controller
public class MainController {
    
    private String PRE_VIEW_PATH = "";
    
    @RequestMapping(value="main.do")
    public String mainDo() {  
        return  PRE_VIEW_PATH+"index";
    }//
}
cs


tomcat 실행 후 확인 (localhost:톰캣서버포트/경로)

-

프론트 적용후 빌드 시키지 않고 프론트 서버에서 확인하려면 npm run dev 실행으로 바로바로 확인할 수 있다.


실행 포트 변경은 config / index.js (빌드 경로 설정했던 파일) 에서


1
2
3
4
5
6
7
// Various Dev Server settings
    host: 'localhost'// can be overwritten by process.env.HOST
    port: 8080// can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false// https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
cs


port부분 수정후 frontend 경로에서  npm run dev 실행 

localhost:위부분에서 변경된포트번호 /경로 로 브라우저에서 바로바로 확인 할 수 있다.

수정 후에는 빌드 실행해야 톰캣서버에 반영이 된다.



spring5 mvc 가 아닌 spring-boot vue webpack 연동으로 만들어본 샘플 (환율계산기)

https://github.com/pikaSoYoung/converter




참고 사이트 - 

https://febdy.tistory.com/65         



'spring' 카테고리의 다른 글

spring lucy-xss-servlet-filter 적용  (0) 2019.01.23
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함