美文网首页
Spring Boot学习中有关Lombok插件,Thymele

Spring Boot学习中有关Lombok插件,Thymele

作者: 微笑吧_5464 | 来源:发表于2018-09-07 01:41 被阅读0次
11.png

一:Lombok插件的使用

1.为什么出现lombok,解决了什么问题?
  • 出现原因:传统开发过程中Java代码过于冗杂,getter,setter等方法以及构造函数需要手动建立。

  • 解决问题:lombok可以通过简单的注解的形式来帮助我们简化消除显得很臃肿的 Java 代码,省去了许多没必要的get,set,toString,equals,hashCode等代码,简化了代码编写,减少了代码量。

2.以往是如何操作的?
  • 在编写实体类Model时,需要手动生成getter/setter方法,toString和equals等,代码过于繁多。
3.如何使用lombok?
  • (1)IDEA中安装lombok插件:plugins->Browse repositorties->搜索下载->重启IDEA
  • (2)添加依赖:
    <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <version>1.16.10</version>
      </dependency>
  • (3)合理使用注解,自动生成各种方法;
//@Setter
//@Getter
//@ToString
//@EqualsAndHashCode
@Data
@Configuration
public class Student {
    private String name;
    private int age;
    private String male;
    private String studentNO;
}

二:Thymeleaf模板的使用

1.在pom中引入thymeleaf模板引擎依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.html页面头部添加声明
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
3.Controller控制层取值
import com.niit.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import javax.annotation.Resource;

@Controller
public class IndexController {

    //注入一个student的Bean
    @Resource
    private Student student;

//    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @GetMapping("index")
    public String index(ModelMap map) {
        student.setName("朱广旭");
        student.setAge(20);
        student.setMale("男");
        student.setStudentNO("2018");

        map.addAttribute("student",student);
        //返回的是页面名
        return "index";
    }
}
4.在页面中获取变量值时:使用 th:text 即可
<div class="container"><br>
    <div class="alert alert-success">
        <p th:text="${student.name}" >
        <p th:text="${student.age}" >
        <p th:text="${student.male}" >
        <p th:text="${student.studentNO}" >
        </p>
    </div>
</div>

三:Webjars静态资源jar包的使用

1.为什么要使用Webjars?
  • 传统的Web资源管理方式:
    现在的Javaweb项目大都是采用的Maven架构,在开发项目时,我们往往会将一个大项目拆分成许多分散的Maven模块,每个模块实现该项目的某一部分功能,当我们将各个模块开发完毕之后,直接将其组装到一起即可构成一个大的完整项目。
  • Webjars的优点:
    可以借助Maven工具,以jar包的形式,通过对Web前端资源进行统一依赖管理,保证这些Web资源版本的唯一性。
2.Webjars如何使用?
  • 在pom文件引入webjars的jar
<dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7-1</version>
</dependency>
  • 观察一下项目的依赖jar包,依赖中就有了bootstrap.jar


    image.png
  • 在html中引用,页面写完整
 <link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.css">
<body>
<h2>来自Thymeleaf模板的内容</h2>
<div class="container"><br>
    <div class="alert alert-success">
        <p th:text="${student.name}" >
        </p>
    </div>
</div>
</body>

相关文章

网友评论

      本文标题:Spring Boot学习中有关Lombok插件,Thymele

      本文链接:https://www.haomeiwen.com/subject/odkqgftx.html