1

我对简单的 MVC 应用程序有疑问。

dayoff.html
<div id="dayoff_operations" class="big_box_1">
    <form:form modelAttribute="dayOff" enctype="multipart/form-data" action="/dayoff.html" >
        <div style="width: 50%; float: left; margin-top: 0px ">
            <form:label path="forDate" ><spring:message code="dayoff.fordate.label"/></form:label>
            <form:input path="ax_forDate" id="datepicker" style="width:65px" readonly="readonly"/>&nbsp;&nbsp;
        </div>
        <div style="width: 50%; float: right; margin-top: 0px ">
            <form:label path="toDate" ><spring:message code="dayoff.todate.label"/></form:label>
            <form:input path="ax_toDate" id="datepicker2" style="width:65px" readonly="readonly" />&nbsp;&nbsp;
        </div>
        <div style="float: left">
            <form:select cssClass="more" path="dayofftype" items="${dayoffTypes.list}" itemLabel="label" itemValue="value" htmlEscape="false" cssErrorClass="errorsField"/>
            <form:errors path="dayofftype" cssClass="errors"/>
        </div>
        <input type="submit" name="add" value="Add"/>
    </form:form>

这是我的控制器:

@Controller
@RequestMapping("/dayoff")
@SessionAttributes(types = {DayOff.class})
public class DaysOffController {
private Validator validator;
@Autowired
private ReloadableResourceBundleMessageSource messages;

@ModelAttribute("dayoffTypes")
public Map<String, Object> populateDayoffTypes(Locale locale) {
    return Utils.createComboMap(DayoffType.values(), messages, locale);
}

@RequestMapping(method = RequestMethod.GET)
public String setupForm(Model model) {      
    model.addAttribute("dayOff", new DayOff());
    model.addAttribute("cur_period",SessionManager.getCurrentPeriod());
    return "dayoff";
}

@RequestMapping(method = RequestMethod.POST, params = "add")
public String addNewHoliday(@ModelAttribute DayOff dayOff, BindingResult result, ModelMap model) {
    System.out.println("AAA");
    return "/dayoff";
}

当我单击添加按钮时出现错误 400:SRVE0295E:报告错误:出现 400,服务器控制台上没有信息。我假设它与 setupForm() 方法有关,该方法显示表单并将其粉碎以供将来提交。你能指导我吗?

4

2 回答 2

5

错误 400:SRVE0295E 也会在 MVC 无法填充@ModelAttribute FormHTML 表单中的值时发生。

例如:

  • Form具有原始属性int someCount
  • 您的 HTML 表单必须<input>为这个原始属性赋值
  • 用户为此输入了空值 ( null)<input>

您不应使用原始类型,例如int,booleanfloat

于 2014-05-29T15:28:47.733 回答
0

setupForm 和 addNewHoliday 的返回语句不同。我假设您想从两者返回相同的视图名称。如您所见,提交表单时,我假设“/dayoff”的 addNewHoliday 返回值实际上应该是“dayoff”

于 2013-05-15T21:38:00.573 回答