- <span style=“font-size:14px;”>
- </span>
<span style="font-size:14px;">
</span>
- <strong><span style=“font-size:14px;”>RedirectAttributes对象addFlashAttribute()的使用</span></strong>
<strong><span style="font-size:14px;">RedirectAttributes对象addFlashAttribute()的使用</span></strong>
- <span style=“font-size:14px;”><span style=“font-size:18px;”>@RequestMapping(value=“/edit/{id}”,method= RequestMethod.GET)
- public String edit(@PathVariable(value=“id”) int id,ModelMap map,RedirectAttributes redirectAttributes) {
- PartsChange change = partsChangeService.findOne(id);
- PartsChangeFB fb = PartsChangeFB.toFB(change);
- if(!PartsChange.STATUS_NEW.equals(fb.getStatus())){
- redirectAttributes.addFlashAttribute(”errorInfo”,“不能修改!”);
- return “redirect:/partsChange”;
- }
- List<PartsChangeFile> fileList = partsChangeFileService.findByChangeId(id);
- map.addAttribute(”partsChange”, fb);
- map.addAttribute(”fileList”, fileList);
- return “partsChange/editInfo”;
- }</span></span>
<span style="font-size:14px;"><span style="font-size:18px;">@RequestMapping(value="/edit/{id}",method= RequestMethod.GET)
public String edit(@PathVariable(value="id") int id,ModelMap map,RedirectAttributes redirectAttributes) {
PartsChange change = partsChangeService.findOne(id);
PartsChangeFB fb = PartsChangeFB.toFB(change);
if(!PartsChange.STATUS_NEW.equals(fb.getStatus())){
redirectAttributes.addFlashAttribute("errorInfo","不能修改!");
return "redirect:/partsChange";
}
List<PartsChangeFile> fileList = partsChangeFileService.findByChangeId(id);
map.addAttribute("partsChange", fb);
map.addAttribute("fileList", fileList);
return "partsChange/editInfo";
}</span></span>
ModelMap对象addAttribute()的使用
- <span style=“font-size:14px;”><span style=“font-size:14px;”>@RequestMapping(value = “/save”, method = RequestMethod.POST)
- public String save(@ModelAttribute @Valid EquipmentFB equipmentFB, BindingResult result, ModelMap map) {
- if (result.hasErrors()) {
- map.addAttribute(”message”, ErrorUtils.fetchAllErrorMessages(result));
- if(equipmentFB.getEquipTypeId() != null && equipmentFB.getEquipTypeId() != 0){
- EquipType equipType = equipTypeService.findOne(equipmentFB.getEquipTypeId());
- equipmentFB.setEquipTypeName(equipType.getName());
- }
- map.addAttribute(”equipment”, equipmentFB);
- return “/nbs/equipment/edit”;
- }
- String error = ”“;
- if(StringUtils.isEmpty(equipmentFB.getNumber())){
- error = error +Constant.ERROR_EQUIPMENTCONTROLLER_NUMBER_CANNOT_NULL;
- }
- if(StringUtils.isEmpty(equipmentFB.getName())){
- error = error +Constant.ERROR_EQUIPMENTCONTROLLER_NAME_CANNOT_NULL;
- }
- if(equipmentFB.getEquipTypeId() == null || equipmentFB.getEquipTypeId() == 0){
- error = error +Constant.ERROR_EQUIPMENTCONTROLLER_TYPE_CANNOT_NULL;
- }
- if (!“”.equals(error)) {
- map.addAttribute(”message”, error);
- if(equipmentFB.getEquipTypeId() != null && equipmentFB.getEquipTypeId() != 0){
- EquipType equipType = equipTypeService.findOne(equipmentFB.getEquipTypeId());
- equipmentFB.setEquipTypeName(equipType.getName());
- }
- map.addAttribute(”equipment”, equipmentFB);
- return “/nbs/equipment/edit”;
- }
- Equipment equipment = new Equipment();
- if (equipmentFB.getId() != null) {
- equipment = equipmentService.getEquipment(equipmentFB.getId());
- // machineFB.setVersion(machineEntity.getVersion());//修改时不获得版本号,copy时会报错
- }
- BeanUtils.copyProperties(equipmentFB, equipment);
- if (equipmentFB.getEquipTypeId() != null && equipmentFB.getEquipTypeId() != 0) {
- equipment.setEquipType(equipTypeService.getEquipType(equipmentFB.getEquipTypeId()));
- }
- try {
- equipmentService.save(equipment);
- } catch (DataIntegrityViolationException e) {
- map.addAttribute(”message”, Constant.MESSAGE_EQUIPMENTCONTROLLER_NUMBER_EXEIT);
- if(equipmentFB.getEquipTypeId() != null && equipmentFB.getEquipTypeId() != 0){
- EquipType equipType = equipTypeService.findOne(equipmentFB.getEquipTypeId());
- equipmentFB.setEquipTypeName(equipType.getName());
- }
- map.addAttribute(”equipment”, equipmentFB);
- return “/nbs/equipment/edit”;
- }
- return “redirect:/nbs/equipment”;
- }</span></span>
<span style="font-size:14px;"><span style="font-size:14px;">@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute @Valid EquipmentFB equipmentFB, BindingResult result, ModelMap map) {
if (result.hasErrors()) {
map.addAttribute("message", ErrorUtils.fetchAllErrorMessages(result));
if(equipmentFB.getEquipTypeId() != null && equipmentFB.getEquipTypeId() != 0){
EquipType equipType = equipTypeService.findOne(equipmentFB.getEquipTypeId());
equipmentFB.setEquipTypeName(equipType.getName());
}
map.addAttribute("equipment", equipmentFB);
return "/nbs/equipment/edit";
}
String error = "";
if(StringUtils.isEmpty(equipmentFB.getNumber())){
error = error +Constant.ERROR_EQUIPMENTCONTROLLER_NUMBER_CANNOT_NULL;
}
if(StringUtils.isEmpty(equipmentFB.getName())){
error = error +Constant.ERROR_EQUIPMENTCONTROLLER_NAME_CANNOT_NULL;
}
if(equipmentFB.getEquipTypeId() == null || equipmentFB.getEquipTypeId() == 0){
error = error +Constant.ERROR_EQUIPMENTCONTROLLER_TYPE_CANNOT_NULL;
}
if (!"".equals(error)) {
map.addAttribute("message", error);
if(equipmentFB.getEquipTypeId() != null && equipmentFB.getEquipTypeId() != 0){
EquipType equipType = equipTypeService.findOne(equipmentFB.getEquipTypeId());
equipmentFB.setEquipTypeName(equipType.getName());
}
map.addAttribute("equipment", equipmentFB);
return "/nbs/equipment/edit";
}
Equipment equipment = new Equipment();
if (equipmentFB.getId() != null) {
equipment = equipmentService.getEquipment(equipmentFB.getId());
// machineFB.setVersion(machineEntity.getVersion());//修改时不获得版本号,copy时会报错
}
BeanUtils.copyProperties(equipmentFB, equipment);
if (equipmentFB.getEquipTypeId() != null && equipmentFB.getEquipTypeId() != 0) {
equipment.setEquipType(equipTypeService.getEquipType(equipmentFB.getEquipTypeId()));
}
try {
equipmentService.save(equipment);
} catch (DataIntegrityViolationException e) {
map.addAttribute("message", Constant.MESSAGE_EQUIPMENTCONTROLLER_NUMBER_EXEIT);
if(equipmentFB.getEquipTypeId() != null && equipmentFB.getEquipTypeId() != 0){
EquipType equipType = equipTypeService.findOne(equipmentFB.getEquipTypeId());
equipmentFB.setEquipTypeName(equipType.getName());
}
map.addAttribute("equipment", equipmentFB);
return "/nbs/equipment/edit";
}
return "redirect:/nbs/equipment";
}</span></span>