package com.newconstructs.controller.home;
import com.newconstructs.domain.Animal;
import com.newconstructs.domain.api.Layout;
import com.newconstructs.service.api.AnimalService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.inject.Inject;
import java.util.Collections;
import java.util.List;
@Controller
public class HomeController {
@Inject
private AnimalService animalService;
@Layout(
title = "",
description = "",
selectedNav = "all"
)
@RequestMapping(value = {"/", "/home"})
public String init(ModelMap modelMap) {
List<Animal> animals = animalService.findAll();
modelMap.put("animals", animals);
return "home";
}
}
package com.newconstructs.service;
import com.newconstructs.domain.Animal;
import com.newconstructs.domain.Animal.AnimalType;
import com.newconstructs.service.api.AnimalService;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class AnimalServiceImpl implements AnimalService {
private static final List<Animal> ANIMALS;
static {
ANIMALS = new ArrayList<>();
ANIMALS.add(new Animal(AnimalType.DOG, "Spot", 10, 7, 17, 0));
ANIMALS.add(new Animal(AnimalType.DOG, "Rover", 15, 5, 2, 6));
ANIMALS.add(new Animal(AnimalType.DOG, "Fido", 8, 6, 12, 15));
ANIMALS.add(new Animal(AnimalType.CAT, "Mittens", 18, 3, 3, 0));
ANIMALS.add(new Animal(AnimalType.CAT, "Snowball", 5, 0, 14, 1));
ANIMALS.add(new Animal(AnimalType.CAT, "Waffles", 8, 11, 2, 18));
ANIMALS.add(new Animal(AnimalType.SNAKE, "Slider", 6, 16, 14, 3));
ANIMALS.add(new Animal(AnimalType.SNAKE, "Milton", 16, 6, 11, 17));
ANIMALS.add(new Animal(AnimalType.SNAKE, "Spike", 6, 18, 2, 5));
ANIMALS.add(new Animal(AnimalType.SNAKE, "Alice", 9, 11, 4, 6));
ANIMALS.add(new Animal(AnimalType.RABBIT, "Flopsy", 14, 0, 12, 2));
ANIMALS.add(new Animal(AnimalType.RABBIT, "Peter", 18, 18, 14, 3));
ANIMALS.add(new Animal(AnimalType.RABBIT, "Oreo", 7, 4, 9, 1));
}
@Override
public List<Animal> findAll() {
return ANIMALS;
}
}
package com.newconstructs.service.api;
import com.newconstructs.domain.Animal;
import org.springframework.validation.annotation.Validated;
import java.util.List;
import java.util.Map;
@Validated
public interface AnimalService {
public List<Animal> findAll();
}
a {
text-decoration: none;
color: black;
}
#content, #header, #footer {
width: 600px;
}
.menu {
display: flex;
}
.menu > a {
border-bottom: 1px solid grey;
border-right: 1px solid grey;
text-align: center;
flex-grow: 1;
padding: 10px;
}
.menu > a.selected {
background-color: lightgray;
}
.menu > a:last-child {
border-right: 0;
}
.menu > a:focus, .menu > a:hover {
background-color: #e5e5e5;
}
h2.header {
padding-bottom: 0;
margin-bottom: 15px;
border-bottom: 1px solid grey;
}
.animal-list {
width: 400px;
}
.animal {
border-bottom: 1px solid grey;
padding: 4px 5px;
}
.name {
font-size: 1rem;
}
.characteristic {
text-align: right;
}
.type {
font-size: 0.8rem;
float: right;
}
.rating-table, .rating-table th, .rating-table td, .stats-table, .stats-table th, .stats-table td {
border: 1px solid grey;
border-collapse: collapse;
width: 4.0rem;
padding: 4px;
}
<div id="content">
<h2 class="header">All Animals</h2>
<div class="animal-list">
<div class="animal" th:each="animal : ${animals}">
<span class="name" th:text="${animal.name}">Name</span>
<span class="type" th:text="${animal.animalType}">Animal Type</span>
<span class="offense" th:text="${animal.offense}">Offense</span>
<span class="defense" th:text="${animal.defense}">Defense</span>
<span class="health" th:text="${animal.health}">Health</span>
<span class="items" th:text="${animal.items}">Items</span>
</div>
</div>
</div>
我正在尝试仅从Java数组中获取特定的枚举类型。如果下面的代码显示动物列表中的所有动物。如何更改它只显示枚举类型DOG。我创建了一个Plunker来显示我的所有代码。 https://embed.plnkr.co/exdZ7Uw2jraxpac8Zrlb/
基本上,我需要改变下面的代码才能得到狗,猫等等。
<div class="animal" th:each="animal : ${animals}">
答案 0 :(得分:1)
这可以通过多种方式完成,但我会在服务中添加一个方法,称之为getAllByAnimalType(AnimalType type)
,并让自己灵活地让视图(html)显示你抛出的任何动物它虽然没有传递不需要的数据。
由于数据在视图模型中加载,您将提供一种通过URL了解用户需求的方法。也许:
@RequestMapping(value = {"/dogs"})
public String dogs(ModelMap modelMap) {
List<Animal> animals = getAnimals(AnimalType.DOG);
modelMap.put("animals", animals);
return "home";
}
...
private List<Animal> getAnimals(AnimalType type) {
return animalService.findAllByAnimalType(type);
}
答案 1 :(得分:0)
使用th:if仅显示枚举类型DOG。
<div id="content">
<h2 class="header">All Animals</h2>
<div class="animal-list">
<div class="animal" th:each="animal : ${animals}">
<th:block th:if="${#strings.toString(animal.animalType)== 'DOG'}">
<span class="name" th:text="${animal.name}">Name</span>
<span class="type" th:text="${animal.animalType}">Animal Type</span>
<span class="offense" th:text="${animal.offense}">Offense</span>
<span class="defense" th:text="${animal.defense}">Defense</span>
<span class="health" th:text="${animal.health}">Health</span>
<span class="items" th:text="${animal.items}">Items</span>
</th:block>
</div>
</div>
</div>