首页 > 代码库 > spring boot

spring boot

1.maven配置:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.简单web程序,不需要建立Dynamic Web工程,直接建Java工程即可。(web工程引入Tomcat jar包会报错)

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

3.静态资源访问:

  默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:

  • /static
  • /public
  • /resources
  • /META-INF/resources

  如:src/resources下建立img包,放入1.jpg,可用http://localhost:8080/img/1.jpg访问

4.不推荐使用jsp,推荐使用模版引擎:Thymeleaf、FreeMarker、Velocity、Groovy、Mustache等;

  默认的模板配置路径为:src/main/resources/templates

  引入模版文件解析jar包(Thymeleaf):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

  配置文件:application.properties

# Enable template caching.
spring.thymeleaf.cache=true  
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true  
# Content-Type value.
spring.thymeleaf.content-type=text/html  
# Enable MVC Thymeleaf view resolution.
spring.thymeleaf.enabled=true  
# Template encoding.
spring.thymeleaf.encoding=UTF-8  
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.excluded-view-names=  
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5  
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/  
# Suffix that gets appended to view names when building a URL.
spring.thymeleaf.suffix=.html  spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.

  jsp文件默认路径为:src/main/webapp/

  文件解析jar包:

<dependency> 
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId> 
    <version>7.0.59</version>
</dependency>

 

spring boot