温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot(七)整合themeleaf+bootstrap

发布时间:2020-07-21 13:56:10 来源:网络 阅读:1198 作者:小杨Java 栏目:大数据

前言

Thymeleaf是用于Web和独立环境的现代服务器端Java模板引擎。Thymeleaf的主要目标是将优雅的自然模板带到您的开发工作流程中—HTML能够在浏览器中正确显示,并且可以作为静态原型,从而在开发团队中实现更强大的协作。Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本。

Spring-boot-starter-web集成了Tomcat以及Spring MVC,会自动配置相关东西,Thymeleaf是用的比较广泛的模板引擎.

更新pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
更新application.properties

#thymeleaf

spring.thymeleaf.cache=false

spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.check-template-location=true

spring.thymeleaf.suffix=.html

spring.thymeleaf.encoding=UTF-8

spring.thymeleaf.mode=HTML5

创建Controller

之所以新建Controller,而不是复用之前的IndexController,是因为IndexController使用的是 @RESTController 注解的方式。

  1. 使用@Controller 注解,在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面。若返回json等内容到页面,则需要加@ResponseBody注解

  2. @RestController注解,相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面

新建UserController:

package com.demo.controller;

import com.demo.pojo.UserPosition;

import com.demo.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;

import java.util.List;

/**

  • Created by toutou on 2018/10/20.

*/

@Controller

public class UserController {

@Autowired

UserService userService;

@RequestMapping(value = "/mynearby")

public String myNearby(Model model, double lon, double lat)

{

double r = 6371;//地球半径千米

double dis = 2; //半径 单位:km

double dlng = 2Math.asin(Math.sin(dis/(2r))/Math.cos(lat*Math.PI/180));

dlng = dlng*180/Math.PI;//角度转为弧度

double dlat = dis/r;

dlat = dlat*180/Math.PI;

double minlat =lat-dlat;

double maxlat = lat+dlat;

double minlng = lon -dlng;

double maxlng = lon + dlng;

List<UserPosition> list = userService.getVicinity(BigDecimal.valueOf(minlng), BigDecimal.valueOf(maxlng), BigDecimal.valueOf(minlat), BigDecimal.valueOf(maxlat));

model.addAttribute("myinfo",list);

return "mynearby";

}

}

创建页面

/src/main/resources/templates/mynearby.html

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >

<html lang="en">

<head>

<meta content="text/html;charset=UTF-8"/>

<meta name="viewport" content="width=device-width,initial-scale=1"/>

<link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

<link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet">

<title>附近的小区</title>

</head>

<body>

<br/>

<div class="panel panel-primary">

<div class="panel-heading">

<h4 class="panel-title">我的坐标</h4>

</div>

<div class="panel-body">

<span>116.31064,40.062658</span>

</div>

<br/>

<div th:if="${not #lists.isEmpty(myinfo)}">

<div class="panel panel-primary">

<div class="panel-heading">

<h4 class="panel-title">附近的小区</h4>

</div>

<div class="panel-body">

<ul class="list-group">

<li class="list-group-item" th:each="item : ${myinfo}">

<span th:text="${item.id}"></span>

<span th:text="${item.city}"></span>

<span th:text="${item.position}"></span>

<span th:text="${item.longitude}"></span>

<span th:text="${item.latitude}"></span>

<button class="btn">删除</button>

</li>

</ul>

</div>

</div>

</div>

</div>

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script th:inline="javascript">

// var single = [[${singlePerson}]];

// console.log(single.name+"/"+single.age);

$(function(){

$(".btn").click(function(){

alert("删除功能完善中...");

});

});

</script>

</body>

</html>

xmlns:th="http://www.thymeleaf.org"命名空间,将镜头转化为动态的视图,需要进行动态处理的元素使用“th:”前缀;两个link引入bootstrap框架,通过@{}引入web静态资源(括号里面是资源路径)访问model中的数据通过${}访问.

运行效果:

SpringBoot(七)整合themeleaf+bootstrap目录结构:

SpringBoot(七)整合themeleaf+bootstrap

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI