Spring/MVC Thymeleaf th:selected not working

前端 未结 5 2067
盖世英雄少女心
盖世英雄少女心 2021-02-09 14:34

I am building an application using Spring, Hibernate and of course Thymeleaf for the views. The application is centered around an Activity domain class (which is literally an i

5条回答
  •  再見小時候
    2021-02-09 14:53

    I know its a bit late,. but anyone searching for the solution...

    To use the *{fieldname} select option you should use the th:value with 2 curly brackets

    
    

    You need to add a formatter:

    public class RoadmapFormatter implements Formatter {
        @Override
        public Roadmap parse(String id, Locale locale) throws ParseException {
            Roadmap r = new Roadmap();
            r.setId(Long.parseLong(id));
            return r;
        }
    
        @Override
        public String print(Roadmap r, Locale locale) {
            return String.valueOf(r.getId());
        }
    }
    

    Last but not least register the formatter in the configuration class:

    @Configuration
    public class Configuration implements WebMvcConfigurer {
    
        @Override
        public void addFormatters(FormatterRegistry formatterRegistry) {
            formatterRegistry.addFormatter(new RoadmapFormatter());
        }
    }
    

提交回复
热议问题