Spring @ModelAttribute无效

时间:2016-11-06 04:54:58

标签: spring-mvc modelattribute

我有一个控制器,我在其中放入了get和post方法。两种方法都可以正常工作,但是当我向POST方法引入@ModelAttribute注释时,它开始给我状态400 - 客户端发送的请求在语法上是不正确的。

我不知道我做错了什么。视图看起来像:

<form:form method="post" action="createIdeaPublic" commandName="idea">
    <table class="form">
        <tr>
            <td align="right"><label for="title">Idea Title</label></td><td align="left"><form:input type="text" path="title" id="title"></form:input></td>
            <td align="right"><label for="email">Your Email</label></td><td align="left"><form:input type="text" path="requestorEmail" id="requestorEmail" class="emails"></form:input></td>
        </tr>


        <tr>
            <td align="right"><label for="partnerEmail">CI Contact Email</label></td><td align="left"><form:input type="text" path="cIInitialContact" id="cIInitialContact" class="emails"></form:input></td>
            <td align="right"><label for="sponsorEmail">Sponsor Email</label></td><td align="left"><form:input type="text" path="sponsorEmail" id="sponsorEmail" class="emails"></form:input></td>

        </tr>


        <tr>
            <td align="right"><label for="requestedDeliveryDate">Requested Delivery Date</label></td><td align="left"><form:input type="text" path="requestedDeliveryDate" id="requestedDeliveryDate" class="datepicker"></form:input></td>
            <td align="right"><label>Classification</label></td><td align="left">

                <label for="discretionary" class="radio">Discretionary</label>
                <form:radiobutton path="stateDescription" id="discretionary" value="Discretionary"></form:radiobutton>
                <label for="mandatory" class="radio">Mandatory</label>
                <form:radiobutton path="stateDescription" id="mandatory" value="Mandatory"></form:radiobutton>
                <label for="regulatory" class="radio">Regulatory</label>
                <form:radiobutton path="stateDescription" id="regulatory" value="Regulatory"></form:radiobutton>
            </td>
        </tr>

        <tr>
            <td colspan="4" align="right"><input type="submit" class="ui ui-button ui-corner-all ui-widget" style="margin-top: .6em; margin-right: 1em;font-weight: bold;font-size: 1.2em; width: 150px;" value="Create Idea" /></td>
        </tr>
    </table>
</form:form>

我尝试更改commandName =&#34; idea&#34; to modelAttribute =&#34; idea&#34;但没有好处。

Spring控制器看起来像

@Controller
@RequestMapping ("/createIdeaPublic")
public class CreateIdeaPublicController{


   @RequestMapping(method = RequestMethod.GET)
   public ModelAndView view(ModelMap model) {
      model.addAttribute("areas",Utils.areas);
      return new ModelAndView("createIdeaPublic", "idea", new Idea());
   }


   @RequestMapping(method = RequestMethod.POST)
   public String submit(@ModelAttribute("idea")Idea idea, ModelMap model) {

//     System.out.println(idea.getTitle());
       System.out.println("Hello World");
      return "redirect:createIdeaPublic";
   }

} 

但是,只要我从提交方法中删除 @ModelAttribute(&#34; idea&#34;)创意, ,表单提交就会开始工作。

1 个答案:

答案 0 :(得分:0)

我明白了。这是日期字段的问题。如果我不在日期字段中输入值,我会得到400页。所以这就是我在Controller类中所做的。如果您遇到此问题,请按原样复制(只需相应更改日期格式)。

@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        dateFormat.setLenient(false);

        // true passed to CustomDateEditor constructor means convert empty String to null
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }