当我尝试将String值从DropDown菜单转换为BoreholeType对象的实例并注入Borehole对象时,我收到此错误(点击"提交"填写后的形式):
属性id_borehole_type引发异常;嵌套异常是java.lang.NullPointerException
我根据本教程创建了我的网络应用程序的这一部分:http://howtodoinjava.com/spring/spring-mvc/spring-mvc-populate-and-validate-dropdown-example/
我的代码:
1)Borehole.java
@Entity
@Table(name = "BOREHOLE")
public class Borehole {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToOne
BoreholeType id_borehole_type;
//other fields, getters & setters
}
2)BoreholeType.java
@Entity
@Table(name = "BOREHOLE_TYPE")
public class BoreholeType {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String type;
//setters, getters
}
3)BoreholeTypeRepositoryImpl.java
@Repository
@Transactional
public class BoreholeTypeRepositoryImpl implements BoreholeTypeRepository {
@PersistenceContext
private EntityManager em;
//...
@Override
public BoreholeType getBoreholeTypeById(int id) {
TypedQuery<BoreholeType> boreholeTypeQuery = em.createQuery("SELECT b FROM BoreholeType b WHERE b.id = '" + id + "'", BoreholeType.class);
BoreholeType boreholeType = boreholeTypeQuery.getSingleResult();
return boreholeType;
}
}
4)BoreholeTypeServiceImpl.java
@Service
public class BoreholeTypeServiceImpl implements BoreholeTypeService {
@Autowired
private BoreholeTypeRepository boreholeTypeRepository;
@Override
public List<BoreholeType> getBoreholeTypes() {
return boreholeTypeRepository.getAllBoreholeTypes();
}
@Override
public BoreholeType getBoreholeTypeById(int id) {
return boreholeTypeRepository.getBoreholeTypeById(id);
}
//...
}
5)BoreholeTypeEditor.java
public class BoreholeTypeEditor extends PropertyEditorSupport {
@Autowired
private BoreholeTypeService boreholeTypeService;
@Override
public void setAsText(String id) {
System.out.println("ID: " + id);
// <---------------- To this moment everything is fine
BoreholeType boreholeType = boreholeTypeService.getBoreholeTypeById(Integer.parseInt(id));
System.out.println("ID: " + boreholeType.getId() + ", Value: " + boreholeType.getType());
this.setValue(boreholeType);
}
}
6)BoreholeController.java
@Controller
public class BoreholeController {
@Autowired
private BoreholeService boreholeService;
@Autowired
private BoreholeTypeService boreholeTypeService;
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(BoreholeType.class, new BoreholeTypeEditor());
}
@GetMapping("/add_borehole")
public String showBoreholeForm(@ModelAttribute("newBorehole") Borehole newBorehole) {
return "add_borehole";
}
@PostMapping("/add_borehole")
public String processAddNewBoreholeForm(@ModelAttribute("newBorehole") @Valid Borehole boreholeToAdd,
BindingResult bindingResult, HttpServletRequest request) {
if (bindingResult.hasErrors()) {
return "add_borehole";
}
try {
boreholeService.addBorehole(boreholeToAdd);
} catch (Exception e) {
e.printStackTrace();
return "add_borehole";
}
return "borehole";
}
}
7)add_borehole.jsp
<form:form modelAttribute="newBorehole" method="POST"
action="add_borehole">
<table>
<tr>
<td><spring:message text="_____" /></td>
<td><form:select path="id_borehole_type">
<form:options items="${allBoreholeTypes}" itemValue="id" itemLabel="type" />
</form:select></td>
<td><form:errors path="id_borehole_type" cssClass="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="Dodaj odwiert"></td>
</tr>
</table>
</form:form>
应用程序在此行之前正常工作(BoreholeTypeEditor.java):
BoreholeType boreholeType = boreholeTypeService.getBoreholeTypeById(Integer.parseInt(id));
我犯了错误(这很有可能,因为我是Spring Framework的新手)?
答案 0 :(得分:0)
解决此类问题的步骤(使用转换器):
1)创建一个新的转换器类:
public class StringToBoreholeTypeConverter implements Converter<String, BoreholeType> {
@Autowired
private BoreholeTypeService boreholeTypeService;
@Override
public BoreholeType convert(String source) {
if (source == null || source.isEmpty()) {
return null;
}
BoreholeType boreholeType = boreholeTypeService.getBoreholeTypeById(Integer.parseInt(source));
return boreholeType;
}
}
2)将转换器bean添加到上下文文件中:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.example.converter.StringToBoreholeTypeConverter" />
</set>
</property>
</bean>