【问题标题】:Spring Form: My not null value becomes null after submitting the formSpring Form:提交表单后我的非空值变为空
【发布时间】:2017-01-11 21:47:07
【问题描述】:

我正在使用

  • 休眠
  • 春天
  • Apache Tiles
  • JSTL

我在我的表单中输入填充值

@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 文件表单中,我只想填写类型值,但我希望我的 insuranceCompany 保持原样。我的表格:

<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

【问题讨论】:

    标签: java spring spring-mvc spring-form


    【解决方案1】:

    尝试在您的sf:form 标签中使用modelAttribute="insuranceType"

    【讨论】:

    • 它不起作用,但非常感谢您的回复:)
    猜你喜欢
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多