在百里香模板中显示值

时间:2019-06-29 14:52:15

标签: spring-boot spring-data-jpa thymeleaf

我构建了thymeleaf模板,以显示数据库中特定字段中的一些值。我加入了两个类“ Iden”和“ Target”,每个“ iden”都有许多“ target”。我试图显示那些将两个类互连的值,但在我的百里香模板中出现了一个错误(“在null上找不到属性或字段'ref')”。任何人都可以帮助您找到会发生什么。

<td th:text="${p.iden.ref}"></td>

这是iden类的代码:

package com.example.dot.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@Entity
@Table(name = "iden")
@JsonIdentityInfo(
	      generator = ObjectIdGenerators.PropertyGenerator.class, property = "id_iden")

public class Iden implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue
	private Long id_iden;
	@DateTimeFormat(pattern = "yyyy-MM-dd") 
	@Column(name = "dte_deb_inter", columnDefinition="DATE")
	private Date dte_deb_inter;
	private String cse_deb_inter;
	private String ref ;
	private String dir_gen;
	private String dir_chg;
	
	
	@OneToMany(mappedBy = "iden", fetch = FetchType.LAZY)
	private List<Target> targets;
	
	@ManyToOne
	@JoinColumn(name = "CODE_ORD")
	public Ordre ordre;

	public List<Target> getTargets() {
		return targets;
	}

	public void setTargets(List<Target> targets) {
		this.targets = targets;
	}

	public Iden() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Iden(Date dte_deb_inter,  String cse_deb_inter, 
			String ref,String dir_gen,String dir_chg ,Ordre ordre) {
		super();
		this.dte_deb_inter = dte_deb_inter;
		
		this.cse_deb_inter = cse_deb_inter;
		
		this.dir_chg =dir_chg;
		this.dir_gen =dir_gen;
		this.ref =ref;
		this.ordre = ordre;
	}

	public Long getId_iden() {
		return id_iden;
	}

	public void setId_iden(Long id_iden) {
		this.id_iden = id_iden;
	}

	public Date getDte_deb_inter() {
		return dte_deb_inter;
	}

	public void setDte_deb_inter(Date dte_deb_inter) {
		this.dte_deb_inter = dte_deb_inter;
	}

	

	public String getCse_deb_inter() {
		return cse_deb_inter;
	}

	public void setCse_deb_inter(String cse_deb_inter) {
		this.cse_deb_inter = cse_deb_inter;
	}

	

	public Ordre getOrdre() {
		return ordre;
	}

	public void setOrdre(Ordre ordre) {
		this.ordre = ordre;
	}

	
	public String getRef() {
		return ref;
	}

	public void setRef(String ref) {
		this.ref = ref;
	}

	public String getDir_gen() {
		return dir_gen;
	}

	public void setDir_gen(String dir_gen) {
		this.dir_gen = dir_gen;
	}

	public String getDir_chg() {
		return dir_chg;
	}

	public void setDir_chg(String dir_chg) {
		this.dir_chg = dir_chg;
	}

	

}

这是目标类的代码:

package com.example.dot.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@Entity
@Table(name = "target")

@JsonIdentityInfo(
	      generator = ObjectIdGenerators.PropertyGenerator.class, property = "id_trg")
public class Target implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue
	private Long id_trg;
	@DateTimeFormat(pattern = "yyyy-MM-dd") 
	@Column(name = "dte_fin_inter", columnDefinition="DATE")
	private Date dte_fin_inter;
    private String cse_fin_inter;
	private String acti ;
	private Integer flag_status;
	private String name ;
	private String type ;
	private Long number ;
	private String st;
	private String ope;
	private String por ;
	
	@ManyToOne
	@JoinColumn(name = "CODE_IDEN")
	public Iden iden;
	
	public Target() {
		super();
		// TODO Auto-generated constructor stub
	}

	
	
	
	
	public Target(Date dte_fin_inter, String cse_fin_inter, String acti, Integer flag_status, String name, String type,
			Long number, String st, String por,String ope, Iden iden) {
		super();
		this.ope = ope;
		this.dte_fin_inter = dte_fin_inter;
		this.cse_fin_inter = cse_fin_inter;
		this.acti = acti;
		this.flag_status = flag_status;
		this.name = name;
		this.type = type;
		this.number = number;
		this.st = st;
		this.por = por;
		this.iden = iden;
	}





	public Long getId_trg() {
		return id_trg;
	}

	public void setId_trg(Long id_trg) {
		this.id_trg = id_trg;
	}

	public Date getDte_fin_inter() {
		return dte_fin_inter;
	}

	public void setDte_fin_inter(Date dte_fin_inter) {
		this.dte_fin_inter = dte_fin_inter;
	}

	public String getCse_fin_inter() {
		return cse_fin_inter;
	}

	public void setCse_fin_inter(String cse_fin_inter) {
		this.cse_fin_inter = cse_fin_inter;
	}

	public String getActi() {
		return acti;
	}

	public void setActi(String acti) {
		this.acti = acti;
	}

	public Integer getFlag_status() {
		return flag_status;
	}

	public void setFlag_status(Integer flag_status) {
		this.flag_status = flag_status;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Long getNumber() {
		return number;
	}

	public void setNumber(Long number) {
		this.number = number;
	}

	public String getSt() {
		return st;
	}

	public void setSt(String st) {
		this.st = st;
	}

	public String getpor() {
		return por;
	}

	public void setpor(String por) {
		this.por = por;
	}

	public Iden getIden() {
		return iden;
	}

	public void setIden(Iden iden) {
		this.iden = iden;
	}





	public String getOpe() {
		return ope;
	}





	public void setOpe(String ope) {
		this.ope = ope;
	}


  
}

这是获取目标列表的控制器代码:

@Controller
public class TargetController {

	@Autowired
Targetrepository trgrepo;
	
	
	
	
	@RequestMapping("/alltarget")
	public String formall(Model model) {
		List<Target> listTarget =trgrepo.findAll();
	model.addAttribute("list",listTarget);
		return "allIdentifiant";
	}
	
	
	
}

最后是百里香模板:

	<tbody>
				<tr th:each="p:${list}">
				
					<td th:text="${p.cse_fin_inter}"></td>
				
					
					<td><span th:text="${p.iden.ref}" ></span>	</td>
			     	
					
					<td th:text="${p.acti}"></td>
					<td><span th:text="${p.por}" ></span></td>
					<td><span th:text="${p.ope}"></span></td>
					
					<td th:text="${#dates.format(p.dte_fin_inter, 'yyyy-MM-dd')}"></td>
					
					<td><span th:text="${p.st}"></span></td>
					<td><span th:text="${p.number}"></span></td>
					<td><span th:text="${p.name}"></span></td>
					<td>
						
					</td>
			</tbody>

1 个答案:

答案 0 :(得分:1)

在这种情况下,错误很容易解释。 Property or field 'X' cannot be found on null表示百里香试图在空对象上调用getter方法。在您的情况下,p.iden.ref表示iden为空。

可能的解决方案是:

  • 确保控制器中的iden属性在任何目标中都不为空。
  • 通过以下方式检查您是否不会尝试访问IDen如果为空:

    <span th:if="${p.iden != null}" th:text="${p.iden.ref}" ></span>