使用springboot的thymeleaf在前端获取数据,使用model和session前端都获取不到。
InfoController.java
package com.example.h1.controller;
import com.example.h1.domain.User;
import com.example.h1.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 org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Controller
public class InfoController extends HttpServlet {
@Autowired
public UserService userService;
//查询并返回页面
// @RequestMapping("/info")
// public String list(Model model){
// List<User> list = userService.findAll();
// model.addAttribute("infor" , list);
// model.addAttribute("aaa","aaa");
// return "redirect:/static/infor.html";
// }
@RequestMapping("/info")
public String list(HttpServletRequest request, HttpServletResponse response) {
List<User> list = userService.findAll();
//添加session
request.getSession().setAttribute("infor",list);
System.out.println(request.getSession().getAttribute("infor"));
return "redirect:/static/infor.html";
}
}
这里我输出了一下session在控制台可以看到存入的数据。
infor.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实验室设备管理系统</title>
</head>
<body οnlοad="load();">
<h1>实验室设备管理系统</h1>
<div>
<table>
<thead>
<tr>
<th>编号</th>
<th>账号</th>
<th>密码</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>电话</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="i:${session.infor}">
<td th:text="${i.getId()}"></td>
<td th:text="${i.getUsername()}"></td>
<td th:text="${i.getPassword()}"></td>
<td th:text="${i.getName()}"></td>
<td th:text="${i.getSex()}"></td>
<td th:text="${i.getAge()}"></td>
<td th:text="${i.getTel()}"></td>
<td>
<button>编辑</button>
<button>删除</button>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>h1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>h1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<version>2.9.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.html</include>
<include>**/*.css</include>
</includes>
</resource>
</resources>
</build>
</project>
在网上找了好多,感觉可能是因为InfoController里使用return "redirect:/static/infor.html";跳转的原因。
但是在最早创建项目时就遇到了用return "infor";跳转不过去的问题,网页报错:
控制台报错:
所以才使用return "redirect:/static/infor.html";跳转。
有没有办法让使用return "redirect:/static/infor.html";跳转前端能获取到数据,或者怎么使用return "infor";跳转?
求带佬指导一下,谢谢!