java - Spring Boot中如何解决 "Exception evaluating SpringEL expression"?

标签 java spring thymeleaf

我在 Spring boot 应用程序中使用 Microsoft SQL。我已经弄清楚了一切,现在我正在尝试将数据从 SQL 数据库传递到前端。但是,当我通过 thymeleaf 传递信息时,它不起作用。我有一个应用程序存储库以及一个实际传递数据的 DTO 类。尽管我不断收到同样的错误。很抱歉用这么多代码轰炸大家,但我已经尝试找到解决方案一段时间了,但仍然想不出解决方法。我相信它与 TestApp 类有关,但不确定在哪里。

错误:


    org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'dtoTiername' cannot be found on object of type 'com.John.model.TestApp' - maybe not public or not valid?

应用程序存储库:


        @Query("SELECT new com.John.model.TestApp(t.tiername, t.system) FROM AppSelectorTier t")
        List<TestApp>getServerInfo();

服务层:



    public List<TestApp> getServerInfo() {
                List<TestApp> myList = appRepository.getServerInfo();
                System.out.println(myList.size());
                return myList;
            }

Controller :

        @RequestMapping("/edit")
        public ModelAndView editTab(Model model) {

            ModelAndView modelAndView = new ModelAndView();
            List<TestApp>ary4 = new ArrayList<TestApp>();
            try {
                System.out.println("Hello Code");
                ary4 = joinQueryService.getServerInfo();
                modelAndView.setViewName("create");             

            } catch (Exception e) {
                e.printStackTrace();
                modelAndView.setViewName("test");           
            }
            modelAndView.addObject("ary4",ary4);
            return modelAndView;                                
        }

HTML:


    <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                      <th>Tiername</th>
                      <th>System</th>
                    </tr>
                </thead>

                 <tbody>
                    <tr th:each="ary4 : ${ary4}">
                        <td th:text="${ary4.dtoTiername}"></td>
                        <td th:text="${ary4.dtoSystem}"></td>
                    </tr>
                </tbody>

               </table>

测试应用:

package com.John.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

public class TestApp implements java.io.Serializable  {

    private String dtoTiername;
    private String dtoSystem;

    private static final long serialVersionUID = 1L;


    //Constructor
    public TestApp(String dtoTiername, String dtoSystem) {
        super();
        this.dtoTiername = dtoTiername;
        this.dtoSystem = dtoSystem;
    }

    //Default Constructor
    public TestApp() {

    }



    //Getter
    public String getName() {
        return dtoTiername;
    }

    public String getSystem() {
        return dtoSystem;
    }


    //Setter
    public void setName(String dtoTiername) {
        this.dtoTiername = dtoTiername;
    }


    public void setSystem(String dtoSystem) {
        this.dtoSystem = dtoSystem;
    }

}

实体:

package com.John.model;

import java.util.*;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "tier")
public class AppSelectorTier implements java.io.Serializable {

    @Id
    @Column(name = "tiername")
    private String tiername;

    @Column(name = "system")
    private String system;

    private static final long serialVersionUID = 1L;


    //Constructor
    public AppSelectorTier(String tiername, String system) {
        super();
        this.tiername = tiername;
        this.system = system;
    }

    //Default Constructor
    public AppSelectorTier() {

    }



    //Getter
    public String getName() {
        return tiername;
    }

    public String getSystem() {
        return system;
    }


    //Setter
    public void setName(String tiername) {
        this.tiername = tiername;
    }


    public void setSystem(String system) {
        this.system = system;
    }

}

最佳答案

属性由 getter 和 setter 映射,因此您可以执行其中任一操作

第一:

保持 getter setter 与变量名称对齐

public class TestApp implements java.io.Serializable  {

    private String dtoTiername;
    private String dtoSystem;

    public String getDtoTiername() {
        return dtoTiername;
    }
    public void setDtoTiername(String dtoTiername) {
        this.dtoTiername = dtoTiername;
    }
    public String getDtoSystem() {
        return dtoSystem;
    }
    public void setDtoSystem(String dtoSystem) {
        this.dtoSystem = dtoSystem;
    }

}

第二:

将 JSP 变量更改为

<tr th:each="ary4 : ${ary4}">
    <td th:text="${ary4.name}"></td>
    <td th:text="${ary4.system}"></td>
</tr>

关于java - Spring Boot中如何解决 "Exception evaluating SpringEL expression"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231579/

相关文章:

java - 双重检查锁定保证对象的状态 ? (实践中的并发)

java - 使用 Java 和 Vue 创建 Web 应用程序的更好方法

java - Spring框架MVC中AOP的实现

thymeleaf - 如何在 thymeleaf 中转义双引号“?

java - 浏览器不会将我重定向到 Twitter 进行授权

java - 这一行交换了两个变量,但它是如何进行的呢?

java - 缓存 javax.sql.Datasource 对象的单个实例是个好主意吗?

java - Spring Controller 注入(inject)的属性变空

java - AOP:基本思想 - 保持对象简单?

java - 如何在 Spring Boot 中将变量发送到 Controller 而不是作为表单输入