View层中的Object null

时间:2018-02-01 09:59:32

标签: spring thymeleaf

我有这个问题:

O pedido:

public function __construct($id, $name, $vorname, $matrikelnummer, $status, $wl_platz, $modul, $versuch) 
{
    $this->id=$id;
    $this->name=$name;     // '$' sign in attribute name
    $this->vorname=$vorname;
    $this->matrikelnummer=$matrikelnummer;
    $this->status=$status;
    $this->wl_platz=$wl_platz;
    $this->modul=$modul;
    $this->versuch=$versuch;
}

O控制器:

@Entity
@Table(name="wp_posts")
public class Pedido implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Long id;

@Column(name="post_status")
private String status;

@Column(name="post_type")
private String tipo;

@OneToMany(mappedBy = "pedido", cascade=CascadeType.ALL, orphanRemoval = true)  
private List<PedidoMeta> pedidoMeta;


@OneToMany(mappedBy = "pedido", cascade=CascadeType.ALL, orphanRemoval = true)
private List<PedidoItem> itens;

@Column(name="post_date")
private Date data;

@Transient
private Usuario cliente;

观点:

@GetMapping(value = "/detalhePedido/{id}")
public ModelAndView detalhePedido( @PathVariable(value = "id", required = false) String pedidoid) {

    pedido = pedidoRepository.findOne(new Long(pedidoid));
    pedido = pedidoService.findCliente(pedido);
    ModelAndView modelAndView = new ModelAndView("DetalhePedido");
    modelAndView.addObject("pedido",pedido);
    return modelAndView;
}

O erro:

<tbody>
  <tr th:object="${pedido}">
   <td th:text="*{pedido.id}"></td>
   <td th:text="*{pedido.cliente.nome}"></td>
   <td th:text="*{#dates.format(pedido.data, 'dd-MM-yyyy')}"></td>
 </tr>
</tbody>

调试时,请求是正确的,包含所有信息,但在可视层中接收时会出现此错误。

1 个答案:

答案 0 :(得分:0)

Welcome to SO.

The issue is in your HTML. You may want simply:

<tbody>
  <tr>
   <td th:text="${pedido.id}"></td>
   <td th:text="${pedido.cliente.nome}"></td>
   <td th:text="${#dates.format(pedido.data, 'dd-MM-yyyy')}"></td>
 </tr>
</tbody>

Or, if you are using th:object, then you're telling Thymeleaf that you are selecting the pedido variable and therefore shouldn't specify it again (unless pedido has a property also called pedido):

<tbody th:object="${pedido}">
  <tr>
   <td th:text="*{id}"></td>
   <td th:text="*{cliente.nome}"></td>
   <td th:text="*{#dates.format(data, 'dd-MM-yyyy')}"></td>
 </tr>
</tbody>

The error message states exactly this.

See the docs for examples.