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数组中获取特定的枚举类型。如果下面的代码向我显示了动物列表中的所有动物。我怎么能改变它只显示枚举类型的狗。我创建了一个Plunker来显示我的所有代码。 https://embed.plnkr.co/exdZ7Uw2jraxpac8Zrlb/

基本上,我需要更改下面的代码以仅获取狗,猫等。

<div class="animal" th:each="animal : ${animals}">

最佳答案

这可以通过多种方式完成,但是我会向服务中添加一个方法,将其称为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);
}

关于java - thymeleaf 选择特定的枚举类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40621855/

10-08 23:48