闪闪和碧血 2023-02-19 23:24 采纳率: 100%
浏览 46
已结题

springboot

使用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在控制台可以看到存入的数据。

img

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";跳转不过去的问题,网页报错:

img

控制台报错:

img

所以才使用return "redirect:/static/infor.html";跳转。
有没有办法让使用return "redirect:/static/infor.html";跳转前端能获取到数据,或者怎么使用return "infor";跳转?
求带佬指导一下,谢谢!

  • 写回答

6条回答 默认 最新

  • 不咕鸟会咕咕 2023-02-19 23:32
    关注

    根据您提供的代码和信息,您在InfoController中的list方法中将数据添加到session中,然后重定向到了一个静态页面infor.html。但是您在infor.html中无法通过model或session获取数据。

    这是因为Thymeleaf是一个服务器端的模板引擎,它的作用是在服务器端渲染模板,并将渲染后的HTML代码发送给浏览器。因此,Thymeleaf模板中无法直接获取session或request中的数据。

    如果您想在infor.html中展示数据,可以考虑使用Ajax来获取数据,或者在后端将数据渲染到模板中并返回给浏览器。具体的实现方法取决于您的具体需求。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月21日
  • 已采纳回答 2月21日
  • 创建了问题 2月19日

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!