Spring MVC - 请求语法错误

时间:2013-06-21 15:39:17

标签: java spring-mvc

我正在尝试与这个请求处理程序交谈(对不起的语言感到抱歉,我是春天的新手,我认为这就是所谓的)。我环顾四周,看到了将记录级别设置为DEBUG的建议,我跟我们谈了这个问题,他问我没花时间。我的想法已经用完了,我已经用Google搜索并查看了大量的堆栈溢出,似乎找不到我的方式。

这是我的控制器:

@RequestMapping(value={"/project/save", "/project/add", "/project/edit"}, method=RequestMethod.POST)
public ModelAndView saveProject(@ModelAttribute ProjectForm form) {
    ModelAndView mav = this.createBaseModelAndView("project/start");
    ModelAndView redirect = new ModelAndView("redirect:/admin/projects");
    return this.projectUtils.saveProject(form, mav, redirect);
}

在请求中发送这些变量:

projectId:          51c4619c036492494eca2740
vendorId:           5113babc0364a6e3eb6aa368
name:               Cedar Goodies
description:        ldfkj
url:                http://caseywise.com
openDate:           06/22/2013 12:00 AM
closeDate:          06/26/2013 12:00 AM
organizerId:        517da4b92e3a896d9c613b2e
allowIndividualShipping:    false
allowIndividualPayments:    false
allowOrderEditing:          false
items[0]:           51c461a9036492494eca2741

这些请求标题

Accept:     text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:    gzip,deflate,sdch
Accept-Language:    en-US,en;q=0.8
Cache-Control:  no-cache
Connection:     keep-alive
Content-Length: 366
Content-Type:   application/x-www-form-urlencoded
Cookie:     JSESSIONID=6eebd82250b5429bfd8bb5f82e42
Host:       localhost:8080
Origin:     http://localhost:8080
Pragma:     no-cache
Referer:        http://localhost:8080/admin/project/start
User-Agent:     Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36

ProjectForm对象(来自控制器中的saveProject方法)需要这些属性

private String projectId;
private String name;
private String description;
private Date openDate;
private Date closeDate;
private String organizerId;
private List<String> items; // hidden inputs for form submission, since we can't send an object through a form
private String url;
private boolean allowOrderEditing;
private boolean allowIndividualPayments;
private boolean allowIndividualShipping;
private String vendorId;

我回来了400 - The request sent by the client was syntactically incorrect ()。你看到任何明显的错误吗?你想看更多代码吗?有什么想法吗?


修改

根据Pavel的评论包含ProjectForm个对象:


package com.ordercollector.viewmodels.project;

import com.ordercollector.controllers.utils.ControllerUtils;
import com.ordercollector.entities.ProjectItem;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.http.impl.cookie.DateParseException;
import org.apache.http.impl.cookie.DateUtils;
import org.jboss.logging.Logger;

/**
 *
 * @author jables
 */
public class ProjectForm {
    private String projectId;
    private String name;
    private String description;
    private Date openDate;
    private Date closeDate;
    private String organizerId;
    private List<String> items; // hidden inputs for form submission, since we can't send an object through a form
    private String url;
    private boolean allowOrderEditing;
    private boolean allowIndividualPayments;
    private boolean allowIndividualShipping;
    private String vendorId;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the openDate
     */
    public Date getOpenDate() {
        return openDate;
    }

    public String getOpenDateFormatted() {
        return ProjectForm.formatDateAsString(openDate);
    }

    /**
     * @param openDate the openDate to set
     */
    public void setOpenDate(Date openDate) {
        this.openDate = openDate;
    }

    /**
     * @return the closeDate
     */
    public Date getCloseDate() {
        return closeDate;
    }

    /**
     * Returns the project's close date in a formatted string.  If the close
     * date has not been set, an empty string is returned.
     * @return String
     */
    public String getCloseDateFormatted() {
        return ProjectForm.formatDateAsString(closeDate);
    }

    /**
     * @param closeDate the closeDate to set
     */
    public void setCloseDate(Date closeDate) {
        this.closeDate = closeDate;
    }

    /**
     * @return the organizerId
     */
    public String getOrganizerId() {
        return organizerId;
    }

    /**
     * @param organizerId the organizerId to set
     */
    public void setOrganizerId(String organizerId) {
        this.organizerId = organizerId;
    }

    /**
     * @return the item
     */
    public List<String> getItems() {
        return items;
    }

    /**
     * @param item the item to set
     */
    public void setItems(List<String> items) {
        this.items = items;
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return the projectId
     */
    public String getProjectId() {
        return projectId;
    }

    /**
     * @param projectId the projectId to set
     */
    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    /**
     * @return the allowOrderEditing
     */
    public boolean getAllowOrderEditing() {
        return allowOrderEditing;
    }

    /**
     * @param allowOrderEditing the allowOrderEditing to set
     */
    public void setAllowOrderEditing(boolean allowOrderEditing) {
        this.allowOrderEditing = allowOrderEditing;
    }

    /**
     * @return the allowIndividualPayments
     */
    public boolean getAllowIndividualPayments() {
        return allowIndividualPayments;
    }

    /**
     * @param allowIndividualPayments the allowIndividualPayments to set
     */
    public void setAllowIndividualPayments(boolean allowIndividualPayments) {
        this.allowIndividualPayments = allowIndividualPayments;
    }

    /**
     * @return the allowIndividualShipping
     */
    public boolean getAllowIndividualShipping() {
        return allowIndividualShipping;
    }

    /**
     * @param allowIndividualShipping the allowIndividualShipping to set
     */
    public void setAllowIndividualShipping(boolean allowIndividualShipping) {
        this.allowIndividualShipping = allowIndividualShipping;
    }

    /**
     * Returns the given date in the format used by the form.  If date object
     * is null, an empty string is returned.
     * @param date
     * @return String
     */
    public static String formatDateAsString(Date date) {
        if (null == date) 
        {
            return new String("");
        } 
        else 
        {
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy h:mm a");
            return df.format(date);
        }
    }

    /**
     * @return the vendorId
     */
    public String getVendorId() {
        return vendorId;
    }

    /**
     * @param vendorId the vendorId to set
     */
    public void setVendorId(String vendorId) {
        this.vendorId = vendorId;
    }
}

1 个答案:

答案 0 :(得分:3)

默认情况下,Spring(版本3)使用DateTimeFormatAnnotationFormatterFactory进行Date字段绑定。

模型对象中的Date字段或getter必须使用正确的@DateTimeFormat注释进行注释:

@DateTimeFormat(pattern="MM/dd/yyyy hh:mm a")
public Date getCloseDate() {
    return closeDate;
}