我在Eclipse Juno中创建了Spring MVC项目。控制器如图所示:
Error evaluating generator expression:
$<TARGET_LINKER_FILE:CONAN_PKG::sqlite3>
Target "CONAN_PKG::sqlite3" is not an executable or library.
表格如下所示:
@Controller
public class JftController {
@Autowired
JenisJftManager jenisJftManager;
@Autowired
JftManager jftManager;
@Autowired
JftValidator jftValidator;
@InitBinder
private void initBinder(WebDataBinder binder){
binder.setValidator(jftValidator);
}
@RequestMapping(value = "/jft/view", method = RequestMethod.GET)
public String getAll(Model model){
model.addAttribute("jftList", jftManager.getAll());
return "jftView";
}
@RequestMapping(value = "/jft/add", method = RequestMethod.GET)
public String formJft(Model model){
Map<Integer, String> jenisJft = jenisJftManager.getAllJenisJftMap();
model.addAttribute("jft", new Jft());
model.addAttribute("jenisJft", jenisJft);
return "jftAdd";
}
@RequestMapping(value = "/jft/add", method = RequestMethod.POST)
public String addJft(@Validated @ModelAttribute("jft") Jft jft, BindingResult bindingResult, Model model, final RedirectAttributes redirectAttributes){
System.out.println("1");
System.out.println("2");
if (bindingResult.hasErrors()){
System.out.println("2.1");
Map<Integer, String> jenisJft = jenisJftManager.getAllKelasMap();
model.addAttribute("jenisJft", jenisJft);
for (Object object : result.getAllErrors()) {
if(object instanceof FieldError) {
FieldError fieldError = (FieldError) object;
System.out.println("FieldError = "+fieldError.getCode());
}
if(object instanceof ObjectError) {
ObjectError objectError = (ObjectError) object;
System.out.println("ObjectError = "+objectError.getCode());
}
}
return "jftAdd";
} else {
System.out.println("2.2");
jftManager.add(jft);
redirectAttributes.addFlashAttribute("message", "Jabatan Fungsional Berhasil ditambah!");
return "redirect:/jft/view";
}
}
}
Jft模型如下所示:
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Untitled</title>
<link rel="stylesheet" type="text/css" href="<c:url value='/resources/css/main.css' />">
<link rel="stylesheet" type="text/css" href="<c:url value='/resources/css/font-awesome.min.css' />">
<link rel="icon" type="image/png" sizes="16x16" href="<c:url value='/resources/images/favicon.png' />">
</head>
<body class="app sidebar-mini rtl">
<form:form method="POST" class="form-horizontal" action="" modelAttribute="jft">
<div class="tile-body">
<div class="form-group row">
<label class="control-label col-md-3">Nama Jabatan Fungsional</label>
<div class="col-md-8">
<form:input path="name" class="form-control" cssErrorClass="form-control is-invalid" maxlength="60" />
<form:errors path="name" class="form-control-feedback" />
</div>
</div>
<div class="form-group row">
<label class="control-label col-md-3">Kelas Jabatan</label>
<div class="col-md-8">
<form:select path="jenis" items="${jenisJft}" class="form-control" />
</div>
</div>
</div>
<div class="tile-footer">
<div class="row">
<div class="col-md-8 col-md-offset-3">
<button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-lg fa-check-circle"></i>Tambah</button>
</div>
</div>
</div>
</form:form>
</body>
</html>
JftJenis模型如下所示:
public class Jft {
public Integer id;
public String name;
public JenisJft jenis;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public JenisJft getJenis() {
return jenis;
}
public void setJenis(JenisJft jenis) {
this.jenis = jenis;
}
}
当我提交表单时,bindingResult有错误。其实我知道原因。问题是,Spring Validator尝试匹配Jft.jenis的数据类型。从html表单返回的值是Integer,但Jft.jenis不是Integer而是JenisJft。 Apache Tomcat的输出如下所示:
我已经谷歌了,但仍未找到答案。怎么解决这个?如何将此Integer值从html映射到JenisJft?