【问题标题】:Save a main object with a list object that is not in the main object用不在主对象中的列表对象保存主对象
【发布时间】:2014-04-11 09:51:14
【问题描述】:

我想从表单中保存object C。此表单包含两个选择下拉列表,每个下拉列表代表 class Aclass BClass Cclass A 有一个连接,class Aclass B 有一个列表连接。

当我从表单中保存object C 时,我从选择下拉列表class A 中选择一项,从class B 中选择一项。 C 的新 ID 和 A 的选择 ID 保存到数据库表 db_CB 的选择 ID 应该与新创建的 ID C 和新 ID 一起保存D 到 db 表 db_D,其中包含与 BC 的连接。

表 db_A

__A__
  1  
  2  
  3  

表 db_B

__B__|__C__
  4  |  2
  5  |  1

表 db_C

__C__|__A__
  1  |  2
  2  |  3

表 db_D

__D__|__B__|__C__
  1     2     1

我有一个 C 的控制器:

@RequestMapping(value = "/new", method = RequestMethod.GET)
public String add(Model model) {

    model.addAttribute("listOfAs", listOfAs());
    model.addAttribute("C", new C());
    model.addAttribute("listOfBs", listOfBs());

    return "C/edit";
}

private List<B> listOfBs() {
    List<B> listOfBs = new ArrayList<>();
    try {
        listOfBs.addAll(BService.listBs());
    } catch(DataAccessException ex) {
        logger.error(ex.getMessage(), ex);
    }
    return listOfBs;
}

private List<A> listOfAs() {
    List<A> listOfAs = new ArrayList<>();
    try {
        listOfAs.addAll(AService.listAs());
    } catch (DataAccessException ex) {
        logger.error(ex.getMessage(), ex);
    }
    return listOfAs;
}

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String update(Model model, @ModelAttribute("C") C C, BindingResult CResult) {
    C.setA(AService.getA(C.getA().getID()));

    System.out.println(C.getA().getBs().isEmpty()); //Prints "True"

    if (C.getID() == 0) {
        CService.save(C);
    } else {
        CService.update(C.getID(), C);
    }
    return "redirect:/mvc/C/viewList";
}

还有我的 C 形式

<s:url value="/mvc/C/save" var="actionUrl" />
<sf:form method="POST" modelAttribute="C" action="${actionUrl}">
    <fieldset>
        <table>
            <tr>
                <th><label for="A">A:</label></th>
                <td><sf:select path="A.ID">
                        <sf:option value="0">&nbsp;</sf:option>
                        <sf:options items="${listOfAs}" itemLabel="name" itemValue="ID" />
                    </sf:select></td>
            </tr>
            <tr>
                <th><label for="B">B:</label></th>
                <td>
                    <sf:select path="A.listRefToB">
                        <sf:option value="0">&nbsp;</sf:option>
                        <sf:options items="${listOfBs}" itemLabel="name" itemValue="ID" />
                    </sf:select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input id="saveButton" class="right" type="submit" title="Save" value=" [ Save ] " />
                </td>
            </tr>
        </table>
    </fieldset>
</sf:form>

问题是当我保存时,我得到一个空列表Bs。我认为这是因为C 没有直接引用B。但我不确定。我真的需要帮助!如何将B 的选定值放入保存函数中??

我试过这两个链接都没有结果

List<Foo> as form backing object using spring 3 mvc, correct syntax?

How to send list of Objects to View and back to Post method in controller

我没有在选项标签中输入名称和 ID。

这样做的目标是在C 的表单中选择AB,保存时我想将我在表单中输入的所有值分别保存到db_Cdb_D

我希望有人可以帮助我:)

我编辑了这个问题,因为第一个问题有点混乱。

【问题讨论】:

  • 没有人吗?真的吗?这是不可能的,或者我为什么要这样做没有意义?

标签: java jquery spring spring-mvc


【解决方案1】:

我创建了一个带有打印和解析功能的新类 AFormatter。成功了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-11
    • 2013-10-22
    • 2013-04-10
    • 2017-12-09
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多