springboot---helloworld

来源:互联网 发布:java字符流写文件 编辑:程序博客网 时间:2024/06/06 12:25

springboot这个框架今天第一次学习,接触以后只想说,真的很棒,以前繁琐的配置现在分分钟搞定,springboot算不上新框架,而是将以前框架的各种配置都简化了,写起东西来真的非常快。
下面是一个helloworld的小例子和视图解析的配置。
地址:https://github.com/wkcaeser/springboot-studydemo/tree/master
http://download.csdn.net/download/qq_36666651/10154459
首先是依赖包,这里是 web小例子,所以先导入spring-boot-starter-web,这个包含了spring-boot-starter。
首先是springboot的启动入口类:

@SpringBootApplicationpublic class MySpringBootApplication {    public static void main(String[] args){        SpringApplication.run(MySpringBootApplication.class, args);    }    @Bean    public EmbeddedServletContainerFactory servletContainerFactory(){        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();        factory.setPort(8088);        factory.setSessionTimeout(10, TimeUnit.MINUTES);        return factory;    }}

springbootapplication是个组合注解:

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan(excludeFilters = {        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

main方法为程序入口,执行SpringApplication的run方法,就会自动初始化spring的容器并加载bean,这个类默认扫描与它平级的目录,所以放在根目录。
springboot是内嵌的servlet容器,默认端口为8080,本地开发调试时很容易端口冲突,可以通过上面的servletContainerFactory方法来修改相应参数,也可以在application.properties里面进行配置。
接下来是个web的方法:

@RestControllerpublic class IndexController {    @RequestMapping(value = "/")    public String index(){        return "hello world";    }}

这个和springmvc是一模一样的,就不说了,执行 main方法,访问localhost:8088就可以看到输出了。

web开发返回页面是很常见的需求,springboot默认的视图解析器是thymeleaf解析器,只需要导入相应的依赖就可以使用了:spring-boot-starter-thymeleaf,这时只会在默认路径下寻找页面,我们可以在application.properties文件配置详细参数:

#thymeleaf configspring.thymeleaf.prefix=/WEB-INF/views/spring.thymeleaf.suffix=.htmlspring.thymeleaf.encoding=UTF-8spring.thymeleaf.cache=falsespring.thymeleaf.mode=HTML5

web开发中jsp页面也是很常用的,springboot推荐的是thymeleaf。下面是jsp页面的访问,导入依赖tomcat-embed-jasper。这里需要注意版本的问题,依赖很容易不匹配,在application.properties的配置:

#jsp configspring.mvc.view.prefix=/WEB-INF/views/spring.mvc.view.suffix=.jsp

有时候我们需要同时使用两种视图解析器,这时候可以在thymeleaf的配置加一句:

spring.thymeleaf.view-names=p*

这句话意思是解析文件名是p开头的文件,这时候两个解析器就可以同时使用了。
controller文件为:

@Controllerpublic class PageController {    @RequestMapping(value = "/page0")    public String indexPage0(){        System.out.println("do here");        return "index";    }    @RequestMapping(value = "/page1")    public String indexPage1(){        System.out.println("do here");        return "page1";    }}

和springmvc完全相同。

第一次写因为一来版本的问题忙活了好久,下面给出依赖,主要是tomcat-embed-jasper的版本容易出问题:

group 'springboot-helloworld'version '1.0-SNAPSHOT'apply plugin: 'java'apply plugin: 'war'sourceCompatibility = 1.8repositories {    mavenCentral()}dependencies {    testCompile group: 'junit', name: 'junit', version: '4.12'//    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter//    compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '1.5.9.RELEASE'    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.9.RELEASE'    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf    compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '1.5.9.RELEASE'    // https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper    compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '8.5.23'//    // https://mvnrepository.com/artifact/javax.servlet/jstl//    compile group: 'javax.servlet', name: 'jstl', version: '1.2'}
原创粉丝点击