hallo. ich möchte eine html erstellen, auf der ich eine produktstruktur erstellen kann. dabei möchte ich erstmal Items erstellen , denen ich dann ItemInstances hinzufüge. diese werden unter Item.java in einer Arraylist namens "Uses" gespeichert. jetzt bin ich soweit, dass ich items erstellen kann und im Appstore mit Hilfe einer Id speichern kann. Ich habe auch was geschrieben, dass ich iteminstances den items hinzufügen kann, aber das artet in html immer in eine endlosschleife aus.
mein Appstore:
der controller, wobei ich das letzte postmapping nicht weiß,ob es richtig ist. der rest müsse passen
und das ist das html für die ausgabe der struktur.
daneben habe ich noch ne item klasse , in der die "Arraylist<iteminstances> Uses" drinnen ist, eine iteminstances klasse mit " String name;" und "Item represents; "
zusätzlich auch noch einige andere templates und einen restcontroller, die aber weniger relevant für das problem sind.
ich weiß nicht genau wie ich jetzt eine verbindung von item zu iteminstance machen soll. ich hab das postmapping im controller geschrieben, aber das artet in eine endlosschleife aus . kann mir wer einen tipp geben?
mein Appstore:
Java:
@ApplicationScope
@Component
public class AppStore {
inginf.SampleStore sampleStore = new inginf.SampleStore();
private java.util.ArrayList<inginf.Item> itemStore
= new java.util.ArrayList<inginf.Item>();
public AppStore() {
itemStore = sampleStore.getItems();
}
public ArrayList<inginf.Item> getItemStore() {
return itemStore;
}
public int getItemCount() {
return itemStore.size();
}
public inginf.Item addNewItem(inginf.Item item) {
if (itemStore.size() == 0)
item.Id = 1;
else
item.Id = itemStore.get(itemStore.size()-1).Id + 1;
itemStore.add(item);
return item;
}
Code:
@Controller
public class ItemController {
@Autowired
private ApplicationContext context;
AppStore _AppStore;
AppStore getAppStore() {
if (_AppStore == null)
_AppStore = context.getBean(AppStore.class);
return _AppStore;
}
@PostMapping("/items-gui")
public String createItem(
Model model,
HttpSession session,
@RequestParam Map<String, String> body )
{
inginf.Item item = new inginf.Item(
body.get("Nomenclature"),
body.get("Description"),
body.get("Material"));
getAppStore().addNewItem(item);
model.addAttribute(
"id", item.Id);
return "itemCreated";
}
@GetMapping("/items-gui")
public String createItemDialog() {
return "itemTemplate";
}
@GetMapping("/items-gui/list")
public String listItems(Model model) {
model.addAttribute(
"items",
getAppStore().getItemStore());
return "listItems";
}
@GetMapping("/items-gui/{id}/delete")
public String deleteItem(@PathVariable int id, Model model) {
model.addAttribute(
"id", id);
for (Item item : getAppStore().getItemStore())
if (item.Id == id) {
getAppStore().getItemStore().remove(item);
break;
}
return "itemDeleted";
}
@GetMapping("/items-gui/{id}/show")
public String showItem(@PathVariable int id, Model model) {
model.addAttribute(
"id", id);
for (Item item : getAppStore().getItemStore())
if (item.Id == id) {
model.addAttribute(
"item", item);
model.addAttribute(
"iteminstances", item.getUses());
break;
}
return "showItem";
}
@PostMapping("/items-gui/{id}/add-iteminstances")
public String addItemInstances(
@PathVariable int id,
Model model,
HttpSession session,
@RequestParam Map<String, String> body )
{
model.addAttribute(
"id", id);
for (Item item : getAppStore().getItemStore())
if (item.getId() == id) {
ItemInstance itemInstance = new ItemInstance(body.get("Name"), item);
item.getUses().add(itemInstance);
}
return "redirect:/items-gui/{id}/show";
}
}
und das ist das html für die ausgabe der struktur.
HTML:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Create Item</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h2>Show Item</h2>
<table>
<tr>
<td>Item Name</td>
<td th:text="${item.nomenclature}">Item Name</td>
</tr>
<tr>
<td>Item Description</td>
<td th:text="${item.description}">Item Description</td>
</tr>
<tr>
<td>Item Material</td>
<td th:text="${item.material}">Item Material</td>
</tr>
</table>
<form th:action="@{/items-gui/{id}/add-iteminstances(id=${item.id})}" method="post">
<label for="Name">Name</label>
<input type="text" id="Name" name="Name" required/>
<button type="submit">Hinzufügen</button>
</form>
<form th:action="@{/items-gui/{id}/add-iteminstances(id=${item.id})}" method="post">
<label for="Name">Name</label>
<input type="text" id="Name" name="Name" required/>
<button type="submit">Hinzufügen</button>
</form>
<!-- Formular zum Löschen einer vorhandenen ItemInstance -->
<form th:action="@{/items-gui/{id}/delete-iteminstances(id=${item.id})}" method="post">
<label for="InstanceId">ItemInstance ID to delete</label>
<input type="text" id="InstanceId" name="InstanceId" required/>
<button type="submit">Löschen</button>
</form>
<h3>Produktstruktur:</h3>
<div class="tree">
<div th:fragment="treeviewItem(item)">
<span th:text="${item.nomenclature}">Item</span>
<ul th:if="${item.getUses().size() > 0}">
<li th:each="child : ${item.getUses()}">
<i><span th:text="${child.name}">ItemInstance</span></i>
<div th:replace="this :: treeviewItem(${child.represents})"></div>
</li>
</ul>
</div>
</div>
</body>
</html>
daneben habe ich noch ne item klasse , in der die "Arraylist<iteminstances> Uses" drinnen ist, eine iteminstances klasse mit " String name;" und "Item represents; "
zusätzlich auch noch einige andere templates und einen restcontroller, die aber weniger relevant für das problem sind.
ich weiß nicht genau wie ich jetzt eine verbindung von item zu iteminstance machen soll. ich hab das postmapping im controller geschrieben, aber das artet in eine endlosschleife aus . kann mir wer einen tipp geben?