错误"客户端发送的请求在语法上不正确"当使用Spring MVC进行CRUD时

时间:2016-01-03 04:56:10

标签: java spring hibernate spring-mvc

我在Spring + Hibernate MVC项目中执行CRUD时出错,IDE返回HTTP Status 400错误。我检查了一些链接,但它们不是解决方案:

  1. The request sent by the client was syntactically incorrect.-Spring MVC + JDBC Template
  2. Spring: The request sent by the client was syntactically incorrect ()
  3. http://www.coderanch.com/t/643301/Spring/Spring-MVC-request-message-request
  4. 以下是我的jsp页面中的表单:

    <form:form action="quantity.do" method="GET" commandName="quantity">
                    <form:input type="hidden" path="quantityId" value="1"/>
                    <form:input type="hidden" path="clothes" value="<%= clothe%>"/>
                    <div style="height: 35px; width: 20%; float: left;">
                        Choose color:
                    </div>
                    <div style="height: 35px; width: 78%; float: left">
                        <form:select path="color">
                            <!--object Color-->
                            <form:option value="" label=" - choose color - "/>
                            <form:options items="${colors}" itemLabel="name"/>
                        </form:select>
                    </div>
                    <div style="height: 35px; width: 20%; float: left;">
                        Choose Size
                    </div>
                    <div style="height: 35px; width: 78%; float: left;">
                        <form:select path="size">
                            <!--object size-->
                            <form:option value="" label=" - choose size - "/>
                            <form:options items="${sizes}" itemLabel="sizeDescription"/>
                        </form:select>
                    </div>
                    <div style="height: 35px; width: 20%; float: left;">
                        Quantity:
                    </div>
                    <div style="height: 35px; width: 78%; float: left;">
                        <form:input type="number" path="quantityOfClothes"/>
                    </div>
                    <div style="width: 100%; height: 25px"></div>
                    <div style="height: 50px; width: 16%; float: left; margin-left: 50px">
                        <input type="submit" name="action" value="Add"/>
                    </div>
                    <div style="height: 50px; width: 16%; float: left; margin-left: 50px">
                        <input type="submit" name="action" value="Update"/>
                    </div>
                    <div style="height: 50px; width: 16%; float: left; margin-left: 50px">
                        <input type="submit" name="action" value="Cancel"/>
                    </div>
                </form:form>
    

    这是控制器:

    @RequestMapping(value = "/quantity.do", method = RequestMethod.GET)
    public String doActions(@ModelAttribute Quantity quantity, @RequestParam String action, Map<String, Object> map) {
    
        switch (action.toLowerCase()) {
            case "add":
                break;
            case "update":
                break;
            case "cancel":
                break;
        }
            return "quantity";
        }
    

    当我使用&#34;添加&#34;时,我的所有对象都是通过Serializable实现的。行动,网址将显示:&#34; http://localhost:8080/pinky_spring/quantity.do?quantityId=1&clothes=model.Clothes@229db7d5&color=model.Color@6b248b2f&size=model.Size@591a3a57&quantityOfClothes=1000&action=Add&#34;有这个错误。 请告诉我这里有什么问题以及如何解决这个问题,我是Java Web的新手。

    更新:这是数量类

    @Entity
    @Table(name = "Quantity", schema = "dbo", catalog = "PinkyDB")
    public class Quantity implements java.io.Serializable {
    
    private int quantityId;
    private Clothes clothes; //an object
    private Color color; //an object
    private Size size; //an object
    private Integer quantityOfClothes;
    private Set<ShoppingCart> shoppingCarts = new HashSet<ShoppingCart>(0);
    
    public Quantity() {
    }
    
    public Quantity(int quantityId, Clothes clothes, Color color, Size size) {
        this.quantityId = quantityId;
        this.clothes = clothes;
        this.color = color;
        this.size = size;
    }
    
    public Quantity(int quantityId, Clothes clothes, Color color, Size size, Integer quantityOfClothes, Set<ShoppingCart> shoppingCarts) {
        this.quantityId = quantityId;
        this.clothes = clothes;
        this.color = color;
        this.size = size;
        this.quantityOfClothes = quantityOfClothes;
        this.shoppingCarts = shoppingCarts;
    }
    
    @Id
    @Column(name = "QuantityID", unique = true, nullable = false)
    public int getQuantityId() {
        return this.quantityId;
    }
    
    public void setQuantityId(int quantityId) {
        this.quantityId = quantityId;
    }
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ClothID", nullable = false)
    public Clothes getClothes() {
        return this.clothes;
    }
    
    public void setClothes(Clothes clothes) {
        this.clothes = clothes;
    }
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ColorID", nullable = false)
    public Color getColor() {
        return this.color;
    }
    
    public void setColor(Color color) {
        this.color = color;
    }
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SizeID", nullable = false)
    public Size getSize() {
        return this.size;
    }
    
    public void setSize(Size size) {
        this.size = size;
    }
    
    @Column(name = "QuantityOfClothes")
    public Integer getQuantityOfClothes() {
        return this.quantityOfClothes;
    }
    
    public void setQuantityOfClothes(Integer quantityOfClothes) {
        this.quantityOfClothes = quantityOfClothes;
    }
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "quantity")
    public Set<ShoppingCart> getShoppingCarts() {
        return this.shoppingCarts;
    }
    
    public void setShoppingCarts(Set<ShoppingCart> shoppingCarts) {
        this.shoppingCarts = shoppingCarts;
    }}
    

2 个答案:

答案 0 :(得分:2)

参数“clothe”必须是基本类型,在您的情况下,clothe是一个可序列化的对象,但它不能被浏览器自动序列化。

更新: 你可以这样解决:

<form:input type="hidden" path="clothes.id" value="<%=clothe.getId()%>"/>

答案 1 :(得分:0)

如果select case when sum(quantity) = 0 then 0 else sum(unitprice * quantity) / sum(quantity) end from sales; ClotheColor是实体,并且您想要从数据库加载它们而不是创建新的一次,那么您需要做两件事:

  • 1)发送表单
  • 时,提交ID而不是对象
  • 2)添加一个能够将ID转换为实体的Spring-Converter。

对于某些示例实现,请查看此博客“Spring ID to Entity Conversion”: