如何使用FreeMarker显示实体?

时间:2018-10-02 00:46:19

标签: html spring-boot freemarker

我有实体

@Entity
@Table(name="fights")
public class Fight {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long fightid;

@OneToMany(mappedBy = "fight", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Sportsman> sportsmans;

private Long identifWin;

private Long identifEvent;
/* getters and setters */
}

和控制器

@GetMapping("/fights")
public String getAllFights(Model model) {
    List fights = fightServise.getAllFight();
    model.addAttribute("fights", fights);
    return "fights";
}

我不知道如何使用FreeMarker显示该实体。 此代码显示了一些难以理解的内容:

<form method="get" action="/fights">
<#list fights as fight>
    ${fight}
<#else>
    No fight
</#list>

如果我访问各个字段,则会出现异常。

  

reeMarker模板错误(调试模式;在生产中使用RETHROW!):以下内容评估为空或丢失:==> fight.fightid [在模板“ fights.ftl”的第3行,第11列]

值存在于数据库中
更新
第二个实体:

@Entity
@Table(name = "sportsmans")
public class Sportsman {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String firstName;

private String lastName;

private String alias;

private int growth;

private int weight;

private int lengthOfHands;

private int countOfLose;

private int countOfWin;

private int countOfDraw;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="fightid")
private Fight fight;
/* getters and setters */
}

1 个答案:

答案 0 :(得分:0)

<form method="get" action="/fights">
<#list fights as fight>
    <#list fight.getSportsmans() as b>
        ${b.getFirstName()}
    </#list>
<#else>
    No fight
</#list>