0

у меня есть класс Patent

public class Patent {

private Integer id;
private Set<Participant> participants = new HashSet<Participant>();
private Date date;
private String patentAgency;
private String patentOwner;
private String regNumber;

@Override
public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    if (!(o instanceof Patent)) {
        return false;
    }
    return this.id == ((Patent) o).getId();
}

@Override
public int hashCode() {
    return id.hashCode();
}

public Patent() {
}//...constructosr, getter,setters, tostring}

Есть класс участников патента:

public class Participant implements Serializable {

private Integer id;
private String lastName;
private String firstName;
private String patronymic;

@Override
public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    if (!(o instanceof Participant)) {
        return false;
    }
    return this.id == ((Participant) o).getId();
}

@Override
public int hashCode() {
    return new Integer(id).hashCode();
}

public Participant() {
}//...constructosr, getter,setters, tostring}

Я пытаюсь, на одной странице создать патент и причислить к нему участников вытащенных из бд. Страница jsp

<body>
        <h1>Add patent</h1>
        <div class="container">
            <div class="row">
                <spring:url value="add_patent" var="formURL"/>
                <form:errors path="patent" cssClass="error"/>
                <form:form action="${formURL}"
                           method="post" cssClass="col-md-8 col-md-offset-2"
                           modelAttribute="patent"
                           >
                           <!--...-->
                           <!--формы ввода полей патента-->
                           <!--...-->
                           <table class="table table-hover">
                        <tbody>
                            <tr>
                                <th>Firstname</th><th>Lastname</th><th>Patronymic</th>
                            </tr>
                            <c:forEach items="${participants_to_add}" var="participant">
                                <tr>
                                    <td><form:checkbox path="participants" value="${participant}" label="${participant.id}" /></td>
                                    <td>${participant.firstName}</td>
                                    <td>${participant.lastName}</td>
                                    <td>${participant.patronymic}</td>              
                                </tr>            
                            </c:forEach>

                        </tbody>
                    </table>

                    <button type="submit" class="btn btn-default">Create patent</button>
                </form:form>
            </div>
        </div>
    </body>

Методы контроллера:

@RequestMapping(value = "/add_patent", method = RequestMethod.GET)
public String addPatent(Model model) {
    model.addAttribute("patent", new Patent());
    model.addAttribute("participants_to_add", dbService.findParticipants();
    return "add_patent";
}

@RequestMapping(value = "/add_patent", method = RequestMethod.POST)
public String savePatent(/*@Valid*/@ModelAttribute("patent") Patent patent, Errors errors, Model model) {
    System.out.println("post");
    if (errors.hasErrors()) {
        System.out.println("Has errors");
        System.out.println(errors);
        model.addAttribute("patent", patent);
        model.addAttribute("participants_to_add", dbService.findParticipants());
        return "add_patent";
    } else {
        dbService.addEvent(patent);
        return "add_patent";
    }
}

И в errors мне пишет:

Field error in object 'patent' on field 'participants': rejected value [Participant{id=num, lastName=data, firstName=data, patronymic=data}]; codes [typeMismatch.patent.participants,typeMismatch.participants,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [patent.participants,participants]; arguments []; default message [participants]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Set' for property 'participants'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [model.Participant] for property 'participants[0]': no matching editors or conversion strategy found]

То-есть пришёл лист объектов, чекнутые из них после сабмита ушли обратно, но уже как массив строк... когда нужна коллекция set, чего ещё я не сделал? Может, мне нужен InitBiner для этого, но как его написать я не представляю.

1 ответ 1

0

нашёл способ на javatalks. зарегистрировать парсер в initBiner, а в качестве валуева положить не весь объект, а только id'шники http://javatalks.ru/topics/38429

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.