视图中表之间的Spring关系

时间:2014-06-05 12:22:53

标签: spring eclipselink

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

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

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

我创建了两个相关的表,一个名为" Agency"另一个名为"登录"。一个"代理商"可能包含一个或多个"登录" (@ OneToMany),但问题是视图创建时间,因为来自两个表的数据将组成一个表单。通过一些研究,我注意到我的表单中不能有两个modelAttribute。

我为英语中的错误道歉。

祝你好运!

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

感谢您的回复。

但不是吗:(

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

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

在我看来,我有注释&#34; bind&#34; 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);  
}  

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

谢谢