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
errorsObject 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.
The
<form:hidden>
tag has apath
attribute to access an object's property. It is evaluated from the form's model object mentioned in theModel::modelAttribute
.So, you have to put
makeSwitchForm
to theModel
in the controller:Now, you can access the property
custodyList[0].custodyNumber
like in previous EL expression but using a<c:set>
tag: