【问题标题】:many commandName for one form in Spring MVCSpring MVC中一种形式的许多commandName
【发布时间】:2013-05-23 11:14:27
【问题描述】:

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

例如我想插入一个信息,名称和函数,名称是类 Agent 中的一个属性,而函数是类 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">

【问题讨论】:

  • 在问之前你试过了吗? stackoverflow.com/about "获取实用、详细问题的答案"
  • 是的!我试过了,但它不起作用。我问是否有其他方法可以做到这一点。
  • 请发布您尝试过的内容:(jsp 和控制器)并解释究竟是什么不起作用。 -- 因为我希望有办法让它运行。
  • 好的,我编辑我的问题,见上面的代码

标签: java spring spring-mvc


【解决方案1】:

不可能有多个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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    相关资源
    最近更新 更多