Como criar rapidamente um programa MCV?
Consulte o exemplo oficial da primavera: https://spring.io/guides/gs/serving-web-content/
1. MVC da primavera combinada com o modelo de timidade
Depois de criar o projeto Maven, modifique o arquivo 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/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <ModelVersion> 4.0.0 </modelVersion> <puperid> com.github.carter659 </roupid> <stifactId> spring02 </artifactid> <sipers> 0.1-SNAPSHSNAPT </Groupid> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <name>spring02</name> <url>http://maven.apache.org</url> <properties> <Project.build.sourceEncoding> utf-8 </project.build.sourceEncoding> <nava.version> 1.8 </java.version> </properties> <inymAtiS> <spendency> <voundid> org.springFramework.BROOT </Groupid> <stifacty> primavera de primavera <Tringflorf-spring-Bring-Bring-Bring-Bring-Bring-TringStwork.Bring-STROTS </Groupid> <stifactid> primavera <vringFROTROTWORK.BROOT> <inymAtid> <TringFactid> Spring-Spring-Bring-Bring-BringFROTS-STROT-STROOT <IMATID> <TRATFACTID> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <TarifactId> Spring-boot-maven-plugin </stifactId> </flugin> </plugins> </fruct> </project>
Adicione o arquivo de classe do controlador "MainController.java":
pacote com.github.carter659.spring02; importar org.springframework.stereotype.controller; importar org.springframework.ui.model; importação @(@ControlLlerTringFramework.web.bind.annotation.getMapping; @ControllerPringFramework.web.bind.annotation.getMapping; Model.Addattribute ("Nome", "Liu Dong"); retornar "índice"; }}Modificar o arquivo app.java
pacote com.github.carter659.spring02; importar org.springframework.boot.springApplication; importar org.springframework.boot.autoconfigure.springbootapplication; @springBootApplication {public.. STEPRINGBOOTAPPLICATION; args); }}Em seguida, clique com o botão direito do mouse no projeto para entrar no Java Build Path
Adicionar pasta "e pasta"
Adicione a pasta "Recursos" no diretório principal
Modifique "excluído" de "recursos":
digitar"**"
Crie a pasta "Modelos" em SRC/Main/Recursos e crie um novo arquivo 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 </title = "phtc </title =" phtc = "pHtml; $ {nome} + '!' "/> </body> </html> Digite http: // localhost: 8080 para verificar se é executado com sucesso:
O exposto acima é uma página dinâmica feita usando o modelo ThymEleaf. Então, como usar recursos estáticos em aplicativos MVC?
2. Recursos estáticos
Crie uma nova pasta "estática" em SRC/Main/Recursos
E copie um arquivo de imagem em sua pasta
Modifique o arquivo anterior "index.html" e adicione a tag IMG
<! Doctype html> <html xmlns: th = "http://www.thymeleaf.org"> <head> <meta http-equiv = "content-ttype" content = "text/html; charset = utf-8"/> <title> mvc </title " /> <p th: text = "'hello,' + $ {name} + '!'" /> < /body> < /html>Neste momento, um fenômeno aparece imediatamente:
Descobrimos que o programa carregará automaticamente, porque depende de "Devtools" no Maven
Por fim, atualize a página da web e teste se o recurso estático é carregado
PS: O Spring Boot promove principalmente o modelo ThymEleaf, enquanto sua linguagem usa XML, que eu pessoalmente acho que não é muito conveniente.
Download de código: https://github.com/carter659/spring-boot-02.git
O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.