【问题标题】:Thymeleaf view can't find property/field of jOOQ join queryThymeleaf 视图找不到 jOOQ 连接查询的属性/字段
【发布时间】:2017-02-18 18:25:28
【问题描述】:

我是使用 Spring、Thymeleaf 和 jOOQ 的新手。我想在概览中显示数据库查找的结果。当我只是查询 select().from(CONFIGURATIONS) 时,Thymeleaf 视图工作得很好。但是,当我想加入 User 表以将用户名添加到结果中而不是仅添加 id 时,我会收到错误消息。

这是浏览器中的错误:

2016 年 10 月 10 日星期一 16:04:17 CEST 出现意外错误(类型=内部服务器错误,状态=500)。 评估 SpringEL 表达式的异常:“config.name”(概述:37)

这是 Eclipse 中的错误:

org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 7): 在“org.jooq.impl.RecordImpl”类型的对象上找不到属性或字段“名称” - 可能不公开?

我知道我的视图代码是错误的,但我不知道应该将“config.name”更改为什么以获得正确的 property.field。

这是我的控制器:

@Controller
public class OverviewController
{
    public ArrayList configurationList = new ArrayList();

    @RequestMapping("/overview")
    public String showConfigurationList(Model model) 
    {
        model.addAttribute("configs", getConfigurations());
        return "overview";
    }

    public ArrayList getConfigurations()
    {
        try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) 
        {
            DSLContext create = DSL.using(conn, SQLDialect.POSTGRES_9_5);
            Result<Record5<String, String, Timestamp, String, String>> result = create.select(CONFIGURATION.NAME, CONFIGURATION.VERSION, CONFIGURATION.DATE_MODIFIED, CONFIGURATION.AUTHOR_USER_ID, USER.NAME)
                    .from(USER).join(CONFIGURATION)
                    .on(CONFIGURATION.AUTHOR_USER_ID.equal(USER.ID)))
                    .fetch();
            /*Result<Record> result = create.select()
                    .from(CONFIGURATION)
                    .fetch();*/

            if(configurationList.isEmpty() == true)
            {
                for (Record5 r : result) {                  
                configurationList.add(r);
                }
            }               
        }             
        catch (Exception e) 
        {
            System.out.println("ERROR: " + e);          
        }
        return configurationList;
    }

这是 Thymeleaf 视图:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <title>Nazza Mediator - Overview</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
    <script src="http://code.jquery.com/jquery.js" />
    <script src="webjars/bootstrap/3.3.7-1/js/bootstrap.min.js" />
</head>

<body>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="/">Nazza Mediator</a>
    </div>
    <ul class="nav navbar-nav">
      <li><a href="/">Home</a></li>
      <li class="active"><a href="/overview">Configuration Overview</a></li>
    </ul>
  </div>
</nav>

    <h3>Configuration Overview</h3>

    <table class="table table-hover">
      <tr>
        <th>NAME</th>
        <th>VERSION</th>
        <th>DATE MODIFIED</th>
        <th>AUTHOR</th>
      </tr>
      <tr th:each="config : ${configs}">
        <td th:text="${config.name}"></td>
        <td th:text="${config.version}"></td>
        <td th:text="${config.dateModified}"></td>
        <td th:text="${config.authorUserId}"></td>
      </tr>
    </table>
  </body>

</html>

【问题讨论】:

    标签: java spring thymeleaf jooq spelevaluationexception


    【解决方案1】:

    我假设您已经启动并运行了 jOOQ 代码生成器。

    所以,当你使用第一个选择时

    Result<Record> result = create.select()
                    .from(CONFIGURATION)
                    .fetch();
    

    你最终会得到Result&lt;ConfigurationRecord&gt;,并且那个有一个访问器getName,可以被Thymeleaf使用。

    如果您写下连接并手动添加字段,您将拥有一个通用记录,一个仅提供 #get(String fieldName) 和其他一些的实现。

    在您看来,您必须更改对值的访问权限,如下所示

    <td th:text="${config.get('NAME')}"></td>
    

    您必须对每个字段都这样做。

    但要小心,在您的查询中,您有两个名为“name”的字段,我想您应该为其中一个设置别名。例如,USER.NAME.as('userName') 并在调用中使用它来获取。

    如果有帮助,请告诉我。

    另一个编辑:来自 jOOQ 的 Lukas Eder 询问是否可以使用 config['NAME']:这对于通用记录是不可能的,但也只能用于特定记录。此外,在所有引用字段名称的情况下,都必须小心,因为它区分大小写。

    【讨论】:

    • 是的,成功了!感谢您的解决方案,也非常感谢您的解释,我现在明白为什么我的代码不起作用了。
    猜你喜欢
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多