many commandName for one form in Spring MVC

后端 未结 1 755
慢半拍i
慢半拍i 2021-01-15 03:44

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

相关标签:
1条回答
  • 2021-01-15 04:25

    It is impossible to have multiple commandName attributes with a springform: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>
    
    0 讨论(0)
提交回复
热议问题