Why the <form:hidden> value is set as empty?

136 Views Asked by At

On my default page, I have a hidden fields to set custody details. I've checked the HTML source and the values are coming as empty.

  • I tried to check if the controller itself is sending null data. But, the data is sent by the controller as expected.

  • I tried to debug JSP, input tag is showing the same values correctly whereas, Spring's form tag is showing the values as empty.

  • No BindingResult errors

    Object properties with all the required getters and setters:

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

Controller

@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);
model.addAttribute("makeSwitchForm", form);

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 source

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

Can someone please help me understand why the <form:hidden> value is set as empty.

2

There are 2 best solutions below

8
On

The <form:hidden> tag has a path attribute to access an object's property. It is evaluated from the form's model object mentioned in the Model::modelAttribute.

So, you have to put makeSwitchForm to the Model in the controller:

model.addAttribute("makeSwitchForm", form);

Now, you can access the property custodyList[0].custodyNumber like in previous EL expression but using a <c:set> tag:

<c:set target="${makeSwitchForm.custody}" property="custodyNumber" value="${makeSwitchForm.custodyList[0].custodyNumber}" />
<form:hidden path="custody.custodyNumber" />
0
On

When I tried to set the values as below, it worked fine:

<c:set target="${makeSwitchForm.custody}"property="custodyNumber" value="${makeSwitchForm.custodyList[0].custodyNumber}" /><form:hidden path="custody.custodyNumber" id="custodyNumber_0" />

HTML Source

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

It's still a mystery why the form:hidden tag is not able to set the values within. But for now, I think this works.