powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / Spring MVC + Binding List + @Initbinder(CustomPropertyEditor)
2 сообщений из 2, страница 1 из 1
Spring MVC + Binding List + @Initbinder(CustomPropertyEditor)
    #37808884
grief
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Вопрос как можно привязать CustomPropertyEditor при передаче листа
Контролер
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
@Controller
public class GuestController {

    @Autowired
    CRUDDAO cruddDAO;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(GuestType.class, "guestType", new GuestTypeEditor(cruddDAO));
    }

    ................................

    @RequestMapping(value = "guest_list/add/save", method = RequestMethod.POST)
    public String addGuestSave(@ModelAttribute("guestList") GuestListWrapper guests, BindingResult result, SessionStatus status) {

        if (guests != null) {
            for (Guest guest : guests.getGuests()) {
                cruddDAO.saveOrUpdate(guest);
            }
        }
        return "redirect:/guest_list";
    }
}


Врапер
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
public class GuestListWrapper {
    
    private List<Guest> guests = LazyList.decorate(new ArrayList(),
      FactoryUtils.instantiateFactory(Guest.class));

    public List<Guest> getGuests() {
        return guests;
    }

    public void setGuests(List<Guest> guests) {
        this.guests = guests;
    }
}


Editor
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
public class GuestTypeEditor extends PropertyEditorSupport {

    private CRUDDAO guestService;

    public GuestTypeEditor() {
    }

    
    public GuestTypeEditor(CRUDDAO guestService) {
        this.guestService = guestService;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        GuestType type = (GuestType) guestService.get(GuestType.class, Integer.parseInt(text));
        this.setValue(type);
    }

    @Override
    public String getAsText() {
        GuestType guest = (GuestType) this.getValue();
        String temp = "";
        if (guest != null) {
            temp = guest.getId() != null ? guest.getId().toString() : "";
        }
        return temp;
    }
}



До того как в контролер не был добавлен BindingResult выдавало ошибку что не может привести тип String к типу GuestType, обычно это решается с помощью кастомного редактора, но тут почемуто он не срабатывает? guestList.guest[0].guestType но при передаче в контролер просто обьекта guest все работает как положенно.
...
Рейтинг: 0 / 0
Spring MVC + Binding List + @Initbinder(CustomPropertyEditor)
    #37808903
grief
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
проблема решена, может кому пригодиться
при регистрации
Код: java
1.
2.
3.
4.
@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(GuestType.class, "guestType", new GuestTypeEditor(cruddDAO));
    }


не нужно указывать имя колонки должно быть так
Код: java
1.
2.
3.
4.
@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(GuestType.class, new GuestTypeEditor(cruddDAO));
    }
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / Spring MVC + Binding List + @Initbinder(CustomPropertyEditor)
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]