Spring MVC HTTP Status 400 - 客户端发送的请求在语法上是不正确的

时间:2014-07-04 09:34:41

标签: spring spring-mvc

我的物品对象

public class Item extends MyBaseObject {

    private String          name;
    private Category        category;
    private ItemType        itemType;
    private String          description;
    private Long            reorderLevel;
    private Long            freezeLevel;
    private Set<Inventory>  inventories = new TreeSet<>();
    private long            totalCount;
    private long            currentlyIssued;
    private long            availableStock;

    ... getter and setters
}

itemAdd.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<jsp:include page="/WEB-INF/views/common/head.jsp" />

<h2 class="demoHeaders">Item (Add New)</h2>

<form:form id="frmItemAdd" method="post" modelAttribute="item">
    <table class="vsform">
        <tbody>
            <tr>
                <td>
                    <form:label path="name"><spring:message code="item.name"></spring:message></form:label>
                </td>
                <td>
                    <form:input path="name" class="inputlarge required"/>
                    <form:errors path="name" class="errorMsg"/>
                </td>
            </tr>
            <tr>
                <td>
                    <form:label path="description"><spring:message code="item.description"></spring:message></form:label>
                </td>
                <td>
                    <form:textarea path="description" rows="5" cols="50" class="inputlarge"/>
                    <form:errors path="description" class="errorMsg"/>
                </td>
            </tr>
            <tr>
                <td>
                    <form:label path="category"><spring:message code="item.category"></spring:message></form:label>
                </td>
                <td>
                    <form:select path="category" class="myform inputlarge">
                        <form:options items="${listCategory}" itemLabel="name" itemValue="id"/>
                    </form:select>
                    <form:errors path="category" class="errorMsg"/>
                </td>
            </tr>
            <tr>
                <td>
                    <form:label path="itemType"><spring:message code="item.itemType"></spring:message></form:label>
                </td>
                <td>
                    <form:select path="itemType" class="myform inputlarge">
                        <form:options items="${listItemType}" itemLabel="name" itemValue="id"/>
                    </form:select>
                    <form:errors path="itemType" class="errorMsg"/>
                </td>
            </tr>
            <tr>
                <td>
                    <form:label path="reorderLevel"><spring:message code="item.reorderLevel"></spring:message></form:label>
                </td>
                <td>
                    <form:input path="reorderLevel" class="inputsmall required"/>
                    <form:errors path="reorderLevel" class="errorMsg"/>
                </td>
            </tr>
            <tr>
                <td>
                    <form:label path="freezeLevel"><spring:message code="item.freezeLevel"></spring:message></form:label>
                </td>
                <td>
                    <form:input path="freezeLevel" class="inputsmall required"/>
                    <form:errors path="freezeLevel" class="errorMsg"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <button id="buttonReset" type="reset">Reset</button>&nbsp;&nbsp;&nbsp;&nbsp;<button id="buttonSubmit">Submit</button>
                </td>
            </tr>
        </tbody>
    </table>
</form:form>

<jsp:include page="/WEB-INF/views/common/foot.jsp" />  

我的控制器ItemAddController

    @Controller
@RequestMapping("/ims/item/addNew")
public class ItemAddController extends InventoryBaseController {

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private ItemAddValidator itemAddValidator;

    @ModelAttribute("listCategory")
    public List<Category> getListCategory(){
        return inventoryService.listCategory();
    }

    @ModelAttribute("listItemType")
    public List<ItemType> getListItemType(){
        return inventoryService.listItemType();
    }

    @RequestMapping(method=RequestMethod.GET)
    public String itemAdd(Model model, RedirectAttributes reat, Principal principal){

        Item item = new Item();

        model.addAttribute("item", item);
        return "manager/item/itemAdd";
    }

    @RequestMapping(method=RequestMethod.POST)
    public String itemAdd(@ModelAttribute Item item, Model model, BindingResult result, RedirectAttributes reat, Principal principal){
        this.itemAddValidator.validate(item, result);
        if (result.hasErrors()){
            return "manager/item/itemAdd";
        }

        item.setAddDefaults(principal.getName());
        if (this.inventoryService.addItem(item)){
            reat.addFlashAttribute("message", "Item record added successfully.");
        } else {
            reat.addFlashAttribute("message", "Item record not added.");
        }
        return "redirect:/ims/item/addNew";
    }
}

问题的关键在于,当我为每个字段提供正确的值时,表单提交正常工作。但是当我为表单字段reorderLevel(定义为Long)提供'a'或任何文本值时,浏览器会抛出此错误:客户端发送的请求在语法上是不正确的。

我已经在messages.properties文件中定义了以下内容:

typeMismatch=Could not convert to the required type.
typeMismatch.java.util.Calendar=Please enter date in the dd-MM-yyyy format.
typeMismatch.java.math.BigDecimal=Please enter value in correct format 0.00
typeMismatch.java.lang.Long=Must specify an integer value.

请帮忙......谢谢

0 个答案:

没有答案