0

我是使用 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>
4

1 回答 1

2

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

因此,当您使用第一个选择时

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

实际上,您最终会Result<ConfigurationRecord>得到一个getName可以被 Thymeleaf 使用的访问器。

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

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

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

你必须对每个领域都这样做。

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

让我知道这是否有帮助。

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

于 2016-10-10T21:21:09.553 回答