账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Springboot+Thymeleaf中,SpringEL的使用
    65
    0

    在Html页面中,使用el表达式,绑定后台值没有问题,但怎么使用后台的方法对值进行处理呢?

    <th:block th:each="post : ${users.list}">
       <tr th:cid="${post.uid}">
          <td><th:block th:text="${post.created}"/></td>
          <td><th:block th:text="${com.gateway.website.utils.Commons.fmtdate(post.created)}"/></td>
       </tr>
    </th:block>

    fmtdate方法是个时间戳转换为日期的方法。
    单独写第一个td是是有值的,写第二个会报错,Exception evaluating SpringEL expression
    不清楚怎么用,还是说fmtdate方法这边要做什么工作,往指导...谢谢

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 梦境与我为邻 普通会员 1楼

      Spring Boot + Thymeleaf 是一个常用的 Java Web 应用开发模式。在 Spring Boot 中,我们可以使用 Spring EL(Spring Expression Language)来进行动态配置和动态数据的处理。以下是一个简单的例子,展示了如何在 Thymeleaf 中使用 Spring EL。

      首先,我们需要在 Spring Boot 中添加 Spring EL 的依赖。在 Maven 或 Gradle 中,我们可以添加以下依赖:

      xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

      然后,我们可以使用 Spring EL 来动态配置 Thymeleaf。以下是一个简单的例子,展示了如何使用 Spring EL 来动态设置 Thymeleaf 的模板语言:

      ```java @Configuration public class ThymeleafConfig {

      @Value("${spring.app.name}")
      private String applicationName;
      
      @Bean
      public ThymeleafViewResolver viewResolver() {
          ThymeleafViewResolver resolver = new ThymeleafViewResolver();
          resolver.setApplicationContext(new ClassPathXmlApplicationContext("templates"));
          resolver.setTemplateEngine(new ThymeleafTemplateEngine());
          resolver.setPrefix("/WEB-INF/templates");
          resolver.setSuffix(".html");
          return resolver;
      }
      
      @Bean
      public ThymeleafTemplateEngine templateEngine() {
          ThymeleafTemplateEngine templateEngine = new ThymeleafTemplateEngine();
          templateEngine.setPrefix("/WEB-INF/templates");
          templateEngine.setSuffix(".html");
          return templateEngine;
      }
      

      } ```

      在这个例子中,我们在 Spring Boot 的配置类中定义了 SpringEL 的配置。然后,我们在模板引擎类中定义了 Thymeleaf 的模板语言,并在模板文件中使用 SpringEL 来动态配置模板语言。

      在 Thymeleaf 中,我们使用 ${} 来动态设置模板的值。例如,我们可以使用 ${applicationName} 来动态设置模板的值:

      ```html

      My Application

      Hello, World!

      ```

      在这个例子中,我们在 Thymeleaf 的 <head> 标签中使用 ${applicationName} 来动态设置模板的值。然后,在 <body> 标签中使用 <h1> 标签来显示模板的值。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部