SpringBoot实现(三):关联 Thymeleaf

SpringBoot实现(一):eclipse 下 maven 环境配置
SpringBoot实现(二):MyBatis 配置和 MVC 布置
SpringBoot实现(三):关联 Thymeleaf
SpringBoot实现(四):过滤器 Filter
SpringBoot实现(五):将项目部署到云服务器(完)

首先在pom.xml文件中添加依赖库

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

若按照我之前的博客来的话是已经加了的,没加的要加上:)
然后在application-test.yml中加入thymeleaf配置数据,内容如下

server:
  port: 8080
 
spring:
  thymeleaf:
    cache=false
    prefix=classpath:/templates/
    suffix=.html
    mode=HTML5
    encoding=UTF-8
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/test
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.example.entity

主要加了一些配置文件,cache属性表示缓存,html默认有缓存数据的功能,在这里我们要关掉它,以免造成数据重复,prefix用于设置映射的文件夹,suffix用于设置要映射的文件后缀:)
之后在src/main/resources下新建一个templates文件夹,有的话就不用创建了,然后在其下创建一个html文件 test.html,内容如下

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

加上xmlns:th="http://www.thymeleaf.org"是为了识别thymeleaf标签:)
进入到src/main/java下的com/example/controller下的UserController管理类加入一些数据,内容如下

package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.entity.User;
import com.example.service.UserService;

@RestController
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
//	@RequestMapping("test.do")
//	public void doTest() {
//		for (User u : userService.findAll()) {
//			System.out.println(u.toString());
//		}
//	}
	@RequestMapping("test.go")
	public ModelAndView goTest() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("test.html");
		return mv;
	}
}

运行启动类后进入网址http://localhost:8080/user/test.go即可进入到的test.html文件,看到以下内容
在这里插入图片描述
通过ModelAndView来存储要跳转的网页名:)
如何传输数据呢?可以直接通过ModelAndView传输数据,内容如下

@RequestMapping("test.go")
public ModelAndView goTest() {
	ModelAndView mv = new ModelAndView();
	mv.addObject("var1", "receiver me!");
	mv.setViewName("test.html");
	return mv;
}

直接有一个方法可以以键值对的形式传递数据,如何接受呢:)
test.html加入以下数据,内容如下

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
	<h1>Hello World!</h1>
	<!-- html 标签嵌入 -->
	<span th:text="${var1}"></span>
	<br />
	<!-- 会转义为html标签 -->
	[[${var1}]]
	<br />
	<!-- 不会转义为html标签 -->
	[(${var1})]
	<br />
	<!-- thymeleaf 标签嵌入 -->
	<th:block th:text="${var1}" />
</body>
</html>

结果如下
在这里插入图片描述

要接收Session数据的话要在管理类UserController里加入一些数据,内容如下

package com.example.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.entity.User;
import com.example.service.UserService;

@RestController
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private HttpServletRequest request;
	@RequestMapping("test.go")
	public ModelAndView goTest() {
		request.getSession().setAttribute("sessionValue", "receiver session value!");
		ModelAndView mv = new ModelAndView();
		mv.addObject("var1", "receiver me!");
		mv.setViewName("test.html");
		return mv;
	}
}

test.html中获取数据,内容如下

<span th:text="${session.sessionValue}"></span>

遍历传过来的值为集合,在UserController中加入数据,内容如下

package com.example.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.entity.User;
import com.example.service.UserService;

@RestController
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private HttpServletRequest request;
	@RequestMapping("test.go")
	public ModelAndView goTest() {
		ModelAndView mv = new ModelAndView();
		List<User> list = userService.findAll();
		mv.addObject("list", list);
		mv.setViewName("test.html");
		return mv;
	}
}

test.html加入一些数据,内容如下

<table>
	<tr th:each="user : ${list}">
		<td th:text="${user.id}"></td>
		<td th:text="${user.username}"></td>
		<td th:text="${user.password}"></td>
	</tr>
</table>

效果图如下
在这里插入图片描述
接受html传到服务器的数据,html常常是以表单的形式提交数据的,会设置一个name属性,之后提交到服务器时在管理类UserController中进行以下操作,内容如下

package com.example.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.entity.User;
import com.example.service.UserService;

@RestController
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private HttpServletRequest request;
	@RequestMapping("test.go")
	public ModelAndView goTest() {
		request.getParameter("formNameValue");
		ModelAndView mv = new ModelAndView();
		List<User> list = userService.findAll();
		for (User u : list) {
			System.out.println(u.toString());
		}
		mv.addObject("list", list);
		mv.setViewName("test.html");
		return mv;
	}
}

通过@Autowired注解自动帮我们拿到HttpServletRequest对象,然后通过其方法就可以拿到网页传回来的值了:)

问题询问:hellolxb@yeah.net

SpringBoot实现(一):eclipse 下 maven 环境配置
SpringBoot实现(二):MyBatis 配置和 MVC 布置
SpringBoot实现(三):关联 Thymeleaf
SpringBoot实现(四):过滤器 Filter
SpringBoot实现(五):将项目部署到云服务器(完)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值