Spring Form:提交表单后,我的非null值变为null

时间:2017-01-11 21:47:08

标签: java spring spring-mvc spring-form

我正在使用

  • Hibernate
  • 弹簧
  • Apache Tiles
  • JS​​TL

我在表格中填写了充实的价值

@RequestMapping(value = "/create")
public String createInsuranceType(Model model, HttpServletRequest request) {
    InsuranceType insuranceType = new InsuranceType();
    InsuranceCompany insuranceCompany = insuranceCompanyService
            .getInsuranceCompanyById(Long.parseLong(request.getParameter("id")));
    insuranceType.setInsuranceCompany(insuranceCompany);
    model.addAttribute("insuranceType", insuranceType);
    System.out.println(insuranceType);
    return "insurancetype-create";
}

这是我的控制台输出

InsuranceType [id=0, type=null, insuranceCompany=InsuranceCompany [id=1, fullName=full, shortName=short]]

在jsp文件中我的形式我只想填写类型值,但我希望我的保险公司保持原样。我的表格:

<sf:form method="post"
    action="${pageContext.request.contextPath}/insurancetype/create"
    commandName="insuranceType" id="insuranceTypeForm" class="col s12">
    <sf:hidden path="id" />
    <div class="row">
        <div class="input-field col s6">
            <sf:input path="type" id="type" type="text" class="validate" />
            <label for="type">Insurance Type</label>
        </div>
    </div>
    <div class="row">
        <div class="col s12">
            <c:choose>
                <c:when test="${insuranceType.id < 1}">
                    <a onclick="document.getElementById('insuranceTypeForm').submit()"
                        class="waves-effect waves-light btn"><i
                        class="material-icons right">save</i>Zapisz</a>
                </c:when>
                <c:otherwise>
                    <a onclick="document.getElementById('insuranceTypeForm').submit()"
                        class="waves-effect waves-light btn"><i
                        class="material-icons right">edit</i>Edytuj</a>
                </c:otherwise>
            </c:choose>
        </div>
    </div>
</sf:form>

提交后我的输出如下:

InsuranceType [id=0, type=test value, insuranceCompany=null]

我的POST控制器

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String insuranceTypeCreated(@ModelAttribute("insuranceType") InsuranceType insuranceType,
        @RequestParam("id") long id) {
    System.out.println(insuranceType);
    insuranceTypeService.insertOrUpdateInsuranceType(insuranceType);
    if (id < 1)
        return "redirect:/insurancecompany/list";
    else
        return "redirect:/insurancetype/list";
}

我试过像

这样的东西
<sf:hidden path="insuranceCompany" />

但它返回错误

HTTP Status 400 -

type Status report

message

description The request sent by the client was syntactically incorrect.

Apache Tomcat/8.0.39

1 个答案:

答案 0 :(得分:0)

尝试在modelAttribute="insuranceType"标记中使用sf:form