温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Spring MVC App, in form:input path= converts given object to string, when I need the object to acces
forms jsp model-view-controller path spring

其他 - Spring MVC App,形式为:当我需要访问对象时,输入路径=将给定对象转换为字符串

发布于 2020-04-15 10:13:19

在我的春天MVC应用程序,我想创建一个小的形式,即更新的一个参数object (Quantity),就是在一张 map,它的值keysobjects (Case)

我收到此错误:

org.springframework.beans.InvalidPropertyException:无效的属性'cases [案例或wymiarach:300x200x100mm我powierzchni 0.22m2,Typ = KUFER,material = SKLEJKA,kolor = BLACK,wypelnienie = FOAM,uchwyty = KASETOWY,iloscUchwytow = 0 ,iloscKol = 0,zamki = MOTYLKOWE,uwagi =,cena =110.0zł]',属于bean类[com.wojto.wmcase.entity.Order]:属性路径“ cases”中的索引无效[Case o wymiarach:300x200x100mm,powierzchni 0.22m2 ,typ = KUFER,material = SKLEJKA,kolor = BLACK,wypelnienie = FOAM,uchwyty = KASETOWY,iloscUchwytow = 0,kola = false,iloscKol = 0,zamki = MOTYLKOWE,uwagi =,cena =110.0zł]'; 嵌套异常是org.springframework.beans.ConversionNotSupportedException:无法将类型“ java.lang.String”的属性值转换为属性“ null”的必需类型“ com.wojto.wmcase.entity.Case”;

代码片段如下所示:

<form:form action="updateQuantity" modelAttribute="order" method="PUT">
   <form:input class="" path="cases[${tempCase}].quantity" 
        value="${order.getCases().get(tempCase).getQuantity()}"/>
                                |
        <button type="submit" class="btn btn-link my-3" >Zapisz ilość</button>
 </form:form></td>

tempCase被创建这样的:

<c:forEach var="tempCase" items="${order.getCaseList()}">

因此,这里发生了什么是路径接收适当的对象(参数如预期),但由于某种原因被转换为字符串与它的toString()方法,在这之后抱怨说,它不能将其转换回从StringCase。 ..

我也试过:cases['${tempCase}']cases['tempcase'],和cases[tempCase]它总是将其转换为字符串,在后两个中,它实际上是一个“ tempCase”字符串。

实体:

@Entity
@Proxy(lazy=false)  // test
@Table(name="orders")
public class Order {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    @JoinTable(name="case_quantities",
        joinColumns = {@JoinColumn(name="order_id", referencedColumnName="id")},
        inverseJoinColumns = {@JoinColumn(name="quantity_id", 
        referencedColumnName="id")}
        )
    @MapKeyJoinColumn(name="case_id")
    private Map<Case, Quantity> cases;

    @Column(name="comments")
    private String comments;
    @Column(name="charge")
    private double charge;
    @Enumerated(EnumType.STRING)
    private OrderStatus orderStatus;
    @Column(name="dt")
    private Date date;

    @ManyToOne(cascade= {CascadeType.PERSIST, CascadeType.MERGE,
                         CascadeType.DETACH, CascadeType.REFRESH})
    @JoinColumn(name="client_id")
    private Client client;
(...)
    public Map<Case, Quantity> getCases() {
        if(cases == null) {
            this.cases = new HashMap<Case, Quantity>();
        }
        return cases;
    }

    public List<Case> getCaseList(){
        if(cases == null) {
            this.cases = new HashMap<Case, Quantity>();
        }
        return new ArrayList<>(cases.keySet());
    }
(...)

@Entity
@Proxy(lazy=false)
@Table(name="cases")
public class Case {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;
    @Column(name="length")
    private int length;
    @Column(name="width")
    private int width;
    @Column(name="height")
    private int height;
    @Column(name="surface")
    private double surface;

    @Enumerated(EnumType.STRING)
    private Type type;
    @Enumerated(EnumType.STRING)
    private Material material;
    @Enumerated(EnumType.STRING)
    private Color color;
    @Enumerated(EnumType.STRING)
    private Filling filling;

    @Enumerated(EnumType.STRING)
    private Handle handle;
    @Column(name="handle_num")
    private int handleNum;

    @Column(name="wheels")
    private boolean wheels;
    @Column(name="wheel_num")
    private int wheelNum;

    @Enumerated(EnumType.STRING)
    private Locks locks;

    @Column(name="comments")
    private String comments;
    @Column(name="price")
    private double price;

    // Testing a bi-directional relationship
    @ManyToOne(cascade= {CascadeType.PERSIST, CascadeType.MERGE,
                        CascadeType.DETACH, CascadeType.REFRESH})
    @JoinColumn(name="order_id")
    private Order order;
(...)

控制器方式

    @PutMapping("/updateQuantity")
    public String updateQuantity(@RequestParam("quantity") int quantity,
                                 @ModelAttribute("tempCase") Case tempCase,
                                 @ModelAttribute("order") Order theOrder,
                                 Model theModel) {

        theOrder.getCases().get(tempCase).setQuantity(quantity);

        theModel.addAttribute(theOrder);

        return "redirect:/continueOrder";
    }

整个jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!-- The below enabled Polish character rendering -->
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>

<!DOCTYPE html>

<html lang="pl">

<head>
    <title>WMCASE - </title>
    <meta charset="UTF-8">

    <!--
    <link href="<c:url value="/resources/css/style.css" />" rel="stylesheet">
    -->
    <link rel="stylesheet"
          href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
          crossorigin="anonymous">

</head>

<body>
<c:set var="now" value="<%=new java.util.Date()%>" />

    <div class="container-fluid bg-dark m-0">
        <h2 class="text-light text-center p-3">WMCase - Prośba o wycenę</h2>
    </div>


    <div class="container-fluid align-center">

        <form:form action="newCaseInOrder" method="GET">
            <input name="order" type="hidden" value="${order}"/>
            <input type="submit" value="Dodaj Skrzynkę" class="btn btn-dark my-3" />
        </form:form>

        <table class="table">
            <thead class="thead-dark">
                <tr>
                    <th scope="col" colspan="10">Skrzynki</th>
                </tr>
                <tr>
                    <th scope="col">Wymiary</th>
                    <th scope="col">Typ</th>
                    <th scope="col">Materiał</th>
                    <th scope="col">Kolor</th>
                    <th scope="col">Wypełnienie</th>
                    <th scope="col">Rączki</th>
                    <th scope="col">Koła</th>
                    <th scope="col">Zamki</th>
                    <th scope="col">Uwagi</th>
                    <th scope="col">Ilość</th>
                    <th scope="col">Akcja</th>
                </tr>

            <c:forEach var="tempCase" items="${order.getCaseList()}">

            <!-- Update case, quantity and delete Links -->
            <c:url var="updateQuantityLink" value="/updateQuantity">
                <c:param name="tempCase" value="${tempCase}"/>
                <c:param name="order" value="${order}"/>
            </c:url>

            <c:url var="updateLink" value="/updateCase">
                <c:param name="caseId" value="${tempCase.id}"/>
                <c:param name="orderId" value="${order}" />
            </c:url>

            <c:url var="deleteLink" value="/deleteCase">
                <c:param name="caseId" value="${tempCase.id}"/>
                <c:param name="orderId" value="${order}" />
            </c:url> 

                <tbody>
                    <tr>
                        <td> ${tempCase.length} x ${tempCase.width} x ${tempCase.height} mm</td>
                        <td> ${tempCase.type.getType()}</td>
                        <td> ${tempCase.material.getMaterial()}</td>
                        <td> ${tempCase.color.getColor()}</td>
                        <td> ${tempCase.filling.getFilling()}</td>
                        <td> ${tempCase.handle.getHandle()} - ${tempCase.getHandleNum() } </td>
                        <td> ${tempCase.getWheels()} - ${tempCase.getWheelNum()}</td>
                        <td> ${tempCase.locks.getLocks()} </td>
                        <td> ${tempCase.comments}</td>
                        <td>
                            <form:form action="updateQuantity" modelAttribute="order" method="PUT">

                                <form:input class=""
                                            path="cases[${tempCase}].quantity"
                                            value="${order.getCases().get(tempCase).getQuantity()}"/>
                                |
                                <button type="submit" class="btn btn-link my-3" >Zapisz ilość</button>
                            </form:form></td>
                        <td>
                            <a href="${updateLink}">Modyfikuj</a>
                             |
                            <a href="${deleteLink}"
                            onclick="if (!(confirm('Czy na pewno chcesz usunąć skrzynkę?'))) return false">
                            Usuń</a>
                        <td>
                    </tr>
                </tbody>
            <%-- </c:forEach> --%>
            </c:forEach>

        </table>
        <hr>


        <h3 class="">Dane kontaktowe:</h3>
        <form:form action="sendOrder" modelAttribute="order" method="POST">
            <form:hidden path="id"/>

            <div class="form-inline">
                <div class="col-auto form-group row">
                    <label class="col-form-label">Imię:</label>
                    <form:input class="form-control mx-3 text-right" type="text" path="client.name" />
                </div>
                <div class="col-auto form-group row">
                    <label class="col-form-label">Nazwisko:</label>
                    <form:input class="form-control mx-3 text-right" type="text" path="client.surname" />
                </div>
                <div class="col-auto form-group row">
                    <label class="col-form-label">Email:</label>
                    <form:input class="form-control mx-3 text-right" type="text" path="client.email" />
                </div>
                <div class="col-auto form-group row">
                    <label class="col-form-label">Telefon:</label>
                    <form:input class="form-control ml-3 text-right" type="text" path="client.tel" />
                </div>
            </div>
            <div>
                <label class="col-form-label">Komentarz:</label>
                <form:textarea rows="4" cols="30" class="form-control" type="text" path="comments" />
            </div>

            <button type="submit" class="btn btn-dark my-3" >Wyślij zapytanie</button>

         </form:form>

        <a href="${pageContext.request.contextPath}/clientOrders?clientId=${clientId}">Wróć do zamówień</a>

    </div>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

</body>
</html>

视觉的

查看更多

提问者
Wojciech Warchoł
被浏览
91
Wojciech Warchoł 2020-02-03 04:41

好的,我知道了。问题的确是事实,在forEach中,当我尝试使用它在映射中访问值时,var属性被转换为String,而没有转换回Case。因此,为了尝试将编辑后的参数直接定位到订单所持有的数量,我将新数量保存在临时数量对象(即模型)中,然后使用它来更新定点持有的 map中的实际数量对象。订购。

的jsp

(...)
            <c:forEach var="tempCase" items="${order.getCases()}">
(...)
                            <form:form action="updateQuantity" modelAttribute="tempQuantity" method="PUT">
                                <input name="tempCase" type="hidden" value="${tempCase.key}"/>
                                <form:input class=""
                                            path="quantity"
                                            value="${tempCase.value.quantity}"
                                            />
                                <button type="submit" class="btn btn-link my-3" > Zapisz ilość</button>
                            </form:form></td>
(...)

控制器

(...)
    @PostMapping("/addCaseToOrder")
    public String addCaseToOrder(@ModelAttribute("case") Case theCase,
                                 @ModelAttribute("order") Order theOrder,
                                 Model theModel) {

        System.out.println("Executing the addCaseToOrderMethod");
        theCase.evaluation();
        theOrder.addCase(theCase);
        theOrder.getCharge();
        System.out.println(theCase.toString());
        theModel.addAttribute("order", theOrder);
        System.out.println(theOrder.getCases().size());

        return "redirect:/continueOrder";
    }

    @GetMapping("/continueOrder")
    public String continueOrder(@ModelAttribute("order") Order theOrder,
                                Model theModel) {

        theModel.addAttribute("order", theOrder);

        Quantity tempQuantity = new Quantity();
        theModel.addAttribute("tempQuantity", tempQuantity);

        System.out.println("Executing the continueOrder method");
        System.out.println(theOrder.getCases().size());

        return "new-full-order";
    }
(...)

我的tempCase是一个字符串,我仍然有一个问题。所以我找到了一个肮脏的解决方案。我遍历该订单持有的案例,将它们转换为字符串,然后与已经是字符串的tempCase进行比较。当我找到一个匹配项时,我将使用找到的Case作为密钥来更新数量。:D

控制器

(...)
    @PostMapping("/updateQuantity")
    public String updateQuantity(@ModelAttribute("tempQuantity") Quantity tempQuantity,
                                 @ModelAttribute("tempCase") String tempCaseString,
                                 @ModelAttribute("order") Order theOrder,
                                 Model theModel) {

        for (Case checkedCase : theOrder.getCaseList()) {
            if (checkedCase.toString().equals(tempCaseString)) {
                System.out.println("Found equal cases");
                theOrder.getCases().get(checkedCase).setQuantity(tempQuantity.getQuantity());
            }
        }

        theModel.addAttribute(theOrder);

        return "redirect:/continueOrder";
    }
(...)

仍有一些清理工作要做。

感谢您的帮助A. Wolf!