使用 Java Bean 和 JSP 的属性填充 HTML 表单?

发布于 2024-12-13 09:01:18 字数 975 浏览 2 评论 0原文

我有一个带有这样的表单的网页:

<html>
<head>
    ...
</head>
<body>
<form id="form4" name="form4" method="post" action="Receive.jsp">
    <input name="inputField1" type="text"/>
    ...
    <input type="submit" value="Send"/>
</form>
</body>

</html>

然后在我的 Receive.jsp 中我使用:

<jsp:useBean id="form4" class="control.FormBean4" scope="session"/>
<jsp:setProperty name="form4" property="*"/>

将表单中的所有输入数据获取到 Bean 中,然后我用它做一些事情。现在,我想要做的是将 Bean 中的数据重定向到另一个包含另一个表单的 JSP,并使用我的 Bean 中的属性填写该表单的输入字段。我的问题是是否有某种方法可以自动填写表单,如 但向后?

我已经尝试过 ,但它显然不起作用(我在 JSP 参考中的某处读到这是无效的) ,所以我想知道你是否知道一些方法来做到这一点,因为我有不止一个表单,每个表单都有一堆字段,我想保存一一设置值的所有工作。

我仍然是 JSP 的新手,对 JSTL 或任何框架(如 Struts)了解不多,任何帮助将不胜感激。

I have webpage with a form like this:

<html>
<head>
    ...
</head>
<body>
<form id="form4" name="form4" method="post" action="Receive.jsp">
    <input name="inputField1" type="text"/>
    ...
    <input type="submit" value="Send"/>
</form>
</body>

</html>

Then in my Receive.jsp I use:

<jsp:useBean id="form4" class="control.FormBean4" scope="session"/>
<jsp:setProperty name="form4" property="*"/>

To get all the input data from the form into a Bean, then I do some stuff with it. Now, what I want to do is to redirect the data from the Bean into another JSP with another form in it and fill out the input fields of that form with the properties from my Bean. My question is if there is some way to automatically fill the form like <jsp:setProperty name="form4" property="*"/> but backwards?

I already tried <jsp:getProperty name="form4" property="*"/>, but it obviously didn't work (I read somewhere in the JSP reference that this is not valid), so I'm wondering if you know some way to do this, because I have more than just one form each one with a bunch of fields and I'd like to save all the work of setting the values one by one.

I'm still a newbie in JSP and don't really know much about the JSTL or any frameworks like Struts, any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹沉浮 2024-12-20 09:01:18

我的问题是是否有某种方法可以自动填写表单,例如 但向后填写?


否您需要自己做,或者使用基于 JSP/Servlet 的 MVC 框架,如 JSF、Struts、Spring-MVC 等。

要自己做:

<input type="text" name="textName" value="${fn:escapeXml(bean.textName)}" />
<select name="dropdownName">
  <c:forEach items="${bean.dropdownOptions}" var="dropdownOption">
    <option value="${dropdownOption.key}" ${bean.dropdownName == dropdownOption.key ? 'selected' : ''}>${dropdownOption.value}</option>
  </c:forEach>
</select>
<c:forEach items="${bean.checkboxOptions}" var="checkboxOption">
  <c:forEach items="${bean.checkboxName}" var="checkboxName">
    <c:if test="${checkboxName == checkboxOption.key}">
      <c:set var="checked" value="true" />
    </c:if>
  </c:forEach>
  <input type="checkbox" name="checkboxName" value="${checkboxOption.key}" ${checked ? 'checked' : ''}>${checkboxoption.value}<br/>
</c:forEach>

使用例如 JSF 来做:

<h:inputText value="#{bean.textName}" />
<h:selectOneMenu value="#{bean.dropdownName}">
  <f:selectItems value="#{bean.dropdownOptions}" />
</h:selectOneMenu>
<h:selectManyCheckbox value="#{bean.checkboxName}">
  <f:selectItems value="#{bean.checkboxOptions}" />
</h:selectManyCheckbox>

(这反过来又立即最小化 和所有 Servlet 代码样板)

My question is if there is some way to automatically fill the form like <jsp:setProperty name="form4" property="*"/> but backwards?

No. You'd need to either do it yourself, or to head to a JSP/Servlet based MVC framework like JSF, Struts, Spring-MVC, etc.

To do it yourself:

<input type="text" name="textName" value="${fn:escapeXml(bean.textName)}" />
<select name="dropdownName">
  <c:forEach items="${bean.dropdownOptions}" var="dropdownOption">
    <option value="${dropdownOption.key}" ${bean.dropdownName == dropdownOption.key ? 'selected' : ''}>${dropdownOption.value}</option>
  </c:forEach>
</select>
<c:forEach items="${bean.checkboxOptions}" var="checkboxOption">
  <c:forEach items="${bean.checkboxName}" var="checkboxName">
    <c:if test="${checkboxName == checkboxOption.key}">
      <c:set var="checked" value="true" />
    </c:if>
  </c:forEach>
  <input type="checkbox" name="checkboxName" value="${checkboxOption.key}" ${checked ? 'checked' : ''}>${checkboxoption.value}<br/>
</c:forEach>

To do it with for example JSF:

<h:inputText value="#{bean.textName}" />
<h:selectOneMenu value="#{bean.dropdownName}">
  <f:selectItems value="#{bean.dropdownOptions}" />
</h:selectOneMenu>
<h:selectManyCheckbox value="#{bean.checkboxName}">
  <f:selectItems value="#{bean.checkboxOptions}" />
</h:selectManyCheckbox>

(which in turn also immediately minimizes the <jsp:useBean> and all Servlet code boilerplate)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文