Springboot 模板渲染
在之前所见到的信息显示发现都是以Rest风格进行显示,但是在实际开发之中, 所有数据的显示最终都应该交由页面去处理完成。在Springboot 中这个页面并不是我之前的jsp ,FM 这些 而是普通的html页面,而且最为重要的是 此处所使用的渲染页面采用的是模板方式的显示 而在java开发之中 前台显示模板为三类 FreeMarker Velocity thymeleaf (官方推荐使用)
1.0 如果要使用 thymeleaf 那么应该首先进行相关支出依赖库导入 修改pom.xml 配置文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
记住改完Maven配置文件要刷新一下哈!
2.0 本次开发需要一个控制层跳到页面进行信心处理,在SpirngMvc 的时候使用的是ModelAndView 传递,而现在Springboot 里面如果要传递直接在方法中定义一个Model 参数就可以了。
3.0 控制层代码
package com.fabu.demo4.controller;
import com.fabu.demo4.service.IMessage;
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 org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
/****
* 控制层 用来测试模板渲染处理
*/
@Controller
@RequestMapping("/Message")
public class MessageController {
@Autowired
private IMessage iMessage;
@RequestMapping(value = "/info",method = RequestMethod.GET)
public String info(String user, Model model){
model.addAttribute("url",iMessage.info()); //request 属性传递包装
model.addAttribute("username",user);
return "message/index"; //返回一个路径 后缀是默认.html
}
/*****
* 老的方式 进行操作
* @param user
* @param map
* @return
*/
@RequestMapping(value = "/msg",method = RequestMethod.GET)
public ModelAndView msg(String user, HashMap<String,Object> map){
map.put("url",iMessage.info());
map.put("username",user);
return new ModelAndView("message/index",map);
}
}
3.1 service 层的代码如下
package com.fabu.demo4.service;
public interface IMessage {
/*****
* 按照真实开发 数据全部要去数据库查询出来 我这里为方便就不建立 数据库操作层了
*/
public String info();
}
3.2 service 层实现类代码如下
package com.fabu.demo4.service;
public class IMessageImpl implements IMessage {
/*****
* 按照真实开发 数据全部要去数据库查询出来 我这里为方便就不建立 数据库操作层了
*/
@Override
public String info() {
return "测试模板渲染 zw";
}
}
3.4 使用Springboot 构建Bean 方式注入
package com.fabu.demo4.config;
import com.fabu.demo4.service.IMessageImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/****
* 使用配置向的方式来配置Bean
*/
@Configuration
public class IMessageService {
@Bean //构建一个Bean 进行处理
public IMessageImpl getIMessageImpl(){
return new IMessageImpl();
}
}
项目目录结果如下
'
4.0 现在控制层使用的是@Controller 注解 所以执行该控制器的方法后会进行跳转处理 如果要进行跳转页面的定义 有严格要求 在classpath 路径下 src/main/resources src/main/view 必须建立有一个templates 的目录 在这个目录里面保存有thymeleaf的所有相关页面
5.0 在 templates 里面编写index页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Springboot thymeleaf 模板渲染</title>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
</head>
<body>
<p th:text=" '数据'+ ${url} "></p>
<p th:text=" '用户名' +${username} "></p>
</body>
</html>
页面编写 所有元素必须要有完结标签 6.0 运行服务 而后输入访问路径
http://localhost:8080/Message/msg?user=springboot
结果如下
7.0 如果现在我们所定义的要访问的页面不是通过控制器跳转的该怎么办? 可以考虑在thymeleaf 所在父路径里面建一个static 的子目录,该目录保存的是所有静态页面
在以后的实际开发之中 像js .css images 等信息文件都要求放在static 目录里面
8.0 thymeleaf 默认的访问的页面路径的后缀为*.html 那么可以通过application.yml 配置文件进行变更
server:
port: 8080
spring:
thymeleaf:
suffix: .htm
这个时候你需要修改index 页面后缀为 .htm