这里
@RequestParam(value = "image", required = false) MultipartFile image
发现@RequestParam去掉,就报错,
beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]:
Specified class is an interface
而留下这句就不会报错,不知道什么原因。。。
减少到最小
@RequestParam
到这种程度。
@Controller
@RequestMapping("/pets")
@SessionAttributes("pet")
public class EditPetForm {
// ...
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
Pet pet = this.clinic.loadPet(petId);
model.addAttribute("pet", pet);
return "petForm";
}
}
@RequestMapping("/entity")
public ResponseEntity<String> handle(HttpEntity<String> requestEntity)
throws UnsupportedEncodingException {
String requestHeader = requestEntity.getHeaders().getFirst(
"MyRequestHeader");
String requestBody = requestEntity.getBody();
// do something with request header and body
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
return new ResponseEntity<String>("Hello World", responseHeaders,
HttpStatus.CREATED);
}
}
结果如上图
@ModelAttribute注解
可以用在方法声明上面,或者方法参数声明的时候。注意声明在方法之前的@ModelAttribute注解(假设这个方法没有RequestMapping),将会在@RequestMapping定义了的方法之前执行一遍(如果@ModelAttribute和ModelAttribute和@RequestMapping不在同一个方法),然后再执行匹配的方法。如果@ModelAttribute和@RequestMapping在一个方法都存在。不会在其他方法之前执行。
@ModelAttribute("types")
public Collection<String> populatePetTypes() {
List<String> ss = new ArrayList<String>();
return ss;
}
@RequestMapping("/model")
public String processSubmit() {
return "index";
}
如上populatePetTypes方法是没有的RequestMapping,请求
http://127.0.0.1:8081/daowole/test/model,先执行
再执行
修改代码
@ModelAttribute("types")
@RequestMapping("/model1.do")
public Collection<String> populatePetTypes() {
List<String> ss = new ArrayList<String>();
return ss;
}
@RequestMapping("/model")
public String processSubmit() {
return "index";
}
访问http://127.0.0.1:8081/daowole/test/model
直接进入processSubmit,无需先执行populatePetTypes