我有这个html / thymleaf模板,一直在使用th:if语句。如果SQL数据库中的该字段中有内容,则代码使用if语句仅显示div及其内容。
我试图添加一个新的私有字符串cyberAttackInsurance并使用相同的代码来显示它,但它抛出此错误:方法调用:尝试在空上下文对象上调用方法isEmpty()
为什么这是这段代码而不是其余代码的问题?我尝试使用th:block和th:switch th:case来编写不同的代码,但是它也不喜欢...引发了另一个错误。我们将不胜感激任何解决该错误的想法,或者对导致这种错误的原因有更深入的了解!
这是添加了cyberAttackInsurance的代码。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="fragments/layout" th:with="currentPage='dashboard'">
<head>
<title>Home</title>
</head>
<body>
<div class="container" layout:fragment="content">
<div class="row">
<br/>
<div class="ui segment">
<div class="ui container">
<div th:if="${param.sentMessageFail}">
<div class="ui warning message">
<p style="color: #ffffff; text-align:left;"> Oops! Something went wrong. Please try submitting your form again.</p>
</div>
</div>
</div>
<div th:if="${param.sentMessage}">
<div class="ui positive message">
<p style="color: #ffffff; text-align:left;"> Sent Successfully!</p>
</div>
</div>
</div>
<div class="col m6 xl3 valign-wrapper">
<div th:if="${access!=null and !access.micrositeLink.isEmpty()}" class="card xs scale-transition scale-out z-depth-0">
<a th:href="${access.micrositeLink}" target="_blank">
<div class="card-image">
<img src="images/ReferralMicrosite.jpg"/>
</div>
</a>
<div class="card-content">
<p>Submit a new opportunity</p>
</div>
</div>
</div>
<div class="col m6 xl3 valign-wrapper">
<div th:if="${access!=null and !access.partnerPortalLink.isEmpty()}" class="card xs scale-transition scale-out z-depth-0">
<a th:href="${access.partnerPortalLink}" target="_blank" style="background-color:#2f373e;color:white;">
<div class="card-image">
<img src="images/ProspectManager.png"/>
</div>
</a>
<div class="card-content">
<p>Track all your referral business</p>
</div>
</div>
</div>
<div class="col m6 xl3 valign-wrapper">
<div th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')} or ${loggedUser.client.ams360Affiliate == 'All Access – Traditional'}" class="card xs scale-transition scale-out z-depth-0">
<a href="https://platform.vertafore.com/#/" target="_blank">
<div class="card-image">
<img src="images/ams360.png"/></div>
</a>
<div class="card-content">
<p>Agency Management System</p>
</div>
</div>
</div>
<div class="col m6 xl3 valign-wrapper">
<div th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')} or ${loggedUser.client.ams360Affiliate == 'All Access – Traditional'}" class="card xs scale-transition scale-out z-depth-0">
<a href="https://rating.vertafore.com/UserInterface/main/login.aspx" target="_blank">
<div class="card-image">
<img src="images/plRating.png"/>
</div>
</a>
<div class="card-content">
<p>Personal Lines Comparative Rater & Quoting</p>
</div>
</div>
</div>
</div>
<div class="row flex" style="margin-bottom:0;">
<div class="col s12 xl6">
<div class="row" style="margin-bottom:-5px;">
<div class="col s6 m4 scale-transition scale-out">
<a th:href="@{/carriers}">
<div class="white red-text text-accent-2 valign-wrapper ">
<span class="center-align" style="width:100%; padding:10px;">
<img src="/images/menu_icons/icons8-handshake-48.png"/>
<br/>
<span class="grey-text text-darken-2">Know your carriers!</span>
</span>
</div>
</a>
</div>
<div th:if="${access!=null and !access.indioCustomerApplicationSite.isEmpty()}" class="col s6 m4 scale-transition scale-out">
<a th:href="${access.indioCustomerApplicationSite}" target="_blank">
<div class="white text-accent-4 valign-wrapper ">
<span class="center-align" style="width:100%; padding:10px;">
<img class="responsive-img" src="images/Indio.png"/>
<br/>
<span class="grey-text text-darken-2">Online Applications for your customers to complete</span>
</span>
</div>
</a>
</div>
<div class="col s6 m4 scale-transition scale-out">
<a target="_blank" href="https://www.university.com/">
<div class="white valign-wrapper ">
<span class="center-align" style="width:100%; padding:10px;">
<img src="/images/menu_icons/icons8-reading-48.png"/>
<br/>
<span class="grey-text text-darken-2">University</span>
</span>
</div>
</a>
</div>
<div th:if="${access!=null and !access.cyberAttackInsurance.isEmpty()}" class="col s6 m4 scale-transition scale-out">
<a th:href="${access.cyberAttackInsurance}" target="_blank">
<div class="white valign-wrapper ">
<span class="center-align" style="width:100%; padding:10px;">
<img class="responsive-img" src="images/vendor_logos/CAI.jpg"/>
<br/>
<span class="grey-text text-darken-2">Cyber Attack Insurance</span>
</span>
</div>
</a>
</div>
<div th:if="${access!=null and !access.hiscoxQuotingSite.isEmpty()}" class="col s6 m4 scale-transition scale-out">
<a th:href="${access.hiscoxQuotingSite}" target="_blank">
<div class="white black-text text-accent-4 valign-wrapper ">
<span class="center-align" style="width:100%; padding:10px;">
<img class="responsive-img" src="images/vendor_logos/hiscox_logo.gif"/>
<br/>
<span class="grey-text text-darken-2">Online Quoting site for <strong>P&C licensed Affiliates only</strong></span>
</span>
</div>
</a>
</div>
<div th:if="${access!=null and !access.CRQLink.isEmpty()}" class="col s6 m4 scale-transition scale-out">
<a th:href="https://esubmissions.thehartford.com/" target="_blank">
<div class="white black-text valign-wrapper " >
<span class="center-align" style="width:100%; padding:10px;">
<img class="responsive-img" src="images/hartford-logo.png"/>
<br/>
<span class="grey-text text-darken-2">The Hartford Pocket </span>
</span>
</div>
</a>
</div>
<div th:if="${access!=null and !access.hartfordCustomerQuotingSite.isEmpty()}" class="col s6 m4 scale-transition scale-out">
<a th:href="${'mailto:?body=Thank you for your interest in our Commercial Lines Insurance, here is a link to get a quote online NOW! '+access.hartfordCustomerQuotingSite+'&subject=Thank you for your interest!'}" >
<div class="white black-text valign-wrapper" >
<span class="center-align" style="width:100%; padding:10px;">
<i class="material-icons center-align" style="font-size:3.25rem">mail_outline</i>
<br/>
<span class="grey-text text-darken-2">Email: Link to Hartford Customer Facing Rater </span>
</span>
</div>
</a>
</div>
</div>
</div>
<div class="col s12 xl6">
<div class="white scale-transition scale-out" style="padding:5px 10px">
<h6><Strong>Latest Announcements</Strong></h6>
<table class="table striped" data-toggle="table" data-classes="table-no-bordered" data-striped="true" data-pagination="true" data-page-size="3">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="announcement : ${announcements}">
<td>
<a th:href="@{|/announcement/${announcement.id}|}">
<h6><strong th:text="${announcement.heading}">Announcement Heading</strong></h6>
<small class="designation" th:text="${announcement.description}">Description for this announcement.</small></a>
</td>
<td class="center-align">
<h6><strong th:if="${announcement.createdDate}" th:text="${#dates.day(announcement.createdDate)}">28</strong></h6>
<h6><strong th:if="${announcement.createdDate}" th:text="${#dates.monthName(announcement.createdDate)}">Feb</strong></h6>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="row">
<div class="col s12">
<div class="white scale-transition scale-out " style="padding:5px 10px">
<h6 style="margin-bottom:-40px;"><strong>Resources Directory</strong></h6>
<table class="table striped highlight" data-toggle="table" data-classes="table-no-bordered" data-striped="true" data-search="true" data-pagination="true" data-page-size="4">
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Services</th>
</tr>
</thead>
<tbody>
<tr th:each="resource : ${resources}">
<td><a th:href="@{/}" th:text="${resource.name}"></a></td>
<td th:text="${resource.category}"></td>
<td th:text="${resource.services}"></td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
$(document).ready(function(){
$('.scale-out').addClass("scale-in");
});
</script>
</div>
</div>
</body>
</html>
这是模型:
@Entity
public class ResourceWebsiteAccess {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ResourceWebsiteAccessId")
private Long id;
private String micrositeLink;
private String partnerPortalLink;
private String hartfordCustomerQuotingSite;
private String hiscoxQuotingSite;
private String indioCustomerApplicationSite;
private String CRQLink;
private String cyberAttackInsurance;
@OneToOne
@JoinColumn(name = "client")
private Client client;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMicrositeLink() {
return micrositeLink;
}
public void setMicrositeLink(String micrositeLink) {
this.micrositeLink = micrositeLink;
}
public String getPartnerPortalLink() {
return partnerPortalLink;
}
public void setPartnerPortalLink(String partnerPortalLink) {
this.partnerPortalLink = partnerPortalLink;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public String getHartfordCustomerQuotingSite() {
return hartfordCustomerQuotingSite;
}
public void setHartfordCustomerQuotingSite(String hartfordCustomerQuotingSite) {
this.hartfordCustomerQuotingSite = hartfordCustomerQuotingSite;
}
public String getHiscoxQuotingSite() {
return hiscoxQuotingSite;
}
public void setHiscoxQuotingSite(String hiscoxQuotingSite) {
this.hiscoxQuotingSite = hiscoxQuotingSite;
}
public String getIndioCustomerApplicationSite() {
return indioCustomerApplicationSite;
}
public void setIndioCustomerApplicationSite(String indioCustomerApplicationSite) {
this.indioCustomerApplicationSite = indioCustomerApplicationSite;
}
public String getCRQLink() {
return CRQLink;
}
public void setCRQLink(String CRQLink) {
this.CRQLink = CRQLink;
}
public String getCyberAttackInsurance() {
return cyberAttackInsurance;
}
public void setCyberAttackInsurance(String cyberAttackInsurance) {
this.cyberAttackInsurance = cyberAttackInsurance;
}
}
答案 0 :(得分:2)
首先像检查cyberAttackInsurance
一样检查access
是否为空
<div th:if="${access!=null and access.cyberAttackInsurance != null and !access.cyberAttackInsurance.isEmpty()}" class="col s6 m4 scale-transition scale-out">
更好的是,您可以简单地使用条件运算符
<div th:if="${access?.cyberAttackInsurance}" class="col...
答案 1 :(得分:0)
您忘记设置cyberAttackInsurance
吗?该错误是因为您正在调用isEmpty()
,但它是null
,因此为什么在这里看到空指针。