1.引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.11.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
</dependencies>
2.配置资源文件
//设置为false表示没有缓存
spring.thymeleaf.cache=false
//设置读取html的路径
spring.thymeleaf.prefix=classpath:/static/
//回避HTML进行严格的检查的配置,需要提前引入nekohtml依赖。
spring.thymeleaf.mode = LEGACYHTML5
//设置连接的solr地址
spring.data.solr.host= http://192.168.163.131:8983/solr
3.创建实体类
package cn.ps.entity;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
//表示引入get set 方法
@Data
//表示引入无参的构造方法
@NoArgsConstructor
//表示引入有参的构造方法
@AllArgsConstructor
//表示引入tostring方法
@ToString
@SolrDocument(solrCoreName="mycore")
public class Person {
private String id;
@Field("country_ik")
private String country;
@Field("name_ik")
private String name;
@Field("desc_ik")
private String desc;
@Field("age_i")
private int age;
}
4.创建控制层
package cn.ps.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import cn.ps.dao.MoneyDap;
import cn.ps.entity.Person;
import cn.ps.entity.User;
@Controller
public class ThyController {
@GetMapping("/index")
public String index(Model model ,HttpServletRequest request) {
Map map =new HashMap();
map.put("id", 1);
map.put("name", "zs");
model.addAttribute("user", map);
User user1=new User("2","ls");
//model.addAttribute("user2", user1);
request.getSession().setAttribute("user2", user1);
return "idx";
}
@Autowired
private MoneyDap mo;
@GetMapping("/ps")
public String person(String keyword,Model model) {
List<Person> paList = mo.findByDesc(keyword);
model.addAttribute("po", paList);
return "pas";
}
@GetMapping("/inc")
public String person1() {
return "aaa";
}
}
5.创建dao层
package cn.ps.dao;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.repository.Query;
import org.springframework.data.solr.repository.SolrCrudRepository;
import cn.ps.entity.Person;
public interface MoneyDap extends SolrCrudRepository<Person, String> {
public List<Person> findByDesc(String keyword);
}
6.创建main方法
package cn.ps;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ThyMain {
public static void main(String[] args) {
SpringApplication.run(ThyMain.class, args);
}
}
7.创建html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- th:utext在thymeleaf是不会转译的,但th:text会 -->
<div th:utext="${user.id}">
欢迎光临
</div>
<div th:object="${user}">
<p th:text="*{id}"></p>
<p th:text="*{name}"></p>
<!-- <p th:text="${user2.userName}"></p> -->
<p th:text="${session.user2.userName}"></p>
</div>
<br/>
<p th:text="${session.user2.toString()}"></p>
<form action="ps" method=get>
新闻:<input id="b" type="text" name="keyword">
<input type="submit" value="搜索">
</form>
<table>
<tr style="width: 100%" >
<th>国家</th>
<th>姓名</th>
<th>城市</th>
<th>年龄</th>
</tr>
<tr style="width: 100%" th:each="pea,iterStat : ${po}"
th:style="${iterStat.odd}?'background-color:red':''"
>
<td th:text="${pea.country}"></td>
<td th:text="${pea.name}"></td>
<td th:text="${pea.desc}"></td>
<td th:text="${pea.age}"></td>
</tr>
</table>
<!--被包含的标签:<div th:fragment="myhe">
<script type="text/javascript">alert("hello")</script>
新闻 视频 图片 军事 体育 NBA 娱乐 财经 科技 时尚 汽车 房产 教育 文化 游戏
</div>
包含标签
<!-- html的名字(a.html)加 被包含的标签名 -->
<div th:include="a :: myhe">
</div> -->
</body>
</html>