问题描述
我有一个页面jsp,我将使用不同POJO类的许多属性,所以我需要在表单中使用两个commandName。
可以在控制器中使用多个@ModelAttribute,那么如果多个commandName不工作又有什么意义呢?
例如我想插入一个信息,名称和功能,name是类Agent中的一个属性,function是类Activity中的一个属性?我该怎么办?
控制器
@RequestMapping(value =/ fiche_service ,method = RequestMethod.GET)
public ModelAndView Fiche_service(@ModelAttribute Activite activitey,@ ModelAttribute Etablissement etabl,ModelMap model){
Agent ag = new Agent();
返回新的ModelAndView(FicheService,agent,ag);
}
表格
< form:form
action =$ {pageContext.request.contextPath} / ajouter_activite
method =postcommandName =activity commandName =etabl>
< table id =tabmenu>
< tr>
< td> Fonction:< / td>
< td>< form:input type =textclass =round default-width-inputpath =fonction/>< / td>
< / tr>
< tr>
< td> Nom d'établissement:< / td>
< td>< form:input type =textclass =round default-width-inputpath =noml/>< / td>
< / tr>
< tr>
< td> Ville:< / td>
< td>< form:input type =textclass =round default-width-inputpath =villel/>< / td>
< / tr>
< tr>
< td>Délégation:< / td>
< td>< form:input type =textclass =round default-width-inputpath =cd_reg< / td>
< / tr>
< tr>
< td>日期首字母:< / td>
< td>< form:input type =textname =dateclass =tcalvalue =path =dateAffect/>< / td>
< / tr>
< tr>
< td>日期:< / td>
< td>< form:input type =textname =dateclass =tcalvalue =path =dateAffect_etab/>< / td>
< / tr>
< tr>
< td>< input class =button round blue-right ic-right-arrow
type =submitvalue =Créer/>< / td>
< td>< input class =button round blue-right ic-right-arrow
type =resetvalue =Initialiser/>< / td>
< / tr>
< / table>
< / form:form>
例外:
Etat HTTP 500 - /WEB-INF/pages/FicheService.jsp(第397行,第64列)属性限定名在元素
第397行==>
method =post commandName =activitycommandName =etabl>
解决方案不可能有多个
commandName
带有springform:form
标签的属性。
(实现
org.springframework.web.servlet.tags.form.FormTag
只有一个字段来保存此值。)
最简单的解决方案(无效工作)将使用包装器命令对象,它必须包含字段。
public class CombinedCommand() {
活动活动;
Etabl etabl;
// Getter和setter
}
jsp:
< form:form
action =$ {pageContext.request.contextPath} / ajouter_activite
method =post commandName =combinedCommand>
...
< form:input type =textpath =activity.x/>
.. 。
< form:input type =textpath =etabl.y/>
...
< / form:form>
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 ??
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?
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 ==>
method="post" commandName="activity" commandName="etabl">
解决方案It is impossible to have multiple
commandName
attributes with aspringform:form
tag.(The implementation
org.springframework.web.servlet.tags.form.FormTag
has only a single field to hold this value).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:
<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>
这篇关于Spring MVC中一个表单的许多commandName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!