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

qv7cva1a  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(99)

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

  • 我试图检查控制器本身是否正在发送null数据。但是,数据是由控制器按预期发送的。
  • 我试着调试JSP,input标签正确地显示了相同的值,而Spring的form标签显示的值为空。
  • 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);
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源代码

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


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

bqujaahr

bqujaahr1#

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

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

字符串

HTML源代码

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


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

zzwlnbp8

zzwlnbp82#

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

model.addAttribute("makeSwitchForm", form);

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

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

相关问题