在html(Thymelaaf)后面的“
Servlet.service()为servlet[调度Servlet]在上下文路径[]抛出异常[请求处理失败;嵌套异常org.thymeleaf.exceptions.TemplateInputException:一个错误发生在模板解析(模板:"类路径资源[模板/ad-edit.html]")]
控制器:
@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";
}
实体和dto:
@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>
ad-edit html文件的第14行出错,
<span th:text="${ad.name}"></span>
我正在尝试从一个Boostrap模式中的表单发布。 这是我的表格: 致: 给我“bean名称'university'的BindingResult或plain target object都不作为请求属性可用”错误。
> POJO摘要 对方法预授权筛选器。 在传递完全填充的对象(存在正确的用户和组组合)时,安全筛选器引发以下异常: 原因是: 请提供地址相同的指针。
类项目: hbm文件: 方法如下:
问题内容: 我有一个Spring / Thymeleaf应用程序 但是,页面看起来正常。所有变量都与数据一起呈现。我只是担心每个请求都会引发异常。 这是控制器: 有问题的html的代码段: 该字段是InstallLinkJoined对象的一部分: 我在所有领域都有吸气剂和吸气剂。 如果我将违规行注释掉,则仅在下一个变量处出现异常。 而且,如前所述,页面中的所有数据都显示出来了,因此显然模型对象不是
我得到的。 这是我的密码: 我得到错误作为 2014年10月16日下午4:31:47 严重:Servlet。路径为[/CustomerPortal]的上下文中servlet[dispatcherServlet]的服务()引发了异常[Request processing failed;嵌套异常为java.lang.NumberFormatException:null],其根本原因为 当我运行这个项目