1

I having a trouble when doing CRUD in Spring+Hibernate MVC project, the IDE return an HTTP Status 400 error. I have checked some link but they are not the solution:

  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

Here is the form:form in my jsp page:

<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>

This is the controller:

@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";
    }

All of my objects are implemented through Serializable, when I use "Add" action, the url will show: "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" with this error. Please tell me what is the problem in here and how to solve that, I am new in Java Web.

update: here is Quantity class

@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;
}}
Community
  • 1
  • 1
iamatsundere181
  • 1,401
  • 1
  • 16
  • 38
  • TopGun_Viper is right with spoting the problem. But the solution depends on what `Clothe`, `Color` and `Size` are. Are this Objects: value-objects or entitis. Is it important that for example there is only ONE INSTANCE for color "green", or are MORE "green" color INSTANCE withe the same colorcode allowed? – Ralph Jan 03 '16 at 10:05
  • As you see on the model class, I made them as @Entity and when I use them in 'action', Clothe, Color and Size become valued-objects. – iamatsundere181 Jan 03 '16 at 10:20

2 Answers2

2

The parameter "clothe" must be primitive type,in your case , clothe is a serializable object,but it can not be serialized by browser automatically.

update: you can solve it like this:

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

If Clothe, Color and Size are entities, and you want to load them from the database instead of creating new once, then you need to do two things:

  • 1) submit the ID instead of the object, when you send the form
  • 2) add an Spring-Converter that is able to convert IDs into entities.

For some example implementation have a look at this Blog "Spring ID to Entity Conversion":

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Thanks a lots, but if they are value-objects, what can I do? I just want to know more. – iamatsundere181 Jan 03 '16 at 15:07
  • Value objects could been created by Spring when the request is received, but then you must send all fields of the value object (not only the id) – Ralph Jan 03 '16 at 15:18