Java springboot Item mit ItemInstance verbinden

gabrielml618

Neues Mitglied
Hallo. Ich habe ein Java Springboot Projekt und möchte eine Benutzeroberfläche für http programmieren, in der eine Produktstruktur ausgegeben wird.
Dafür nutze ich items und eine arraylist in items,in der ich die ItemInstances speichere.
Allein in Java kann ich das programmieren, und weiß, wie man jetzt einem Item, ein Iteminstances hinzufügt und auch wieder abruft, aber hier in springboot komm ich nicht weiter.

Ich habe im Programm bereits die möglichkeit Items zu erstellen, sie anzurufen und auch wieder zu löschen. Das was mit fehlt ist das mit den Item instances.
Ich hatte gedacht, dass ich im ItemController ein neues @PostMapping schreibe, in dem ich ein ItemInstance erstelle, aber ich weiß nicht genau ,was ich da schreiben soll.
17017815760301403881408197233058.jpg
die items werden per ID im Appstore.java gespeichert und die item instances unter Item.java in der arraylist "Uses"
vielleicht kann mir jemand weiter helfen.


das ist mein item controller

Java:
@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"));
        if (body.get("WeightedWeight") != null && body.get("WeightedWeight").length() > 0)
            item.setWeightedWeight(Integer.parseInt(body.get("WeightedWeight")));
        if (body.get("CalculatedWeight") != null && body.get("CalculatedWeight").length() > 0)
            item.setCalculatedWeight(Integer.parseInt(body.get("CalculatedWeight")));
        if (body.get("EstimatedWeight") != null && body.get("EstimatedWeight").length() > 0)
            item.setEstimatedWeight(Integer.parseInt(body.get("EstimatedWeight")));
    
            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);
                break;
            }
              return "showItem";
 
Zunächst fehlen hier Infos. Man sieht nicht wie AppStore aufgebaut ist, demnach müsste man erraten wie man dem AppStore ein neues ItemInstance hinzufügt. Aber ich nehme an appStore.addNewItemInstance(...); sollte das machen.

Wenn AppStore schon eine Bean ist, warum injectest du die nicht einfach, anstatt so einen komischen Konstrukt zu verwenden?

das verstehe ich auch nicht:
Java:
    @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"));
        if (body.get("WeightedWeight") != null && body.get("WeightedWeight").length() > 0)
            item.setWeightedWeight(Integer.parseInt(body.get("WeightedWeight")));
        if (body.get("CalculatedWeight") != null && body.get("CalculatedWeight").length() > 0)
            item.setCalculatedWeight(Integer.parseInt(body.get("CalculatedWeight")));
        if (body.get("EstimatedWeight") != null && body.get("EstimatedWeight").length() > 0)
            item.setEstimatedWeight(Integer.parseInt(body.get("EstimatedWeight")));
   
            getAppStore().addNewItem(item);
        model.addAttribute(
                "id", item.Id);
        return "itemCreated";
    }
Warum nicht einfach:
Java:
    @PostMapping("/items-gui")
    public String createItem(
        Model model,
        HttpSession session,
        @RequestParam Item body )
    {      
   
        getAppStore().addNewItem(body);
        model.addAttribute("id", body.Id);
        return "itemCreated";
    }
 
Zunächst fehlen hier Infos. Man sieht nicht wie AppStore aufgebaut ist, demnach müsste man erraten wie man dem AppStore ein neues ItemInstance hinzufügt. Aber ich nehme an appStore.addNewItemInstance(...); sollte das machen.

Wenn AppStore schon eine Bean ist, warum injectest du die nicht einfach, anstatt so einen komischen Konstrukt zu verwenden?

das verstehe ich auch nicht:
Java:
    @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"));
        if (body.get("WeightedWeight") != null && body.get("WeightedWeight").length() > 0)
            item.setWeightedWeight(Integer.parseInt(body.get("WeightedWeight")));
        if (body.get("CalculatedWeight") != null && body.get("CalculatedWeight").length() > 0)
            item.setCalculatedWeight(Integer.parseInt(body.get("CalculatedWeight")));
        if (body.get("EstimatedWeight") != null && body.get("EstimatedWeight").length() > 0)
            item.setEstimatedWeight(Integer.parseInt(body.get("EstimatedWeight")));
  
            getAppStore().addNewItem(item);
        model.addAttribute(
                "id", item.Id);
        return "itemCreated";
    }
Warum nicht einfach:
Java:
    @PostMapping("/items-gui")
    public String createItem(
        Model model,
        HttpSession session,
        @RequestParam Item body )
    {     
  
        getAppStore().addNewItem(body);
        model.addAttribute("id", body.Id);
        return "itemCreated";
    }
Ich kann morgen den Code vom AppStore schicken. Der Speichert die items mit jeweils einer Id.

Der Code ist nicht von mir selber, den hat mein Informatik Professor gegeben.
 

Zurück
Oben