民间,
我正在使用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>
我已将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;
}
问题界面:
@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;
....
}
同样,我有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
我知道它无法实例化任何接口。所以我更改了Question.java和QuestionImpl.java中的AnswerImpl替换了Answer。这解决了这个问题。但它听起来不像是在接口中具体实现的好解决方案。
有没有办法解决这个错误?
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 :(得分:0)
我有类似的问题,我可以通过将空的公共构造函数添加到Answer
和TagsImpl
public UserEntity(){}
public TagsImpl(){}