if (bindingResult.hasErrors()) {
return "detailed_news_template";
}
@RequestMapping(value = "/user/addComment", method = RequestMethod.POST)
public String addComment(HttpServletRequest req, @ModelAttribute("Comment") @Valid Comment comment, BindingResult bindingResult, RedirectAttributes redirectAttributes) throws IOException {
if (bindingResult.hasErrors()) {
redirectAttributes.addFlashAttribute("errors", bindingResult.getAllErrors());
return "redirect:/user/detailedNews";
}
...
}
Map<String, Integer> map = list.stream()
.collect(Collectors.toMap(Animal::getType,
Animal::getCount,
(a, b) -> a + b));
Map<String, Integer> map = list.stream()
.collect(Collectors.groupingBy(Animal::getType,
Collectors.summingInt(Animal::getCount)));
List<Animal> aggregated
= list.stream()
.collect(Collector.of(HashMap<String, Integer>::new,
(acc, val) -> acc.compute(val.getType(), (k, v) -> v == null ? val.getCount() : v + val.getCount()),
(l, r) -> {
l.replaceAll((k, v) -> v + r.getOrDefault(k, 0));
return l;
},
acc -> acc.entrySet()
.stream()
.map(e -> new Animal(e.getKey(), e.getValue()))
.collect(Collectors.toList())));
def fetch_pdf_resources(uri, rel):
if uri.find(settings.MEDIA_URL) != -1:
path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ''))
elif uri.find(settings.STATIC_URL) != -1:
path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, ''))
else:
path = None
return path
pdf = pisa.pisaDocument(BytesIO(template.encode('UTF-8')), result,
encoding='utf-8',
link_callback=fetch_pdf_resources)
class Competition(models.Model):
name = models.CharField(max_length=30)
teams = models.ManyToManyField(Team)