本文介绍了百里香麻烦.请求处理失败.EL1007E: 在 null 上找不到属性或字段“名称"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在后面的 html(Thymeleaf) 中,Servlet.service() for servlet [dispatcherServlet] in context with path [] 抛出异常 [Request processing failed;嵌套异常是 org.thymeleaf.exceptions.TemplateInputException:模板解析过程中发生错误(模板:类路径资源 [模板/ad-edit.html]")]
控制器:
@GetMapping("/ads/{id}/edit")public String adEdit(@PathVariable(value = "id") int id, Model model) {列表<平台>平台 = platformRepository.findAll();列表<PlatformDTO>platformDTOList =platforms.stream().map(platform ->PlatformDTO.toDTO(platform)).collect(Collectors.toList());广告广告 = adRepository.findById(id).orElseThrow(() ->新的 ResponseStatusException(HttpStatus.NOT_FOUND,具有 id 的广告"+ id + "未找到"));AdDTO adDto = 新的 AdDTO(ad);model.addAttribute(平台",platformDTOList);model.addAttribute(adDto", adDto);返回广告编辑";}@PostMapping("/ads/{id}/edit")public String adUpdate(@PathVariable(value = "id") int id,@ModelAttribute("adDto") AdDTO adDto,模型模型){广告广告 = adRepository.findById(id).orElseThrow(() ->新的 ResponseStatusException(HttpStatus.NOT_FOUND,具有 id 的广告"+ id + "未找到"));ad.setPlatforms((platformRepository.findAllById(adDto.platformsIds)));ad.setName(adDto.name);ad.setAssetUrl(adDto.assetUrl);adRepository.save(ad);返回重定向:/广告";}
实体和 DTO:
@Table(name = "ads")@EqualsAndHashCode(of = {name"})公共课广告{@ID@GeneratedValue(策略 = GenerationType.IDENTITY)私有整数 ID;私人字符串名称;私人状态;私人字符串资产网址;@ManyToOne(可选 = false,级联 = CascadeType.ALL)@JoinColumn(name = "campaign_id")私人竞选活动;@ManyToMany@JoinTable(name = "ads_platforms", joinColumns = @JoinColumn(name = "ad_id"),inverseJoinColumns = @JoinColumn(name = "platform_id", nullable = true))@Column(名称=平台")私人列表<平台>平台;公共 AdDTO(广告广告){this.id = ad.getId();this.name = ad.getName();this.assetUrl = ad.getAssetUrl();this.status = ad.getStatus();this.campaign = ad.getCampaign();this.platformsIds = adPlatformIds(ad.getPlatforms());}列表<整数>adPlatformIds(List平台){列表<整数>platformIdsList =platforms.stream().map(platform -> platform.getId()).collect(Collectors.toList());返回platformIdsList;}}@实体@Table(name = 平台")@EqualsAndHashCode(of = {name"})@ToString(of = {"name"})公共类平台{@ID@GeneratedValue(策略 = GenerationType.IDENTITY)@Column(name = "id", nullable = true)私有整数 ID;私人字符串名称;@ManyToMany@JoinTable(name = "ads_platforms", joinColumns = @JoinColumn(name = "platform_id", nullable = true),inverseJoinColumns = @JoinColumn(name = "ad_id"))@Column(名称=广告")私人列表<广告>广告;公共类 PlatformDTO {公共 PlatformDTO() {}公共整数 ID;公共字符串名称;公共列表<整数>广告 ID;public static PlatformDTO toDTO(平台平台){如果(平台==空)返回空;PlatformDTO platformDTO = new PlatformDTO();platformDTO.id = platform.getId();platformDTO.name = platform.getName();platformDTO.adsIds = platformAdsIds(platform.getAds());返回平台DTO;}静态列表<整数>platformAdsIds(List ads){列表<整数>adIdsList = ads.stream().map(ad -> ad.getId()).collect(Collectors.toList());返回 adIdsList;}
ad-edit.html:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet"完整性="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="匿名"><link rel="stylesheet" href="/css/style.css"/><头><meta charset="UTF-8"><title>Редактирование обьявления</title>头部><身体><标题><nav class="navbar navbar-light bg-light"><form class="container justify-content-start"><h3 class="caption">Редактирование обьявления <span th:text="${ad.name}"></span></h3><button class="navbar-toggler" type="button"><a href="../home.html" th:href="@{/home}">На главную</a></button><button type="button" class="btn btn-light"><a href="../info.html" th:href="@{/info}">Обе SQL Таблицы</a></button><button type="button" class="btn btn-light"><a href="../ads.html" th:href="@{/ads}">Обьявления</a></button><button type="button" class="btn btn-light"><a class="link" href="../campaigns.html" th:href="@{/campaigns}">Кампании</a></button></表单></nav></标题><form th:object="${adDto}" th:action="@{/ads/{id}/edit}" method="post"><input type="text" th:value="${adDto.name}" th:field="*{adDto.name}"placeholder="Введите название обьявления" class="form-control"><input type="text" th:value="${adDto.assetUrl}" th:field="*{adDto.assetUrl}"placeholder="Введите ссылку на рекламу" class="form-control"><select class="form-control" th:field="*{adDto.platformsIds}" multiple="multiple" ><div><option th:each="platform : ${platforms}"th:value="${*platform.id*}"th:selected="${adDto.platformsIds.contains(*platform.id*)}"th:text="${*platform.name*}"></option></div></选择>
<button type="submit" class="btn btn-success">Редактировать</button></表单></html>
在此处输入图片描述 在后面的 html(Thymeleaf) 中, 解决方案
ad-edit html 文件第 14 行出错,
<span th:text="${ad.name}"></span>
我们需要替换ad.name"到adDto.name",因为对象ad"不是模型固有的.
In the latter of html(Thymeleaf) below "<option th:each="platform : ${platforms}" Intelij Idea emphasizes red "platform.id" and "platform.name"
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/ad-edit.html]")]
Controller:
@GetMapping("/ads/{id}/edit")
public String adEdit(@PathVariable(value = "id") int id, Model model) {
List<Platform> platforms = platformRepository.findAll();
List<PlatformDTO> platformDTOList = platforms.stream().map(platform ->
PlatformDTO.toDTO(platform)).collect(Collectors.toList());
Ad ad = adRepository.findById(id).orElseThrow(() ->
new ResponseStatusException(HttpStatus.NOT_FOUND,
"Ad having id " + id + " not found"));
AdDTO adDto = new AdDTO(ad);
model.addAttribute("platforms", platformDTOList);
model.addAttribute("adDto", adDto);
return "ad-edit";
}
@PostMapping("/ads/{id}/edit")
public String adUpdate(@PathVariable(value = "id") int id,
@ModelAttribute("adDto") AdDTO adDto,
Model model) {
Ad ad = adRepository.findById(id).orElseThrow(() ->
new ResponseStatusException(HttpStatus.NOT_FOUND,
"Ad having id " + id + " not found"));
ad.setPlatforms((platformRepository.findAllById(adDto.platformsIds)));
ad.setName(adDto.name);
ad.setAssetUrl(adDto.assetUrl);
adRepository.save(ad);
return "redirect:/ads";
}
Entities and DTOs:
@Table(name = "ads")
@EqualsAndHashCode(of = {"name"})
public class Ad {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private Status status;
private String assetUrl;
@ManyToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "campaign_id")
private Campaign campaign;
@ManyToMany
@JoinTable(name = "ads_platforms", joinColumns = @JoinColumn(name = "ad_id"),
inverseJoinColumns = @JoinColumn(name = "platform_id", nullable = true))
@Column(name = "platform")
private List<Platform> platforms;
public AdDTO(Ad ad) {
this.id = ad.getId();
this.name = ad.getName();
this.assetUrl = ad.getAssetUrl();
this.status = ad.getStatus();
this.campaign = ad.getCampaign();
this.platformsIds = adPlatformIds(ad.getPlatforms());
}
List<Integer> adPlatformIds(List<Platform> platforms){
List<Integer> platformIdsList = platforms.stream().map(platform -> platform.getId()).collect(Collectors.toList());
return platformIdsList;
}
}
@Entity
@Table(name = "platforms")
@EqualsAndHashCode(of = {"name"})
@ToString(of = {"name"})
public class Platform {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = true)
private Integer id;
private String name;
@ManyToMany
@JoinTable(name = "ads_platforms", joinColumns = @JoinColumn(name = "platform_id", nullable = true),
inverseJoinColumns = @JoinColumn(name = "ad_id"))
@Column(name = "ad")
private List<Ad> ads;
public class PlatformDTO {
public PlatformDTO() {
}
public Integer id;
public String name;
public List<Integer> adsIds;
public static PlatformDTO toDTO(Platform platform) {
if (platform == null)
return null;
PlatformDTO platformDTO = new PlatformDTO();
platformDTO.id = platform.getId();
platformDTO.name = platform.getName();
platformDTO.adsIds = platformAdsIds(platform.getAds());
return platformDTO;
}
static List<Integer> platformAdsIds(List<Ad> ads){
List<Integer> adIdsList = ads.stream().map(ad -> ad.getId()).collect(Collectors.toList());
return adIdsList;
}
ad-edit.html:
<!DOCTYPE html>
<html>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<link rel="stylesheet" href="/css/style.css"/>
<head>
<meta charset="UTF-8">
<title>Редактирование обьявления</title>
</head>
<body>
<header>
<nav class="navbar navbar-light bg-light">
<form class="container justify-content-start">
<h3 class="caption">Редактирование обьявления <span th:text="${ad.name}"></span></h3>
<button class="navbar-toggler" type="button">
<a href="../home.html" th:href="@{/home}">На главную</a></button>
<button type="button" class="btn btn-light">
<a href="../info.html" th:href="@{/info}">Обе SQL Таблицы</a></button>
<button type="button" class="btn btn-light">
<a href="../ads.html" th:href="@{/ads}">Обьявления</a></button>
<button type="button" class="btn btn-light">
<a class="link" href="../campaigns.html" th:href="@{/campaigns}">Кампании</a></button>
</form>
</nav>
</header>
<form th:object="${adDto}" th:action="@{/ads/{id}/edit}" method="post">
<input type="text" th:value="${adDto.name}" th:field="*{adDto.name}"
placeholder="Введите название обьявления" class="form-control">
<input type="text" th:value="${adDto.assetUrl}" th:field="*{adDto.assetUrl}"
placeholder="Введите ссылку на рекламу" class="form-control">
<select class="form-control" th:field="*{adDto.platformsIds}" multiple="multiple" >
<div><option th:each="platform : ${platforms}"
th:value="${*platform.id*}"
th:selected="${adDto.platformsIds.contains(*platform.id*)}"
th:text="${*platform.name*}">
</option></div>
</select>
</div>
<button type="submit" class="btn btn-success">Редактировать</button>
</form>
</body>
</html>
enter image description here
In the latter of html(Thymeleaf) below "<option th:each="platform : ${platforms}" Intelij Idea emphasizes red "platform.id" and "platform.name"
解决方案
Error in line 14 of ad-edit html file,
<span th:text="${ad.name}"></span>
we need replace "ad.name" to "adDto.name", because object "ad" not inherent in the model.
这篇关于百里香麻烦.请求处理失败.EL1007E: 在 null 上找不到属性或字段“名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!