简体   繁体   English

Thymeleaf视图找不到jOOQ连接查询的属性/字段

[英]Thymeleaf view can't find property/field of jOOQ join query

I am new to working with Spring, Thymeleaf and jOOQ. 我刚接触Spring,Thymeleaf和jOOQ。 I want to display the results from the database lookup in the overview. 我想在概述中显示数据库查找的结果。 When I just query select().from(CONFIGURATIONS) the Thymeleaf view works just fine. 当我只查询select()。from(CONFIGURATIONS)时,Thymeleaf视图工作正常。 But when I want to join with the User table to add the name of the user to the results instead of just the id, I get an error. 但是,当我想与User表联接以将用户名(而不只是ID)添加到结果中时,出现错误。

This is the error in the webbrowser: 这是网络浏览器中的错误:

Mon Oct 10 16:04:17 CEST 2016 There was an unexpected error (type=Internal Server Error, status=500). 2016年10月10日星期一10:04:17 CEST发生意外错误(类型=内部服务器错误,状态= 500)。 Exception evaluating SpringEL expression: "config.name" (overview:37) 评估SpringEL表达式的异常:“ config.name”(概述:37)

This is the error in Eclipse: 这是Eclipse中的错误:

org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 7): Property or field 'name' cannot be found on object of type 'org.jooq.impl.RecordImpl' - maybe not public? org.springframework.expression.spel.SpelEvaluationException:EL1008E:(pos 7):在“ org.jooq.impl.RecordImpl”类型的对象上找不到属性或字段“名称”-也许不是公共的?

I get that my view code is wrong, but I don't know what I should change "config.name" into to get the right property.field. 我知道我的视图代码是错误的,但是我不知道我应该将“ config.name”更改为正确的property.field。

This is my controller: 这是我的控制器:

@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;
    }

And this is the Thymeleaf view: 这是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>

I assume you have the jOOQ code generator up and running. 我假设您已经启动并运行了jOOQ代码生成器。

So, when you're using the first select 因此,当您使用第一个选择

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

You'll actually end up with Result<ConfigurationRecord> and that one has an accessor getName which can be used by Thymeleaf. 实际上,您最终将得到Result<ConfigurationRecord>并且该getName具有可被Thymeleaf使用的访问器getName

If you write the join down and add the fields manually, you'll have a generic record, an implementation that only provides #get(String fieldName) and some others. 如果写下连接并手动添加字段,则将有一个通用记录,该记录仅提供#get(String fieldName)和其他一些实现。

In your view you have to change the access to the values as follows 在您看来,您必须按以下方式更改对值的访问权限

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

You have to do this for every field. 您必须对每个字段都执行此操作。

But be careful, in your query you have two fields named "name", I guess you should alias one of them. 但是要小心,在查询中有两个名为“名称”的字段,我想您应该为其中一个别名。 For example, USER.NAME.as('userName') and use that in the call to get. 例如, USER.NAME.as('userName')并在调用中使用它来获取。

Let me know if that helps. 让我知道是否有帮助。

One more edit: Lukas Eder from jOOQ asked wether it is possible to use config['NAME'] : This is not possible with the generic record, but only with the specific as well. 另一个编辑:jOOQ的Lukas Eder询问是否可以使用config['NAME'] :通用记录不可能,但特定记录也不能。 Also, in all cases where you reference the field name, you gotta be careful, because it is case sensitive. 另外,在所有引用字段名称的情况下,都必须小心,因为它区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM