【SpringBoot】Thymeleaf获取ModelAndView传的值

Thymeleaf获取ModelAndView传的值

Thymeleaf获取ModelAndView的值有俩种可能,一种是直接由后台传递的值,第二种是后台重定向传递值

后台直接传递

后台

        ModelAndView mv = new ModelAndView();
        mv.setViewName("/hello");
        return mv;

前端

<div th:text="${msg}"></div>

重定向传值

后台

        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/hello");
        return mv;

前端

<div th:text="${param.msg}"></div>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Spring Data JPA来访问两个数据库表,然后在Thymeleaf模板中分别展示它们的数据。 首先,在你的Spring Boot应用中,你需要配置两个数据源和实体管理器工厂。这个可以通过在application.properties文件中添加以下配置来完成: ``` # 第一个数据源 spring.datasource.url=jdbc:mysql://localhost:3306/db1 spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 第二个数据源 datasource2.url=jdbc:mysql://localhost:3306/db2 datasource2.username=root datasource2.password=password datasource2.driver-class-name=com.mysql.jdbc.Driver # 配置JPA spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.properties.hibernate.hbm2ddl.auto=create spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true ``` 然后,你需要在你的应用中定义两个数据源的JdbcTemplate实例,以便你可以使用它们来执行查询。这可以通过像这样在你的应用中定义两个JdbcTemplate实例来完成: ``` @Configuration public class DataSourceConfig { @Bean(name = "jdbcTemplate1") public JdbcTemplate jdbcTemplate1(DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "jdbcTemplate2") public JdbcTemplate jdbcTemplate2(@Qualifier("dataSource2") DataSource dataSource) { return new JdbcTemplate(dataSource); } } ``` 接下来,你需要为每个数据库表定义一个实体类,并使用Spring Data JPA创建一个存储库来访问它们。例如,对于一个名为“table1”的表,你可以定义一个名为“Table1”的实体类和一个名为“Table1Repository”的存储库: ``` @Entity @Table(name = "table1") public class Table1 { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; // 省略getter和setter } @Repository public interface Table1Repository extends JpaRepository<Table1, Long> { } ``` 然后,在你的控制器中,你可以注入这两个存储库,并使用它们来获取两个数据库表的数据。例如: ``` @Controller public class MyController { @Autowired private Table1Repository table1Repository; @Autowired @Qualifier("jdbcTemplate2") private JdbcTemplate jdbcTemplate2; @GetMapping("/") public ModelAndView index() { List<Table1> table1List = table1Repository.findAll(); List<Map<String, Object>> table2List = jdbcTemplate2.queryForList("SELECT * FROM table2"); ModelAndView modelAndView = new ModelAndView("index"); modelAndView.addObject("table1List", table1List); modelAndView.addObject("table2List", table2List); return modelAndView; } } ``` 最后,在Thymeleaf模板中,你可以使用类似以下的代码来展示这两个数据库表的数据: ``` <!-- 展示第一个数据库表的数据 --> <table> <thead> <tr> <th>id</th> <th>name</th> </tr> </thead> <tbody> <tr th:each="table1 : ${table1List}"> <td th:text="${table1.id}"></td> <td th:text="${table1.name}"></td> </tr> </tbody> </table> <!-- 展示第二个数据库表的数据 --> <table> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> </tr> </thead> <tbody> <tr th:each="table2 : ${table2List}"> <td th:text="${table2.id}"></td> <td th:text="${table2.name}"></td> <td th:text="${table2.age}"></td> </tr> </tbody> </table> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值