为什么<form:hidden>值设置为空?

问题描述 投票:0回答:1

在我的默认页面上,我有一个隐藏字段来设置托管详细信息。我检查了 HTML 源代码,值是空的。

  • 我尝试检查控制器本身是否正在发送

    null
    数据。但是,数据是由控制器按预期发送的。

  • 我尝试调试 JSP,输入标记正确显示相同的值,而 Spring 的表单标记将值显示为空。

  • 没有

    BindingResult
    错误

    具有所有必需的 getter 和 setter 的对象属性:

public class MakeSwitchForm implements Serializable {
    private Custody custody;
    private List<Custody> custodyList;

控制器

@GetMapping
public String defaultView(Model model, HttpServletRequest request, HttpServletResponse response, @RequestHeader(name = "Accept-Language") String locale)
        throws ServletException, IOException, PropertyValueNotFoundException, NoSuchMessageException  {
MakeSwitchForm form = new MakeSwitchForm();
List<Custody> custodyList = null;
custodyList = filterUserRightCustodies(redCustody, custodyList);// fetches custody list
form.setCustodyList(custodyList);

JSTL

<form:form id="makeSwitchForm" name="makeSwitchForm" modelAttribute="makeSwitchForm" action="${actionUrl}/makeSwitch" method="post" class="opux-g-container">
<%@ include file="subscriptionSection.jspf"%>

subscriptionSection.jspf

<c:choose>
     <c:when test="${fn:length(makeSwitchForm.custodyList) == 1}">
        <input type="hidden" value="<c:out value="${makeSwitchForm.custodyList[0].custodyNumber}" />" id="custodyNumber_0" />
         <form:hidden path="custody.custodyNumber" value="${makeSwitchForm.custodyList[0].custodyNumber}" />

HTML 源代码

<input type="hidden" value="0007832348" id="custodyNumber_0">  
<input id="custody.custodyNumber" name="custody.custodyNumber" value="" type="hidden">

有人可以帮我理解为什么

<form:hidden>
值设置为空吗?

forms spring-mvc jsp jsp-fragments
1个回答
0
投票

<form:hidden>
标签具有
path
属性来访问对象的属性。它是根据
Model::modelAttribute
中提到的表单模型对象进行评估的。

所以,你必须将

makeSwitchForm
放入控制器中的
Model

model.addAttribute("makeSwitchForm", form);

现在,您可以像之前的 EL 表达式一样访问属性

custodyList[0].custodyNumber
,但使用
<form:hidden>
标签:

<form:hidden path="custodyList[0].custodyNumber" />
© www.soinside.com 2019 - 2024. All rights reserved.