2014-09-20 4 views
2

Люди,Spring MVC | Не удалось создать тип свойства для автоматического роста пути вложенного свойства

Я использую Spring MVC (4.1.0.RELEASE), и у меня есть эта форма, которую я хочу отобразить пользователю. Вот отрывок для представления Thymeleaf:

<form class="form-login" th:action="@{/admin/question/add}" th:object="${question}" method="post"> 
    <div class="login-wrap"> 
         <input type="text" class="form-control" placeholder="Question" th:field="*{questionStr}" autofocus> 
         <br> 
         <input type="text" class="form-control" placeholder="Option One" th:field="*{answerOptions.optionOne}" autofocus> 
         <br> 
         <input type="text" class="form-control" placeholder="Option Two" th:field="*{answerOptions.optionTwo}" autofocus> 
         <br> 
         <input type="text" class="form-control" placeholder="Option Three" th:field="*{answerOptions.optionThree}" autofocus> 
         <br> 
         <input type="text" class="form-control" placeholder="Option Four" th:field="*{answerOptions.optionFour}" autofocus> 
         <br> 
         <button class="btn btn-theme btn-block" type="submit"><i class="fa fa-lock"></i> Submit</button> 
         <hr> 
        </div> 

Я выставил «вопрос» как объект формы подложки.

@Autowired 
    private Question question; 

    @RequestMapping(value = "add", method = RequestMethod.GET) 
    public ModelAndView add(ModelAndView model) { 
     model.setViewName("admin/add/question"); 
     model.addObject("question", question); 
     return model; 
    } 

Вопрос Интерфейс:

@Component 
public interface Question { 

    String getQuestionStr(); 

    void setQuestionStr(String question); 
    .... 
    .... 
} 

Вопрос реализации:

@Component 
public class QuestionImpl implements Question { 

    private String questionStr; 

    private Answer answerOptions; 

    private Answer correctAnswer; 

    private Set<TagsImpl> tags; 

    .... 
    .... 

} 

Кроме того, у меня есть интерфейс ответа и AnswerImpl.

Теперь проблема я сталкиваюсь, когда я прошу на этой странице, я получаю следующее исключение:

org.springframework.beans.NullValueInNestedPathException: Invalid property 'answerOptions' of bean class [com.xyz.abc.bean.impl.QuestionImpl]: Could not instantiate property type [com.saxena.vaibhav.bean.Answer] to auto-grow nested property path: java.lang.InstantiationException: com.xyz.abc.bean.Answer 

Я понимаю, что не может создать экземпляр любых интерфейсов. Поэтому я заменяю Ответ на AnswerImpl в Question.java и QuestionImpl.java. Это решило проблему. Но это не похоже на хорошее решение, имеющее конкретные реализации в интерфейсах.

Есть ли способ обойти эту ошибку?

Spring Конфигурация:

@Configuration 
@ComponentScan("com.xyz.abc") 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { 

    /** 
    * 
    * @return ServletContextTemplateResolver ServletContextTemplateResolver. 
    */ 
    @Bean 
    @Description("Thymeleaf template resolver for serving HTML 5") 
    public ServletContextTemplateResolver templateResolver() { 
     ServletContextTemplateResolver templateresolver = new ServletContextTemplateResolver(); 
     templateresolver.setPrefix("/WEB-INF/views/"); 
     templateresolver.setSuffix(".html"); 
     templateresolver.setTemplateMode("LEGACYHTML5"); 
     templateresolver.setCacheable(false); 
     return templateresolver; 
    } 

    @Bean 
    @Description("Thymeleaf Template Engine with Spring Integration") 
    public SpringTemplateEngine templateEngine() { 
     SpringTemplateEngine templateEngine = new SpringTemplateEngine(); 
     templateEngine.setTemplateResolver(templateResolver()); 
     return templateEngine; 
    } 

    @Bean 
    @Description("Thymeleaf View Resolver") 
    public ThymeleafViewResolver thymeleafViewResolver(){ 
     ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); 
     viewResolver.setTemplateEngine(templateEngine()); 
     return viewResolver; 
    } 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
    } 

    @Bean(name = "hibernateProperties") 
    public PropertiesFactoryBean hibernateProperties() { 

     PropertiesFactoryBean bean = new PropertiesFactoryBean(); 
     bean.setLocation(new ClassPathResource("properties/hibernate.props")); 
     return bean; 
    } 

Answer.java

@Component 
public interface Answer { 

    String getOptionOne(); 

    void setOptionOne(String optionOne); 

    String getOptionTwo(); 

    void setOptionTwo(String optionTwo); 

    String getOptionThree(); 

    void setOptionThree(String optionThree); 

    String getOptionFour(); 

    void setOptionFour(String optionFour); 
+0

Ваш контекст.xml, пожалуйста? – dieend

+0

@dieend Вы ищете файлы конфигурации Spring? – va1b4av

+0

Да. Или xml config или java config – dieend

ответ

0

У меня был подобный вопрос, и я был в состоянии решить проблему, добавив пустой открытый конструктор для Answer и TagsImpl

public UserEntity(){} 

public TagsImpl(){} 
Смежные вопросы