0

I would like to save an object C from a form. This form contains of two select drop downs each represents class A and class B. Class C has a connection to class A and class A has a list connection to class B.

When I save object C from form I choose one item from select drop down class A and one item from class B. The new ID of C and the choosen ID of A is saved to a db table db_C and choosen ID of B is supposed to be saved together with newly created ID of C and a new ID of D to a db table db_D which contains connections to B and C.

Table db_A

__A__
  1  
  2  
  3  

Table db_B

__B__|__C__
  4  |  2
  5  |  1

Table db_C

__C__|__A__
  1  |  2
  2  |  3

Table db_D

__D__|__B__|__C__
  1     2     1

I have a controller of 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";
}

And my form of 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>

The problem is that when I save I get an empty list of Bs. I think it's because C doesn't have a direct reference to B. But I'm not sure. I really need help with this! How do get selected value of B into the save-function??

I've tried these two links with no result

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

I didn't get the names and id's into the optiontag.

The goal in this is to choose A and B in the form for C and on save I'd like to save all values I enter in form to db_C and db_D respectivly.

I hope someone can help me :)

I edited the question because the first one was a bit confusing.

Community
  • 1
  • 1
user2018311
  • 105
  • 2
  • 13

1 Answers1

0

I created a new class AFormatter with a print and a parse function. That did the trick!

user2018311
  • 105
  • 2
  • 13