java 以 Struts 1 形式显示列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15229738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display a list in a Struts 1 form
提问by Alexis Dufrenoy
I have to write a backoffice page in order to determine which shops have to be display on a frontoffice page, in a Struts 1 application. So I have a list of shops (boutiques, in the code), contained in a form.
我必须编写一个后台页面,以确定哪些商店必须显示在 Struts 1 应用程序中的前台页面上。所以我有一个商店列表(精品店,在代码中),包含在一个表格中。
The form:
表格:
public class ListeBoutiquesForm extends ActionForm {
private List<Boutique> boutiques = new ArrayList<Boutique>();
public List<Boutique> getBoutiques() {
return boutiques;
}
public void setBoutiques(List<Boutique> boutiques) {
this.boutiques = boutiques;
}
}
and the save action:
和保存操作:
public ActionForward sauverBoutiques(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
ListeBoutiquesForm vlbf = (ListeBoutiquesForm) form;
for (Boutique boutique : vlbf.getBoutiques()) {
boutiqueService.updateBoutique(boutique);
}
request.getSession().removeAttribute("ListBoutiques");
return listeSites(mapping, form, request, response);
}
The Boutique class contains an id, a name and a boolean called "selected". I want to display the name with a checkbox for each element of my boutiques list, and a submit button calling my save action, but I lack the Struts taglib knowledge to write the JSP. Can you help? I know it should involve a logic:iterate
, a html:checkbox
and probably a html:hidden
to store the id, but I have the feeling something is missing and there is something I don't understand. How are those linked together, so to say? How will I get back a ListeBoutiquesForm with the updated values in my save action ?
Boutique 类包含一个 id、一个名称和一个名为“selected”的布尔值。我想为我的精品店列表的每个元素显示一个带有复选框的名称,以及一个调用我的保存操作的提交按钮,但我缺乏编写 JSP 的 Struts taglib 知识。你能帮我吗?我知道它应该涉及 a logic:iterate
, ahtml:checkbox
并且可能是 ahtml:hidden
来存储 id,但是我感觉缺少某些东西并且有些东西我不明白。可以这么说,这些是如何联系在一起的?我将如何在我的保存操作中取回带有更新值的 ListeBoutiquesForm?
Edit:
编辑:
Following Susie's advices, I came up with the following JSP:
按照 Susie 的建议,我想出了以下 JSP:
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<html:errors/>
<html:form action="/admin/boutiqueviparis">
<input type="hidden" name="reqCode" value="sauverBoutiques" />
<table class="tableStatic noHead">
<logic:notEmpty name ="viparisListeBoutiquesForm" property="boutiques">
<logic:iterate id="boutique" name ="viparisListeBoutiquesForm" property="boutiques">
<tr>
<td>
<html:hidden name ="boutique" property="id" />
<bean:write name="boutique" property="nom" />
</td>
<td>
<html:checkbox name="boutique" property="selected" value="true" />
</td>
</tr>
</logic:iterate>
<html:submit styleClass="boutonrouge" value="Valider" onclick="this.form.reqCode.value='sauverBoutiques';return window.confirm('Modification validée');"/>
</logic:notEmpty>
</table>
</html:form>
This displays the list of the shops and the checkboxes, which are checked accordingly to the value of the "selected" field. But my save action doesn't work. Here my save action, if anyone has an idea:
这将显示商店和复选框的列表,根据“已选择”字段的值对其进行检查。但是我的保存操作不起作用。这是我的保存操作,如果有人有想法:
public ActionForward sauverBoutiques(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
ViparisListeBoutiquesForm vlbf = (ViparisListeBoutiquesForm) form;
for (Boutique boutique : vlbf.getBoutiques()) {
log.debug("########## BOUTIQUE nom : "+boutique.getNom()+" selected :"+boutique.isSelected());
boutiqueService.updateBoutique(boutique);
}
request.getSession().removeAttribute("viparisListBoutiques");
return listeSites(mapping, form, request, response);
}
I can see update statements in my Hibernate logs, but nothing is actually saved. I logged the value of my selected
field, and actually, it's the initial value, before I changed it in the displayed form. Kind of puzzling...
我可以在 Hibernate 日志中看到更新语句,但实际上没有保存任何内容。我记录了我的selected
字段的值,实际上,这是我在显示的表单中更改它之前的初始值。有点令人费解...
Edit:
编辑:
Actually, the <html:checkbox name="boutique" property="selected" value="id" />
appears in the final HTML as <input type="checkbox" name="selected" value="id">
, with "id" instead of the id value. But I don't know how to solve this...
实际上,<html:checkbox name="boutique" property="selected" value="id" />
在最终的 HTML 中显示为<input type="checkbox" name="selected" value="id">
,带有“id”而不是 id 值。但我不知道如何解决这个问题...
回答by Susie
The following is something that you should be doing. struts-config.xml file the configuration file that glues everything together.
以下是您应该做的事情。struts-config.xml 文件将所有内容粘合在一起的配置文件。
parameter ---> which method to call
name---> name of the action form
path---> URL
type---> action class that is invoked by the above url.
forward---->where to forward once it's done.
As you can see I set the list in myMethod() and access in the jsp below.
如您所见,我在 myMethod() 中设置了列表并在下面的 jsp 中访问。
JSP page:
JSP页面:
<form name="myForm" action="myAction.do" method="post">
<logic:notEmpty name="myForm" property="myList">
<logic:iterate id="boutique" name="myForm" property="myList" type="com.Boutique">
<tr>
<td><bean:write name="boutique" property="id" /> </td>
<td><bean:write name="boutique" property="name" /></td>
<td><bean:write name="boutique" property="selected" /></td>
</tr>
</logic:iterate>
</logic:notEmpty>
</form>
In my action class method:
在我的动作类方法中:
public ActionForward myMethod(ActionMapping actionMapping,
ActionForm myForm, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws Exception {
List<Boutique> myList = new ArrrayList<Boutique>();
form.setMyList(myList);
return mapping.findForward("success");
}
struts-config.xml
struts-config.xml
<struts-config>
<form-beans>
<form-bean name="myForm" type="com.usps.nom.tops.web.struts.form.transportation.ITransportationInquiryDynaForm">
</form-bean>
</form-beans>
<action-mappings>
<action path="/myAction" type="com.MyAction"
name="myForm" scope="session" validate="false"
parameter="myMethod">
<forward name="success" path="tile.view"></forward>
</action>
</action-mappings>
</struts-config>
回答by Alexis Dufrenoy
Finally, I didn't use the Struts html:checkbox tag at all. After some research, it appears Struts 1 is not handling checkboxes very well...
最后,我根本没有使用 Struts html:checkbox 标签。经过一些研究,似乎 Struts 1 没有很好地处理复选框......
That's my current JSP:
这是我目前的 JSP:
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<html:errors/>
<html:form action="/admin/boutiqueviparis">
<input type="hidden" name="reqCode" value="sauverBoutiques" />
<table class="tableStatic noHead">
<logic:notEmpty name ="viparisListeBoutiquesForm" property="boutiques">
<logic:iterate id="boutique" name ="viparisListeBoutiquesForm" property="boutiques">
<tr>
<td>
<html:hidden name="boutique" property="id" />
<bean:write name="boutique" property="nom" />
</td>
<td>
<logic:equal name="boutique" property="selected" value="true">
<input type="checkbox" name="boutique_<bean:write name="boutique" property="id" />" value="<bean:write name="boutique" property="id" />" checked="1" />
</logic:equal>
<logic:equal name="boutique" property="selected" value="false">
<input type="checkbox" name="boutique_<bean:write name="boutique" property="id" />" value="<bean:write name="boutique" property="id" />" />
</logic:equal>
</td>
</tr>
</logic:iterate>
<html:submit styleClass="boutonrouge" value="Valider" onclick="this.form.reqCode.value='sauverBoutiques';return window.confirm('Modification validée');"/>
</logic:notEmpty>
</table>
</html:form>