春季MVC | 无法实例化属性类型以自动增长嵌套属性路径

va1*_*4av 4 java spring spring-mvc thymeleaf

各位,

我正在使用 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>
</form>
Run Code Online (Sandbox Code Playgroud)

我已将其暴露question为表单支持对象。

@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;
}
Run Code Online (Sandbox Code Playgroud)

提问界面:

@Component
public interface Question {

    String getQuestionStr();

    void setQuestionStr(String question);

    ....
}
Run Code Online (Sandbox Code Playgroud)

问题实施:

@Component
public class QuestionImpl implements Question {

    private String questionStr;

    private Answer answerOptions;

    private Answer correctAnswer;

    private Set<TagsImpl> tags;

    ....
}
Run Code Online (Sandbox Code Playgroud)

同样,我有Answer接口和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
Run Code Online (Sandbox Code Playgroud)

据我了解它无法实例化任何接口。所以我在 Question.java 和 QuestionImpl.java 中将 Answer 替换为 AnswerImpl。这解决了问题。但这听起来并不是一个在接口中具有具体实现的好解决方案。

有什么办法可以解决这个错误吗?

弹簧配置:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

答案.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);
}
Run Code Online (Sandbox Code Playgroud)

tk_*_*tk_ 6

我遇到了类似的问题,我可以通过添加空的公共构造函数Answer修复它TagsImpl

public UserEntity(){}

public TagsImpl(){}
Run Code Online (Sandbox Code Playgroud)