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

qv7cva1a  于 2024-01-04  发布在  其他
关注(0)|答案(2)|浏览(337)

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

  • 我试图检查控制器本身是否正在发送null数据。但是,数据是由控制器按预期发送的。
  • 我试着调试JSP,input标签正确地显示了相同的值,而Spring的form标签显示的值为空。
  • BindingResult错误
    包含所有必需getter和setter的对象属性
  1. public class MakeSwitchForm implements Serializable {
  2. private Custody custody;
  3. private List<Custody> custodyList;

字符串

控制器

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

JSTL

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

subscriptionSection.jspf

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

HTML源代码

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


有人可以帮助我理解为什么<form:hidden>值设置为空吗?

bqujaahr

bqujaahr1#

当我尝试设置如下值时,它工作得很好:

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

字符串

HTML源代码

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


为什么form:hidden标签不能在里面设置值仍然是个谜。但现在,我认为这是可行的。

zzwlnbp8

zzwlnbp82#

<form:hidden>标签有一个path属性来访问对象的属性。它是从Model::modelAttribute中提到的表单的模型对象中计算的。
所以,你必须把makeSwitchForm放在控制器中的Model上:

  1. model.addAttribute("makeSwitchForm", form);

字符串
现在,您可以像前面的EL表达式一样访问属性custodyList[0].custodyNumber,但使用<c:set>标记:

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

相关问题