java - Spring MVC 中一种表单有多个 commandName

标签 java spring spring-mvc

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

例如我想插入一个信息,名称和函数,名称是Agent类中的属性,函数是Activity类中的属性?我应该怎么办?

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

    }

表格

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

异常(exception):

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

最佳答案

一个 springform:form 标签不可能有多个 commandName 属性。

(实现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>

关于java - Spring MVC 中一种表单有多个 commandName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16712462/

相关文章:

java - Jsoup 代码不工作

单元测试 spring 服务类中的 java.lang.NullPointerException

java - Spring Boot 在 WebSocket 上使用 Stomp : Handshake failed due to invalid Upgrade header: null

java - 将 PROPAGATION_REQUIRED_NEW 与两个单独的 spring bean 一起使用时的 Oracle 数据库锁定

java - 如何动态绕过 spring-hibernate 中字段的验证

java - 无法在终端中找到 XML 模式命名空间错误的 Spring NamespaceHandler

performance - spring mvc + wildfly + postgresql 性能调优

java - String.replaceAll() 问题

java - 如何计算 Hibernate 查询语言中的行数?

java - 使用mockito测试servlet的NullPointer异常