简体   繁体   中英

Java Spring problem with send data from the form

For the "Problem" class, I want to add a "Priorytet" class field. "Problem" has its own fields: id, title(tytul), description(opis) etc. and also has a field related to the "Priorytet" class, which is: id, name(nazwa), time(maksymalnyCzas). What's the problem? When I submit the form, the "priorytet" field in the "Problem" class goes blank(It only contains the id of the "Priority" class item).

Priorytet class

public class Priorytet {
    private long id;
    private String nazwa;
    private long maksymalnyCzas;

    public Priorytet(long id, String nazwa, long maksymalnyCzas) {
        this.id = id;
        this.nazwa = nazwa;
        this.maksymalnyCzas = maksymalnyCzas;
    }

    public Priorytet() {

    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getNazwa() {
        return nazwa;
    }

    public void setNazwa(String nazwa) {
        this.nazwa = nazwa;
    }

    public long getMaksymalnyCzas() {
        return maksymalnyCzas;
    }

    public void setMaksymalnyCzas(long maksymalnyCzas) {
        this.maksymalnyCzas = maksymalnyCzas;
    }

}

Problem class

public class Problem {
    private long id;
    private String tytul;
    private String opis;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate dataZgloszenia;
    private float kosztRozwiazania;
    private boolean rozwiazany;
    private Priorytet priorytet;

    public Problem(long id, String tytul, String opis, LocalDate dataZgloszenia, float kosztRozwiazania, boolean rozwiazany, Priorytet priorytet) {
        this.id = id;
        this.tytul = tytul;
        this.opis = opis;
        this.dataZgloszenia = dataZgloszenia;
        this.kosztRozwiazania = kosztRozwiazania;
        this.rozwiazany = rozwiazany;
        this.priorytet = priorytet;
    }

    public Problem() {

    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTytul() {
        return tytul;
    }

    public void setTytul(String tytul) {
        this.tytul = tytul;
    }

    public String getOpis() {
        return opis;
    }

    public void setOpis(String opis) {
        this.opis = opis;
    }

    public LocalDate getDataZgloszenia() {
        return dataZgloszenia;
    }

    public void setDataZgloszenia(LocalDate dataZgloszenia) {
        this.dataZgloszenia = dataZgloszenia;
    }

    public Float getKosztRozwiazania() {
        return kosztRozwiazania;
    }

    public void setKosztRozwiazania(float kosztRozwiazania) {
        this.kosztRozwiazania = kosztRozwiazania;
    }

    public Boolean getRozwiazany() {
        return rozwiazany;
    }

    public void setRozwiazany(boolean rozwiazany) {
        this.rozwiazany = rozwiazany;
    }

    public Priorytet getPriorytet() {
        return priorytet;
    }

    public void setPriorytet(Priorytet priorytet) {
        this.priorytet = priorytet;
    }
}

Controller

@Controller
public class kontroler {
    private static List<Problem> lista = new ArrayList<>();
    private static List<Priorytet> priorytetList = new ArrayList<>();

    @GetMapping(value = "/wypelnij")
    public String wypelnij(Model model){
        Priorytet priorytet = new Priorytet(0,"Najniższy",14);
        priorytetList.add(priorytet);
        priorytet = new Priorytet(1,"Średni",7);
        priorytetList.add(priorytet);
        priorytet = new Priorytet(2,"Najwyższy",2);
        priorytetList.add(priorytet);

        LocalDate date1 = LocalDate.of(2017, 1, 13);
        LocalDate date2 = LocalDate.of(2021, 10, 8);
        var a = new Problem(0,"Przykladowy problem numer 1", "Przykładowy opis problemu #1", date1, 155.3f, true, priorytetList.get(1));
        var b = new Problem(1,"Przykladowy problem numer 2", "Przykładowy opis problemu #2", date2, 125.5f, false, priorytetList.get(2));

        lista.add(a);
        lista.add(b);

        model.addAttribute("listaProblemow",lista);
        return "wyswietlLista";
    }

    @GetMapping(value = "/problem")
    public String dodaj(Model model){
        LocalDate date1 = LocalDate.of(2017, 1, 13);
        var b = new Problem(1,"Przykladowy problem", "Przykładowy opis problemu", date1, 125.5f, false, priorytetList.get(0));

        model.addAttribute("problem",b);
        return "wyswietl";
    }

    @GetMapping(value = "/listaProblemow")
    public String dodajLista(Model model){
        model.addAttribute("listaProblemow",lista);
        return "wyswietlLista";
    }

    @GetMapping(value = {"/add", "/edit/{problemId}"})
    private String showForm(@PathVariable(name="problemId", required=false) Integer id, Model model){
        if (id != null) {
            model.addAttribute("problem", lista.get(id));
        }
        else{
            model.addAttribute("problem", new Problem(0,"","",null,0f,false, priorytetList.get(0)));
        }
        model.addAttribute("priorytet", priorytetList);

        return "formularz";
    }

    @PostMapping(value = {"/add", "/edit/{problemId}"})
    private String processForm(@ModelAttribute(name = "problem") Problem problem, Model model, @PathVariable(name="problemId", required=false) Integer id){
        if(id != null){
            lista.set((int) problem.getId(),problem);
        } else {
            problem.setId(lista.size());
            lista.add(problem);
        }

        model.addAttribute("problem", problem);
        return "successView";
    }
}

formularz.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form method="POST" th:object="${problem}">
    <input type="hidden" th:field="*{id}" th:value="*{id}">

    Priorytet:
    <select th:field="*{priorytet.id}">
        <option th:each="p : ${priorytet}" th:value="${p.id}" th:text="${p.nazwa}" th:selected="${p.id == problem.priorytet.id}"></option>
    </select><br>

    Tytuł: <input type="text" th:field="*{tytul}" th:value="*{tytul}"><br>
    Opis: <input type="text" th:field="*{opis}" th:value="*{opis}"><br>
    Data: <input type="date" th:field="*{dataZgloszenia}" th:value="*{dataZgloszenia}"><br>
    Koszt rozwiązania: <input type="number" th:field="*{kosztRozwiazania}" th:value="*{kosztRozwiazania}"><br>
    Rozwiązany:
    <select th:field="*{rozwiazany}">
        <option value="true" th:selected="*{rozwiazany == true}">Tak</option>
        <option value="false" th:selected="*{rozwiazany == false}">Nie</option>
    </select><br>
    <input type="submit" value="Wyślij">
</form>

</body>
</html>

I think you are referencing same variable to all three object here try changing

 priorytetList.add(priorytet);
    priorytet = new Priorytet(1,"Średni",7);
    priorytetList.add(priorytet);
    priorytet = new Priorytet(2,"Najwyższy",2);
    priorytetList.add(priorytet);   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM