¿Cómo construir rápidamente un programa MCV?
Consulte el ejemplo de primavera oficial: https://spring.io/guides/gs/serving-web-content/
1. Spring MVC combinado con plantilla de thymeleaf
Después de crear el proyecto Maven, modifique el archivo 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.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0. http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion> 4.0.0 </modelversion> <MoupRupid> com.github.carter659 </groupid> <artifactid> spring02 </artactid> <proation> <MoupRid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-parent </artifactid> <versión> 1.4.2.release </versión> </parent> <name> spring02 </name> <url> http://maven.apache.org </s url> <pertiese> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <Spendency> <MoupRoMID> org.springframework.boot </groupid> <artifactid> spring-boot-devlools </arfactid> <pectional> true </pectional> </dependency> </dependencias> <construcción> <glugins> <glugin> <proupid> org.springframework.boot </proupid> <artifactID> spring-boot-saven-plugin </artifactid> </glugin> </glugins> </build> </proyecto>
Agregue el archivo de clase del controlador "MainController.java":
paquete com.github.carter659.spring02; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.getMapping; @ControllerPublic Class principal {@getmapping ("/") model.addattribute ("nombre", "liu dong"); devolver "índice"; }}Modificar el archivo APP.Java
paquete com.github.carter659.spring02; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @SpringBootapplationPplicationPpublic Class App {public static estatic main (string [] args) {springapplication.run (app.cLassClassClass, args, args, args (args); }}Luego haga clic derecho en el proyecto para ingresar a Java Build Rath
Agregar carpeta "y carpeta"
Agregue la carpeta "Recursos" en el directorio principal
Modificar "excluido" de "recursos":
ingresar"**"
Cree la carpeta "Plantillas" en src/main/recursos y cree un nuevo archivo html "index.html"
<! Doctype html> <html xmlns: th = "http://www.thymeleaf.org"> <fead> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> mvc </title> </eve> <p th: text = hello + hello » $ {name} + '!' "/> </body> </html> Ingrese http: // localhost: 8080 para verificar si se ejecuta correctamente:
Lo anterior es una página dinámica hecha con la plantilla de Tymeleaf. Entonces, ¿cómo usar recursos estáticos en aplicaciones MVC?
2. Recursos estáticos
Cree una nueva carpeta "estática" en SRC/Main/Resources
Y copiar un archivo de imagen en su carpeta
Modifique el archivo anterior "index.html" y agregue la etiqueta IMG
<! Doctype html> <html xmlns: th = "http://www.thymeleaf.org"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> mvc </title> </fead> <cody> <omg src = "img.pngg.png" /> <p th: text = "'Hello,' + $ {name} + '!'" /> </body> < /html>En este momento, aparece inmediatamente un fenómeno:
Descubrimos que el programa se cargará automáticamente, porque depende de "DevTools" en Maven
Finalmente, actualice la página web y pruebe si el recurso estático está cargado
PS: Spring Boot promueve principalmente la plantilla de Tymeleaf, mientras que su lenguaje usa XML, que personalmente creo que no es muy conveniente.
Descargar el código: https://github.com/carter659/spring-boot-02.git
Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.