Spring Boot非常适合Web应用程序开发。 我们可以使用嵌入式Tomcat,Jetty或Undertow轻松创建自包含的HTTP服务器。 大多数Web应用程序将使用spring-boot-starter-web模块快速启动和运行。

  一。关于SpringBoot中的mvc

  在SpringBoot中使用mvc与springmvc基本一致,我们甚至可以按照springmvc中的标准来完成控制器的实现。

  代码示例:

package com.bdqn.lyrk.study.springboot.controller;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author chen.nie
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/number/{number}/Desc/{desc}")
    @ResponseBody
    public BeanEntity bean(@PathVariable ("number") int number, @PathVariable("desc") String desc) {
        return new BeanEntity(number,desc);
    }
}

@Data
@AllArgsConstructor
class BeanEntity {
    private int number;
    private String desc;
}
View Code

  当我们访问浏览器地址时得到对应的结果:

  我们可以发现这里跟springmvc中controller写法无二,其余的service层和dao层也均是按常规写法,用@Service和@Repository标记service与dao即可

  二.关于SpringBoot中mvc(静态资源-视图)

    默认情况下,Spring Boot将从类路径或ServletContext的根目录中的名为/static(或/ public或/resources或/META-INF/resources)的目录提供静态内容。

    在静态内容当中我们可以放js,css样式等文件,除Web服务,我们还可以使用Spring MVC来提供动态HTML内容。Spring MVC支持各种模板技术,包括Thymeleaf,FreeMarker和JSP。当然SpringBoot不推荐用JSP来作为视图层,通常情况我们把模板放在src/main/resources/templates下

  以下目录就是典型的模板与静态资源目录结构,按照上述规则我们把静态资源js文件放在static目录下,模板文件(这里使用的是Freemarker)放在规定的目录下:

  

 

  三.关于SpringBoot中的devtools(开发者工具)

    在使用SpringBoot时,有时候我们希望能够在改变相关资源时能够实时生效,尤其在更新视图资源时,体现尤为突出,那么这时候我们可以用SpringBoot给我们提供开发者工具来实现该效果,当类路径上的文件发生更改时自动重新启动:

  gradle代码:

dependencies {
    compile("org.springframework.boot:spring-boot-devtools")
}
View Code

  排除资源:

    如果要自定义这些排除项在类路径上的文件发生更改时自动重新启动,可以使用spring.devtools.restart.exclude属性。

  监视额外的路径:

    对不在类路径中的文件进行更改时,需要重新启动或重新加载应用程序,可以使用spring.devtools.restart.additional-paths属性来配置其他路径以监视更改

  使用触发器文件
    当使用IDE工具连续不断的进行编译,每次都重启并不好,因此当我们需要特定时间进行重启时,可使用触发文件
    因此我们可以使用spring.devtools.restart.trigger-file来指定触发文件

     liveReload:

    springboot开发者工具包含一个liveReload服务器,它可以在静态资源改变时触发浏览器刷新

 

  四。SpringBoot添加对jsp的支持

   1) 原则上来说,SpringBoot不推荐使用Jsp做为视图层,如果想用Jsp,我们需要包含以下的依赖:

    

     <dependency>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-tomcat</artifactId>
                     <scope>provided</scope>
              </dependency>
        <dependency>
          <groupId>org.apache.tomcat</groupId>
             <artifactId>tomcat-jasper</artifactId>
            <version>8.5.28</version>
    </dependency>     
            

            
View Code

   2) 在application.properties做相关视图的配置

    

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

    

posted on 2017-11-02 09:22  聂晨  阅读(864)  评论(0编辑  收藏  举报