关于java:Spring-Boot初步学习04

3次阅读

共计 2669 个字符,预计需要花费 7 分钟才能阅读完成。

通过之前整合 mybatis 框架之后, 本文持续将 springboot 与 springmvc 进行整合.

MVC 设计模式

MVC 设计模式将零碎分为了三局部:
控制器(Controller)- 负责获取申请,解决申请,响应后果;
模型(Model)– 实现业务逻辑,数据逻辑实现;
视图(View)– UI 设计人员进行图形界面设计,负责实现与用户交互;

MVC 的首要职责: 是让每个对象各司其职, 各尽所能;
其次是: 基于 ” 高内聚, 低耦合 ” 的面向接口思维实现相干层与层对象之间的交互.

整合根本配置

增加依赖

首先须要增加 springmvc 依赖:
右键 pom.xml–>spring–>Edit Starters–> 搜寻 spring WEB 以及 Thymeleaf, 并将这两个依赖增加.

Web 依赖提供了 Spring MVC 外围 API,同时会嵌入一个 Tomcat 服务器;
Thymeleaf 依赖提供了一个视图解析器对象以及数据绑定机制.
Thymeleaf 是一个 html 模板引擎,提供了与 Spring MVC 进行整合的 API,可作为 MVC 架构中 Web 利用的 View 层(曾经逐渐代替 JSP 进行应用).

配置外围

须要在 application.properties 配置文件中增加视图解析器配置(如果没有配置也会默认配置,在默认配置中 prefix 默认值为 classpath:/templates/, 后缀默认为.html)

#springthymeleaf
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html

在这里咱们配置如上图所示.

留神:依据配置, 咱们须要在 src/main/resources 目录下创立 templates/pages 目录.

thymeleaf 目录下的 html 文件 (模板文件) 无奈间接拜访, 若要间接拜访须要在 src/main/resources 目录下创立 static 目录(用于存储动态资源).

拜访实现

编写 GoodsController 类

代码如下:

@Controller
@RequestMapping("/goods/")
public class GoodsController {@RequestMapping("doGoodsUI")
    public String doGoodsUI() {return "goods";}
}

通过增加 @Controller 注解将其交给 spring 治理, 尽管是 @Controller, 但其实他并不属于 MVC 设计模式中的 ”C”, 而是 Handler(处理器), 可了解为一个后端控制器, 但属于 ”M”.

该办法是由前端控制器 DispatcherServlet 调用, 返回值也是返回给前端控制器; 返回值会被前端控制器调用视图解析器增加前后缀.

创立 goods.html

因为 GoodsController 类最初会 return “goods”; 所以咱们须要在 /templates/pages/ 目录下创立 goods.html 文件.

那么当 GoodsController 类中 return 时, 视图解析器会通过咱们所配置的前后缀, 将其指向 /templates/pages/ 目录下的 goods.html 文件, 在网页中进行显示.

拜访测试

关上服务器(内嵌的 tomcat), 关上浏览器输出门路进行拜访测试

#server
server.port=80

因为我将端口号改为了 80, 所以只需在浏览器输出:
localhost/goods/doGoodsUI 即可胜利拜访.

业务实现

既然曾经能够胜利拜访后, 能够与之前的 Dao 层的数据操作一起应用一下, 看一下成果, 这里以查问所有商品为实例.

@Controller
@RequestMapping("/goods/")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("doGoodsUI")
    public String doGoodsUI(Model model) {List<Goods> list = goodsService.findObjects();
        model.addAttribute("list", list);
        return "goods";
    }
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>the goods pages</h1>
    <table>
        <thead>
            <tr>
                <th>id</th>
                <th>name</th>
                <th>remark</th>
                <th>createdTime</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="g:${list}">
                <td th:text="${g.id}">1</td>
                <td th:text="${g.name}">pathon</td>
                <td th:text="${g.remark}">framwork</td>
                <td th:text="${#dates.format(g.createdTime,'yyyy/MM/dd HH:mm:ss')}">2020/8/3 17:33</td>
            </tr>
        </tbody>
    </table>
</body>
</html>
@Mapper
public interface GoodsDao {@Select("select * from tb_goods")
    public List<Goods> findObjects();}
public interface GoodsService {public List<Goods> findObjects();
}
@Service
public class GoodsServiceImpl implements GoodsService{
    @Autowired
    private GoodsDao goodsDao;
    @Override
    public List<Goods> findObjects() {long t1=System.currentTimeMillis();
        List<Goods> list = goodsDao.findObjects();
        long t2=System.currentTimeMillis();
        log.info("execute time:{}",(t2-t1));
        return list;
    }
}

以上是代码实现, 能够失去正确显示:

正文完
 0