Spring MVC中一种形式的许多commandName


问题内容

我有一个页面jsp,在这里我将使用来自不同POJO类的许多属性,因此我需要在表单中使用两个commandName。可以在控制器中使用多个@ModelAttribute,所以如果多个commandName不起作用,那有什么意义?

例如,我要插入信息,名称和函数,名称是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();
        return new ModelAndView("FicheService","agent",ag);

    }

表格

<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>

例外:

Etat HTTP 500 - /WEB-INF/pages/FicheService.jsp (line: 397, column: 64) Attribute qualified names must be unique within an element

397行==>

method="post" commandName="activity" commandName="etabl">

问题答案:

commandName带有springform:form标签的多个属性是不可能的。

(该实现org.springframework.web.servlet.tags.form.FormTag只有一个字段来保存此值)。

最简单的解决方案(可以有效地工作)将使用包装器命令对象,该对象必须具有字段。

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>