简体   繁体   English

如何将@value注解的值传递给SpringBoot中的controller

[英]How to pass the value of an @value annotation to the controller in SpringBoot

I have a spring in which I use the dependencies of: spring-boot-starter-web v-2.7.3, spring-boot-devtools and spring-boot-starter-thymeleaf my purpose is to send an "Invoice" class with the dependencies of a client class and an ItemFactura class to the controller, with the @value annotation I add an environment variable located in the application.properties:我有一个 spring,我在其中使用以下依赖项:spring-boot-starter-web v-2.7.3、spring-boot-devtools 和 spring-boot-starter-thymeleaf 我的目的是发送“发票”class客户端 class 和 ItemFactura class 对 controller 的依赖关系,使用 @value 注释我添加了一个位于 application.properties 中的环境变量:

factura.descripcion: Deporte

I add it with @value to my java invoice class:我用@value 将它添加到我的 java 发票 class 中:

@Component
public class Factura {

@Value("${factura.descripcion}")
private String descripcion;

@Autowired
private Cliente cliente; 

private List<ItemFactura> items; 

/**
 * @return String return the description
 */
public String getDescription() {
    return descripcion;
}

/**
 * @param descripcion the description to set
 */
public void setDescription(String descripcion) {
    this.descripcion = descripcion;
}

/**
 * @return Cliente return the cliente
 */
public Cliente getCliente() {
    return cliente;
}

/**
 * @param cliente the cliente to set
 */
public void setCliente(Cliente cliente) {
    this.cliente = cliente;
}

/**
 * @return List<ItemFactura> return the items
 */
public List<ItemFactura> getItems() {
    return items;
}

/**
 * @param items the items to set
 */
public void setItems(List<ItemFactura> items) {
    this.items = items;
}

} }

Después de esto inyecto la clase Factura en el controlador: Después de esto inyecto la clase Factura en el controlador:

@Controller
@RequestMapping("/Factura")
public class FacturaController {

@Autowired
private Factura factura; 

@GetMapping("/ver")
public String verFactura(Model model){
    model.addAttribute("factura", factura);
    model.addAttribute("Titulo_factura", "Esto es el título de una factura");

    return "index";
    
}

} }

Y por último la llamó en mi index.html: Y por último la llamó en mi index.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Facturas</title>
</head>
<body>
<h1>Aquí se ven las facturas</h1>

<h1 th:text="${Titulo_factura}"></h1>
<h1 th:text="${factura.descripcion}"></h1>



</body>
</html>

When I lift again and enter my route I get the error: Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "invoice.description" (template: "ver-invoice" - line 13, col 9) and **Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'description' cannot be found on object of type 'com.nodependencyinjection.demo.Domain.Factura' - maybe not public or not valid?当我再次抬起并进入我的路线时,我收到错误:由:org.attoparser.ParseException:评估 SpringEL 表达式的异常:“invoice.description”(模板:“ver-invoice”-第 13 行,第 9 列)和**原因:org.springframework.expression.spel.SpelEvaluationException:EL1008E:在“com.nodependencyinjection.demo.Domain.Factura”类型的 object 上找不到属性或字段“描述” - 可能不公开或无效? ** **

Mi primera reacción fue cambiar private descripcion por public, pero lo que intento es realizar un encapsulamiento del objeto, la única opción que veo para este error es que mi clase com.nodependencyinjection.demo.Domain.Factura y mi com.nodependencyinjection.demo.Controllers están en paquetes diferentes, o que para utilizar las variables de entorno haya que agregar una configuración especial en application.properties, la cual desconozco, cualquier ayuda se agradece de antemano Mi primera reacción fue cambiar private descripcion por public, pero lo que intento es realizar un encapsulamiento del objeto, la única opción que veo para este error es que mi clase com.nodependencyinjection.demo.Domain.Factura y mi com.nodependencyinjection.demo. Controllers están en paquetes diferentes, o que para utilizar las variables de entorno haya que agregar una configuración especial en application.properties, la cual desconozco, cualquier ayuda se agradece de antemano

pddt: when I add "factura" only in the html the page loads correctly and I get the hash of the class. pddt:当我仅在 html 中添加“factura”时,页面加载正确,我得到 class 的 hash。

this confuses me...这让我很困惑...... html图片

Property is descripcion so you need a getter getDescripcion() and not getDescription()属性是descripcion ,所以你需要一个getter getDescripcion()而不是getDescription()

The value of an @value annotation is pass through application.properties file to the controller and value is Static. @value 注释的值通过 application.properties 文件传递到 controller,值为 Static。

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

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