本文内容大部分来自黑马视频的SpringBoot与Shiro整合-权限管理实战视频,在此记录为个人学习笔记。
可按步骤操作,无法实操的实战blog都是耍流氓。
一、搭建SpringBoot开发环境
1. 安装好开发软件和Maven等
本文用的是MyEclipse 2017 CI和Maven 3.5.3。
2. 进入https://start.spring.io/网站,填写相关信息,导出一个新项目
3. 将这个Maven项目导入MyEclipse中
导入过程中出现一个报错信息:
点了Finish,查看pom.xml文件,报错如下:
No marketplace entries found to handle Connector org.eclipse.m2e.jdt.JarLifecycleMapping in Eclipse.
神奇的解决办法:重启MyEclipse,Maven->Update Project
4. pom.xml文件配置说明
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fukaiit</groupId>
<artifactId>spring-shiro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-shiro</name>
<description>Demo project for Spring Boot</description>
<!-- 继承Spring Boot的默认父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 修改参数 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- jdk的编译版本,修改之后需要Update Project -->
<java.version>1.8</java.version>
</properties>
<!-- 导入依赖 -->
<dependencies>
<!-- 导入thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 导入Web支持:Spring web开发支持,servlet相关程序等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5. 编写测试Controller
package com.fukaiit.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "OK";
}
}
6. 启动spring boot启动类
package com.fukaiit.springshiro;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot启动类
* @author Administrator
*
*/
@SpringBootApplication
public class SpringShiroApplication {
public static void main(String[] args) {
SpringApplication.run(SpringShiroApplication.class, args);
}
}
7. 在浏览器中输入http://localhost:8080/hello查看
又报错了:Whitelabel Error Page
错误原因:程序只加载Application.java所在包及其子包下的内容(https://www.cnblogs.com/JealousGirl/p/whitelabel.html)。
而我的项目启动类所在的包是com.fukaiit.springshiro,将其修改为com.fukaiit。
8. 再刷新http://localhost:8080/hello查看
二、使用Thymeleaf页面模板
1. 导入thymeleaf依赖
<!-- 导入thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. 在UserController中编写测试thymeleaf的方法(testThymeleaf)
@RequestMapping("/testThymeleaf")
public String testThymeleaf(Model model) {
model.addAttribute("name","fukaiit");
return "testThymeleaf";
}
3. 在resources/templates文件夹下新建testThymeleaf.html文件
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>testThymeleaf页面</title>
</head>
<body>
<h1 th:text="${name}"></h1>
</body>
</html>
4. 重启项目,访问http://localhost:8080/testThymeleaf
Tips:
<html xmlns:th="http://www.thymeleaf.org">
可写可不写,不影响运行,关系到编辑器中是否提示无法识别th:开头的标签属性- 在thymeleaf 3.0之前,每个标签必须正确的闭合,否则会提示Whitelabel Error Page错误
- 修改thymeleaf版本的方法:
在pom.xml中配置如下信息:
<properties> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version> </properties>