Spring Boot的Web开发
-
Spring Boot的Web开发支持 Spring Boot提供了spring-boot-starter-web为Web开发予以支持.它为我们提供了嵌入的Tomcat以及Spring MVC的依赖.
-
Thymeleaf模板引擎 Spring Boot 推荐使用Thymeleaf作为模板引擎.因为其提供了完整的Spring MVC支持. 因为使用嵌入的Servlet容器来运行JSP的话有一些小问题,内嵌Tomcat,Jetty不支持以jar的形式运行JSP,而且Undertow不支持JSP.
-
Thymeleaf基础知识 Thymeleaf是一个java类库,它是一个xml/xhtml/html5的模板引擎,可以作为MVC的Web应用的View层.
-
补充: 在javascript中访问model
-
-
Spring Boot的Thymeleaf支持 Spring Boot通过自动配置功能对Thymeleaf进行了自动配置,因此可以直接使用.
Thymeleaf 访问model
列表
package person.learn.thymeleaf; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.List; /** * 1.使用Thymeleaf作为模板引擎,要在application.yml里设置spring.thymeleaf.caching设置为false * 2.用了模板引擎之后,原先对于jsp的设置无效 * 3.(idea编辑器需要手动点击CTRL+F9,手动编译,因为IDEA文件不需要手动保存)修改Thymeleaf模板的内容后,要不重启项目就生效的话,需要make一下, * 或者使用热部署:http://mamicode.com/info-detail-1346413.html * 4.要使用Thymeleaf模板 请将pom.xml中相应的jar依赖注释去掉 * Created by barton on 16-5-19. */ @Controller public class ThymeleafController { @RequestMapping("/thymeleaf") public String index(Model model) { Person single = new Person("aa", 11); List
people = new ArrayList<>(); Person p1 = new Person("xx", 11); Person p2 = new Person("yy", 22); Person p3 = new Person("zz", 33); people.add(p1); people.add(p2); people.add(p3); model.addAttribute("singlePerson", single); model.addAttribute("people", people); return "thymeleaf/thymeleaf"; } } package person.learn.thymeleaf; /** * Created by barton on 16-5-19. */ public class Person { public Person(String name, Integer age) { this.name = name; this.age = age; } private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
-
Spring Boot自动配置的静态资源
- 类路径文件 把类路径下的/static,/public,/resources和/META-INF/resources文件夹下的静态文件直接映射为/**,可以通过.
- webjar webjar就是将我们常用的脚本框架封装在jar包中的jar包. 把webjar的/META-INF/resources/webjars/下的静态文件映射为/webjar/,可以通过 来访问
-
Spring Boot 对静态首页的支持
- classpath:/META-INF/resources/index.html
- classpath:/resources/index.html
- classpath:/static/index.html
- classpath:/public/index.html
-
将Tomcat替换为Jetty
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-jetty -
将Tomcat替换为Undertow
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-undertow -
设置Favicon 只需将自己的favicon.ico 防止在类路径根目录,类路径META-INF/resources/下,类路径resources/下,类路径static/下或者类路径public/下.