Dropdown value binding in Spring MVC

前端 未结 1 851
情话喂你
情话喂你 2021-01-04 21:49

I am new to Spring MVC. I am writing an app that uses Spring, Spring MVC and JPA/Hibernate I don\'t know how to make Spring MVC set a value coming from a dropdown into a mod

1条回答
  •  我在风中等你
    2021-01-04 22:16

    The trick as you have already noted is to register a custom converter which will convert the id from the drop down into a Custom instance.

    You can write a custom converter this way:

    public class IdToCustomerConverter implements Converter{
        @Autowired CustomerRepository customerRepository;
        public Customer convert(String id) {
            return this.customerRepository.findOne(Long.valueOf(id));
        }
    }
    

    Now register this converter with Spring MVC:

    
    
    
        
           
              
           
        
    
    

    0 讨论(0)
提交回复
热议问题