MCV 프로그램을 신속하게 구축하는 방법은 무엇입니까?
공식 스프링 예를 참조하십시오 : https://spring.io/guides/gs/serving-web-content/
1. 스프링 MVC는 Thymeleaf 템플릿과 결합되었습니다
Maven 프로젝트를 작성한 후 pom.xml 파일을 수정하십시오
<project xmlns = "http://maven.apache.org/pom/4.0.0"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xsi : schemalocation = "http://maven.apache.org/pom/0.0.0.0 http://maven.apache.org/xsd/maven-4.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.github.carter659 </groupid> <artifactid> spring02 </artifactid> <bersion> 0.0.1-1-1-snapshot> <-1-snapshot> <groupid> org.springframework.boot </groupid> <artifactid> Spring-Boot-Starter-Parent </artifactid> <bersion> 1.4.2.2.2.2.2.2.20 </version> </parent> <name> spring02 </name> <url> http://maven.apache.org </url> <trerties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <pectionency> <groupid> org.spramework.spramework.spramework.sprameword> spring-hoot-starter-starter-starter-stimelebe- spring-theMeleceR-stiMELEBER-SPRICELER-STAROTER-STAROTID> </의존성> <pectionency> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-devtools </artifactid> <seleptional> true </옵션> </dependency> </fexendencies> <flugins> <groupin> <groupin> org.springframework.springframework.boot. <Artifactid> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </build> </project>
컨트롤러 "maincontroller.java"의 클래스 파일을 추가하십시오.
package com.github.carter659.spring02; import org.springframework.stereotyp.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.getmapping; @controllerpublic maintroller { "/") {@getmapping ( "/") model.addattribute ( "이름", "liu dong"); "색인"을 반환합니다. }}app.java 파일을 수정하십시오
package com.github.carter659.spring02; import org.springframework.boot.springApplication; import org.spramewort.autoconfigure.springbootApplication; @springbootApplicationPublic class app {public static void main (String) {spring plusts, applass.); }}그런 다음 프로젝트에서 마우스 오른쪽 버튼을 클릭하여 Java 빌드 경로로 들어갑니다.
폴더 "및 폴더"추가
기본 디렉토리에 "리소스"폴더를 추가하십시오
"리소스"의 "제외"수정 :
입력하다"**"
SRC/Main/Resources에서 "Templates"폴더를 작성하고 새 HTML 파일 "Index.html"을 만듭니다.
<! doctype html> <html xmlns : th = "http://www.thymeleaf.org"> <head> <meta http-equiv = "content-type"content = "text/html; charset = utf-8"/> <title> mvc </head> <pt : aft = "" ""텍스트> $ {name} + '!' "/> </body> </html> http : // localhost : 8080을 입력하여 성공적으로 실행되는지 확인하십시오.
위는 Thymeleaf 템플릿을 사용하여 만든 동적 페이지입니다. 그렇다면 MVC 애플리케이션에서 정적 자원을 사용하는 방법은 무엇입니까?
2. 정적 자원
SRC/Main/Resources에서 새로운 "정적"폴더를 만듭니다
폴더에 그림 파일을 복사하십시오
이전 "index.html"파일을 수정하고 IMG 태그 추가
<! docType html> <html xmlns : th = "http://www.thymeleaf.org"> <head> <meta http-equiv = "content-type"content = "text/html; charset = utf-8"/> <title> mvc </head> </head> </head> </head> /> <p th : text = " 'hello,' + $ {name} + '!'" /> < /body> < /html>현재 현상이 즉시 나타납니다.
Maven의 "DevTools"에 따라 프로그램이 자동으로로드 될 것임을 발견했습니다.
마지막으로 웹 페이지를 새로 고치고 정적 리소스가로드되었는지 테스트하십시오.
추신 : Spring Boot는 주로 Thymeleaf 템플릿을 홍보하는 반면, 언어는 XML을 사용하는 반면 개인적으로 편리하지 않다고 생각합니다.
코드 다운로드 : https://github.com/carter659/spring-boot-02.git
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.