当前位置:   article > 正文

springboot—— 使用STS作为IDE实现MVC模式的Web程序_sts ide

sts ide

前言

1. 在springboot的官网推荐了两种开发环境,一个是IDEA,另一个就是Spring Tool Suite(STS)。之前一直在IDEA上,今天用STS试一下。并比较二者的区别。

2. 有两种方法使用STS,一种是在eclipse里下载STS的插件,一种是下载STS整个IDE。因为我的eclipse版本比较老不支持STS插件,所以我选择了后者。

一、下载安装STS(spring Tool Suite 4)

1. 下载jar包

目前spring官网已经不支持spring Tool Suite 3了。所以直接下载的STS 4,地址:Spring | Tools

下载下来是一个jar包

2. 解压jar包

找到jar包的路径,在该路径下打开cmd

输入如下命令解压jar包。

 

得到安装包的可执行文件,安装STS。 

在同级目录下生成一个“sts-4.14.0.RELEASE”文件夹,打开这个文件夹就能看见下面的STS可执行程序。

双击进入。 

二、新建一个基于Thymeleaf的springboot web项目

2.1 新建项目

1. 选择maven项目。我的java version要选8,然后起个名字就可以了

2. 选择模板。web项目的话,前两个ThymeleafSpring web必选。

 到这里为止和IDEA里差不多。来看一下二者目录结构(左IDEA,右STS)。

稍有不同,但其实是差不多的。

对了,在STS里maven的本地仓库也是默认在下面这个路径下。

C:\Users\User\.m2\repository 

而且在STS里构建项目不会和IDEA一样提醒你是否更改本地仓库的路径,直接按照默认的来,所以第一个项目有些慢,可以看到正在往C盘那个默认的本地仓库里下东西。 

当然,我们可以和IDEA里一样更改本地仓库的路径:

 2.2 配置web应用的上下文路径

在application.properties文件中配置如下内容:

server.servlet.context-path=/ch5_2

 

这样,我们访问这个项目的时候,在浏览器地址栏输入http://localhost:8080/ch5_2就可以了。

2.3 导入静态资源

JS脚本、CSS样式、图片等静态文件默认放置在src/main/resources/static目录下。

2.4 创建实体类Book

创建名为com.example.springboot01.model的包,并在该包中创建名为Book的实体类。此实体类用在模板页面展示数据。

 

  1. package om.example.springboot01.model;
  2. public class Book {
  3. String isbn;
  4. Double price;
  5. String bname;
  6. String publishing;
  7. String author;
  8. String picture;
  9. public Book(String isbn, Double price, String bname, String publishing, String author, String picture) {
  10. super();
  11. this.isbn = isbn;
  12. this.price = price;
  13. this.bname = bname;
  14. this.publishing = publishing;
  15. this.author = author;
  16. this.picture = picture;
  17. }
  18. public String getIsbn() {
  19. return isbn;
  20. }
  21. public void setIsbn(String isbn) {
  22. this.isbn = isbn;
  23. }
  24. public Double getPrice() {
  25. return price;
  26. }
  27. public void setPrice(Double price) {
  28. this.price = price;
  29. }
  30. public String getBname() {
  31. return bname;
  32. }
  33. public void setBname(String bname) {
  34. this.bname = bname;
  35. }
  36. public String getPublishing() {
  37. return publishing;
  38. }
  39. public void setPublishing(String publishing) {
  40. this.publishing = publishing;
  41. }
  42. public String getAuthor() {
  43. return author;
  44. }
  45. public void setAuthor(String author) {
  46. this.author = author;
  47. }
  48. public String getPicture() {
  49. return picture;
  50. }
  51. public void setPicture(String picture) {
  52. this.picture = picture;
  53. }
  54. }

2.5 创建控制器类ThymeleafController

创建名为com.example.springboot01.controller的包,并在该包中创建名为ThymeleafController的控制器类。在该控制器类中,实例化Book类的多个对象,并保存到集合ArrayList<Book>中。

 

  1. package com.example.springboot01.controller;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import om.example.springboot01.model.Book;
  8. @Controller
  9. public class ThymeleafController {
  10. @RequestMapping("/")
  11. public String index(Model model) {
  12. Book teacherGeng = new Book(
  13. "9787302464259",
  14. 59.5,
  15. "Java 2实用教程(第5版)",
  16. "清华大学出版社",
  17. "耿祥义",
  18. "073423-02.jpg"
  19. );
  20. List<Book> chenHeng = new ArrayList<Book>();
  21. Book b1 = new Book(
  22. "9787302529118",
  23. 69.8,
  24. "Java Web开发从入门到实战(微课版)",
  25. "清华大学出版社",
  26. "陈恒",
  27. "082526-01.jpg"
  28. );
  29. chenHeng.add(b1);
  30. Book b2 = new Book(
  31. "9787302502968",
  32. 69.8,
  33. "Java EE框架整合开发入门到实战——Spring+Spring MVC+MyBatis(微课版)",
  34. "清华大学出版社",
  35. "陈恒",
  36. "079720-01.jpg");
  37. chenHeng.add(b2);
  38. model.addAttribute("aBook", teacherGeng);
  39. model.addAttribute("books", chenHeng);
  40. return "index";
  41. }
  42. }

2.6 View视图页面

Thymeleaf模板默认将视图页面放在src/main/resources/templates目录下。因此,我们在src/main/resources/templates目录下新建html页面文件index.html。在该页面中,使用Thymeleaf模板显示控制器类TestThymeleafController中的model对象数据。

 

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. <link rel="stylesheet" th:href="@{css/bootstrap.min.css}" />
  7. <!-- 默认访问 src/main/resources/static下的css文件夹-->
  8. <link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" />
  9. </head>
  10. <body>
  11. <!-- 面板 -->
  12. <div class="panel panel-primary">
  13. <!-- 面板头信息 -->
  14. <div class="panel-heading">
  15. <!-- 面板标题 -->
  16. <h3 class="panel-title">第一个基于Thymeleaf模板引擎的Spring Boot Web应用</h3>
  17. </div>
  18. </div>
  19. <!-- 容器 -->
  20. <div class="container">
  21. <div>
  22. <h4>图书列表</h4>
  23. </div>
  24. <div class="row">
  25. <!-- col-md针对桌面显示器 col-sm针对平板 -->
  26. <div class="col-md-4 col-sm-6">
  27. <a href="">
  28. <img th:src="'images/' + ${aBook.picture}" alt="图书封面" style="height: 180px; width: 40%;"/>
  29. </a>
  30. <!-- caption容器中放置其他基本信息,比如标题、文本描述等 -->
  31. <div class="caption">
  32. <h4 th:text="${aBook.bname}"></h4>
  33. <p th:text="${aBook.author}"></p>
  34. <p th:text="${aBook.isbn}"></p>
  35. <p th:text="${aBook.price}"></p>
  36. <p th:text="${aBook.publishing}"></p>
  37. </div>
  38. </div>
  39. <!-- 循环取出集合数据 -->
  40. <div class="col-md-4 col-sm-6" th:each="book:${books}">
  41. <a href="">
  42. <img th:src="'images/' + ${book.picture}" alt="图书封面" style="height: 180px; width: 40%;"/>
  43. </a>
  44. <div class="caption">
  45. <h4 th:text="${book.bname}"></h4>
  46. <p th:text="${book.author}"></p>
  47. <p th:text="${book.isbn}"></p>
  48. <p th:text="${book.price}"></p>
  49. <p th:text="${book.publishing}"></p>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. </body>
  55. </html>

2.7 运行

首先,运行自动生成的springboot01Application.java主类。然后,访问http://localhost:8080/

ch5_2/。运行效果如下图所示。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/967715
推荐阅读
相关标签
  

闽ICP备14008679号