【问题标题】:Spring relationships between tables in view视图中的表之间的弹簧关系
【发布时间】:2014-06-05 12:22:52
【问题描述】:

好吧,我使用 Spring MVC我是技术初学者)开始了一个新项目,很快我就遇到了一个在互联网,也许是我做错或实施错误问题的原因。

我有一个表格,其中数据将被持久保存在两个不同的表中。

有什么更好的方法来做到这一点?

我创建了两个相关的表,一个名为“Agency”,另一个名为“Login”。 “Agency”可能包含一个或多个“Login”(@OneToMany),但问题需要花费视图创建时间,因为来自两个表的数据将组成一个表单。通过一些研究,我注意到我的表单中不能有两个 modelAttribute。

对于英语中的错误,我深表歉意。

最好的问候!

【问题讨论】:

    标签: spring eclipselink


    【解决方案1】:

    如果映射正确并且代理包含一个或多个登录,您需要做的是在模型和视图中渲染代理,并在您的表单中迭代登录

       <form:form id="foo"
               method="post"
               action="url"
               modelAttribute="agency">
    
            <form:input type="hidden" path="id"/>
    
                    <c:forEach var="login" items="${agency.logins}"
                               varStatus="login_index">
                            <form:input type="hidden" path="login.id" />
                    </c:foreach>
        </form:form>
    

    【讨论】:

      【解决方案2】:

      感谢您的回复。

      但不是吗:(

      我有一个表格,其中数据将被持久保存在两个不同的表中。

      <form class="form-signin" method="post" action="addAgency">  
          <div class="input-group">  
              <span class="input-group-addon entypo-user"></span> 
                  //Table Agency
                  <spring:bind path="tenant.firstName"/>  
                  <input class="form-control" placeholder="Nome"/>
                  //Table Login
                  <spring:bind path="login.email"/>  
                  <input class="form-control" placeholder="Nome"/>  
          </div>  
      
                                   //Rest of my form... 
      
                            </form>  
      

      在我看来,我有注释“绑定”Spring,在互联网上搜索我发现这种方式可以在控制器和视图之间建立连接以持久化两个表。

          @RequestMapping(value = "/", method = RequestMethod.GET)  
          public String home(@ModelAttribute("tenant") Agency tenant, @ModelAttribute("login") Login login, ModelMap map) {  
      
              Agency agency = dashboardFacade.getAgency();  
              map.addAttribute("agency", agency);  
      
      
              if (tenantResolver.isMasterTenant()) {
                  //Here is the problem!!  
                  // Add an attribute in my view of type login and agency, but i don't kwon if it is correct.
                  map.addAttribute("tenant", tenant);  
                  map.addAttribute("login", login);  
                  return "landing/index";  
      
              } else {  
      
                  return "dashboard/home";  
              }  
          }
      

      以下方法保存代理并登录。

      // Add a new agency  
      @RequestMapping(value = "/addAgency", method = RequestMethod.POST)  
      public String addAgency(@ModelAttribute("tenant") Agency agency, @ModelAttribute("login") Login login, Model model, final RedirectAttributes redirectAttributes) {  
          agency = dashboardFacade.addAgency(agency);  
          login = dashboardFacade.addLogin(login);  
          return "redirect:" + getAgencyFullUrl(agency);  
      }  
      

      有什么更好的方法来做到这一点?

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        相关资源
        最近更新 更多