【问题标题】:Property or field 'isLPG' cannot be found on object of type 'com.rentautosofia.rentacar.bindingModel.CarBindingModel' - maybe not public?在“com.rentautosofia.rentacar.bindingModel.CarBindingModel”类型的对象上找不到属性或字段“isLPG” - 也许不是公开的?
【发布时间】:2018-04-16 17:31:56
【问题描述】:

我有一个像这样的 MVC 逻辑。

汽车绑定模型

package com.rentautosofia.rentacar.bindingModel

import javax.validation.constraints.Size

class CarBindingModel(@Size(min = 1)
                      var name: String = "",
                      @Size(min = 1)
                      var price: Int = 0,
                      @Size(min = 1)
                      var imgURL: String = "",
                      var isLPG: Boolean? = false)

汽车控制器

package com.rentautosofia.rentacar.controller.admin

import ...

const val PATH_ADMIN_CAR = "admin/car"
const val PATH_ADMIN_ALL_CARS = "$PATH_ADMIN_CAR/all"

@RequestMapping("/$PATH_ADMIN_CAR")
@Controller
class CarController @Autowired
constructor(private val carRepository: CarRepository) {

    @GetMapping("/create")
    fun create(model: Model): String {
        model.addAttribute("car", CarBindingModel())
        model.addAttribute("view", "$PATH_ADMIN_CAR/create")
        return "base-layout"
    }

    @PostMapping("/create")
    fun createProcess(model: Model, @Valid carBindingModel: 
CarBindingModel, bindingResult: BindingResult): String {
        if (bindingResult.hasErrors()) {
            model.addAttribute("view", "$PATH_ADMIN_CAR/create")
            model.addAttribute("message", "Invalid data.")
            model.addAttribute("car", carBindingModel)
            return "base-layout"
        }

        val car = car {
            name = carBindingModel.name
            price = carBindingModel.price
            imgURL = carBindingModel.imgURL
            isLPG = carBindingModel.isLPG
        }

        this.carRepository.saveAndFlush(car)
        return "redirect:/$PATH_ADMIN_ALL_CARS"
    }
...
}

汽车视图

重要的是这个(这是我得到错误的地方)

<input  id="isLPG" type="checkbox" name="isLPG" th:checked="${car.isLPG}"/>

问题是我认为我得到了一个 SpEL。它说它在CarBindingModel 中找不到属性isLPG

错误:

An error happened during template parsing (template: "class path resource [templates/base-layout.html]")
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "car.isLPG" (template: "admin/car/create" - line 16, col 65)
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "car.isLPG" (template: "admin/car/create" - line 16, col 65)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: 
Property or field 'isLPG' cannot be found on object of type 'com.rentautosofia.rentacar.bindingModel.CarBindingModel' - maybe not public?
org.springframework.expression.spel.SpelEvaluationException: EL1008E: 
Property or field 'isLPG' cannot be found on object of type 'com.rentautosofia.rentacar.bindingModel.CarBindingModel' - maybe not public?

我基本上是想从这个复选框绑定一个布尔值。 如果您需要更多详细信息,我会很乐意添加它们。

【问题讨论】:

  • 尝试这样调用:${car.getIsLPG()}。如果 Kotlin 生成 Java Bean 访问器,当然……
  • 我试试看。
  • 您的解决方案不起作用。找不到方法。

标签: spring-boot kotlin thymeleaf spring-el


【解决方案1】:

看起来 SpEL 被 is 前缀混淆了。

这行得通

package foo

class CarBindingModel(
                  var name: String = "",

                  var price: Int = 0,

                  var imgURL: String = "",

                  var LPG: Boolean? = false)

public class So49863132Application {

    public static void main(String[] args) {
        CarBindingModel car = new CarBindingModel("Lexus", 42, "img.png", true);
        Expression exp = new SpelExpressionParser().parseExpression("name + ' ' + price + ' ' + LPG");
        System.out.println(exp.getValue(car));
    }

}

Lexus 42 true

编辑

这是由于 Kotlin 创建 getter 的方式;对于isLPG,kotlin 生成isLPG()

SpEL 期待 isIsLPG()

【讨论】:

  • 这是 Kotlin/SpEL 断开连接 - SpEL 需要真正的 JavaBean 规则。
  • isLPG()!嗯? Kotlin 再次不尊重它所开发的语言...... :-)
  • 我猜他们是KotlinBeans,而不是JavaBeans:D
  • isLPG是公开的,是不是意味着无论如何都要封装?
  • 曾与LPG 合作,但有没有办法以某种方式将字段命名为isLPG 跨语言?
猜你喜欢
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
相关资源
最近更新 更多