java Spring MVC 中一种形式的许多 commandName
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16712462/
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
many commandName for one form in Spring MVC
提问by Souad
I have a page jsp where i will use many attributes from differents POJO Classes so I need to use two commandName in the form. It's possible to user multiple @ModelAttribute in the controller so what's the point if multiple commandName is not working ??
我有一个页面 jsp,我将在其中使用来自不同 POJO 类的许多属性,因此我需要在表单中使用两个 commandName。可以在控制器中使用多个 @ModelAttribute 那么如果多个 commandName 不起作用有什么意义?
For example I want to insert an information, name and function, name is an attribute in class Agent and function is an attribute in class Activity ? what should I do?
例如,我想插入一个信息、名称和函数,名称是 Agent 类中的一个属性,而函数是 Activity 类中的一个属性?我该怎么办?
The controller
控制器
@RequestMapping(value="/fiche_service",method=RequestMethod.GET)
public ModelAndView Fiche_service(@ModelAttribute Activite activitey,@ModelAttribute Etablissement etabl,ModelMap model) {
Agent ag = new Agent();
return new ModelAndView("FicheService","agent",ag);
}
The form
表格
<form:form
action="${pageContext.request.contextPath}/ajouter_activite"
method="post" commandName="activity" commandName="etabl">
<table id="tabmenu">
<tr>
<td>Fonction :</td>
<td><form:input type="text" class="round default-width-input" path="fonction"/></td>
</tr>
<tr>
<td>Nom d'établissement :</td>
<td><form:input type="text" class="round default-width-input" path="noml"/></td>
</tr>
<tr>
<td>Ville :</td>
<td><form:input type="text" class="round default-width-input" path="villel"/></td>
</tr>
<tr>
<td>Délégation :</td>
<td><form:input type="text" class="round default-width-input" path="cd_reg"</td>
</tr>
<tr>
<td>Date début :</td>
<td><form:input type="text" name="date" class="tcal" value="" path="dateAffect"/></td>
</tr>
<tr>
<td>Date fin :</td>
<td><form:input type="text" name="date" class="tcal" value="" path="dateAffect_etab"/></td>
</tr>
<tr>
<td><input class="button round blue image-right ic-right-arrow"
type="submit" value="Créer" /></td>
<td><input class="button round blue image-right ic-right-arrow"
type="reset" value="Initialiser" /></td>
</tr>
</table>
</form:form>
The Exception:
例外:
Etat HTTP 500 - /WEB-INF/pages/FicheService.jsp (line: 397, column: 64) Attribute qualified names must be unique within an element
line 397 ==>
第 397 行 ==>
method="post" commandName="activity" commandName="etabl">
回答by Ralph
It is impossible to have multiple commandName
attributes with a springform:form
tag.
commandName
一个springform:form
标签不可能有多个属性。
(The implementation org.springframework.web.servlet.tags.form.FormTag
has only a single field to hold this value).
(实现org.springframework.web.servlet.tags.form.FormTag
只有一个字段来保存这个值)。
The easiest solution (that defently works) would be using a wrapper command objects, that has to fields.
最简单的解决方案(有效地工作)是使用包装器命令对象,它必须是字段。
public class CombinedCommand() {
Activity activity;
Etabl etabl;
//Getter and setter
}
jsp:
jsp:
<form:form
action="${pageContext.request.contextPath}/ajouter_activite"
method="post" commandName="combinedCommand"">
...
<form:input type="text"path="activity.x"/>
...
<form:input type="text"path="etabl.y"/>
...
</form:form>