MCVプログラムをすばやく構築する方法は?
公式の春の例を参照してください:https://spring.io/guides/gs/serving-web-content/
1。スプリングMVCとタイマーフテンプレートと組み合わせた
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.apach/4.0. http://maven.apache.org/xsd/maven-4.0.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.github.carter659 </groupId> <artifactid> spring02 </artifactid> </card </>>>>>>>>> facking <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-boot-starter-parent </artifactid> <version> 1.4.2.Release </version> </parent> <name02 </name> <url> http://maven.apache.org </url> <Propeties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <shiptencies> <mist depthency> </dependency> <dependency> groupid> org.springframework.boot </groupid> <artifactid> spring-boot-devtools </artifactid> <optional> true </optional> </dependency> </dependencies> <build> <blugins> <プラグイン<artifactid> spring-boot-maven-plugin </artifactid> </plugin> </plugins> </build> </project>
コントローラー「MainController.java」のクラスファイルを追加します:
パッケージcom.github.carter659.spring02; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.getMapping; @controllerpublic maintroller {modeletmaptin Model.Addattribute( "name"、 "liu dong"); 「インデックス」を返します。 }}app.javaファイルを変更します
パッケージcom.github.carter659.spring02; import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; args); }}
次に、プロジェクトを右クリックしてJavaビルドパスに入ります
フォルダを追加する「およびフォルダー」
メインディレクトリに「リソース」フォルダーを追加します
「リソース」の「除外」を変更する:
入力"**"
SRC/Main/Resourcesの下に「テンプレート」フォルダーを作成し、新しい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> < $ {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> </<"img /> <p th:text = "'hello、' + $ {name} + '!'" /> < /body> < /html>この時点で、現象がすぐに表示されます。
Mavenの「DevTools」に依存するため、プログラムは自動的にロードされることがわかりました
最後に、Webページを更新して、静的リソースがロードされているかどうかをテストします
PS:Spring Bootは主にThymeleafテンプレートを宣伝しますが、その言語はXMLを使用していますが、これはあまり便利ではないと思います。
コードダウンロード:https://github.com/carter659/spring-boot-02.git
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。