1

I have this simple form with 2 checkboxes and a submit button. When I submit the form, I get this error

HTTP Status 400 The request sent by the client was syntactically incorrect.

This is my POJO:

public class Menu{
    private String day;
    private String name;
    private int price;

    public Menu(){
    }

    public Menu(String day, String name, int price) {
        this.day = day;
        this.name = name;
        this.price = price;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDay() {
        return day;
    }

    public void setDay(String l) {
        this.day = l;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 7 * hash + this.day.hashCode();
        hash = 7 * hash + this.name.hashCode();
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        boolean result = false;
        System.out.println("ARE YOU EVER CALLLED HOW MANY TIMES");
        if (object == null || object.getClass() != getClass()) {
            result = false;
        } else {
            Menu sc = (Menu) object;
            if (this.day == sc.getDay() && this.name == sc.getName()
                && this.price == sc.getPrice()) {
                result = true;
            }
        }
        return result;
    }

This is my Order class:

public class Order {
    private List<Menu> menus = new ArrayList<Menu>();
    public Order(){}

    public Order(ArrayList<Menu> menus){
        this.menus =  menus;        
    }

    public List<Menu> getMenus() {
        return menus;
    }

    public void setMenus(ArrayList<Menu> menus) {
        this.menus = menus;
    }

}

And this is my controller:

@Controller
public class RestaurantController {
    @RequestMapping(value = "/menu", method = RequestMethod.GET)
    public String menuPage(Model model){            
        Order o = new Order();
        ArrayList<Menu> m = new ArrayList<Menu>();
        m.add(new Menu("Sunday", "Phir Aloo", 12));
        m.add(new Menu("Sunday", "Phir Cholay", 9));
        model.addAttribute("today", m);
        model.addAttribute("order", o);
        return "/menu";
    }

    @RequestMapping(value = "/confirm", method = RequestMethod.POST)
    public String done(@ModelAttribute(value="order") Order order, Model model){            
        return "/confirm";
    }

And this is my menu.jsp: (http://localhost:9080/res/menu)

<form:form modelAttribute="order" method="post" action="/res/confirm">  
    <c:forEach items="${today}" var="r">
        <form:checkbox path="menus" value="${r}" label="${r.name }    ${r.price }" />
    </c:forEach>
    <input type="submit" value="Submit Data">

</form:form>

Now I just expect Class Order's property 'menus' to be filled with selected checkboxes. Instead I get this error "The request sent by the client was syntactically incorrect. I have looked up every possible answer on this website but nothing seems to be solving the problem.


After @R Sawant's suggestion I was able to solve the problem. Here is my Property Editor.

public class MenuTypeEditor extends PropertyEditorSupport {
    public void setAsText(String text) { 
        setValue(new Menu(text.toUpperCase()));
    }
}

I kept this class inside the same package which has Menu.java and Order.java

Now inside my controller wrote this:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Menu.class, new MenuTypeEditor());
}

And voila! Magic happened.

I hope this answer can help someone.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Faraz
  • 6,025
  • 5
  • 31
  • 88

1 Answers1

1

The problem is with the value you are posting when the check box is ticked. Look at the below code

<form:checkbox path="menus" **value="${r}"** label="${r.name }    ${r.price }" />

See what have assigned to value attribute in the above line. Its whole object of menu. It will essentially post the toString() representation of the object. Since you have not implemented toString() for Menu class, something like Menu@1ed2e55e gets posted for the check box value. Spring is unable to convert this to something meaningful and hence the problem.

You have to make use of property editor support to deal with these type of situations. Property editor will help you convert string to Object and vice versa. In your case String to Menu object and vice versa. Take a look at examples of property editors. Hope this helps

Edit:- a google search got this result. Take a look at it, may help you to understand.

Community
  • 1
  • 1
R Sawant
  • 241
  • 1
  • 8
  • @R Sawant I am working on it will update you after implementing PropertyEditor – Faraz Nov 29 '15 at 23:47
  • Thanks @R Sawant it worked. I am posting my property editor in my question. There I encountered a very small problem to which I was able to find a work around. Still if you can take a look at it and tell me how to fix it I will be thankful to you. Otherwise, your previous suggestion sufficed. – Faraz Nov 30 '15 at 04:51