- 在controller 中需要指定 sessionAttribute的key
@sessionattributes注解应用到Controller上面,可以将Model中的属性同步到session当中。
当需要清除session当中的值得时候,我们只需要在controller的方法中传入一个SessionStatus的类型对象 通过调用setComplete方法就可以清除了。
@RequestMapping(params=”method=index3”)
public ModelAndView index4(SessionStatus status) {
ModelAndView mav = new ModelAndView(“success.jsp”);
status.setComplete();
return mav;
}
@Controller("com.ninelephas.meerkat.controller.property.PropertyController")
@RequestMapping("/properties")
@SessionAttributes("property_template")
public class PropertyController {
...
}
- 在方法中通过 ModelMap model 来设置对象给jsp显示。其中model.put的key必须和SessionAtrributes的key 一致
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String create(ModelMap model) throws Exception {
logger.debug("create(Model) - start"); //$NON-NLS-1$
PropertyTemplate propertyTemplate = new PropertyTemplate();
propertyTemplate.setId(UUID.randomUUID().toString());
propertyTemplate.setCreatedBy("-99999");
propertyTemplate.setUpdatedBy("-99999");
model.put("property_template", propertyTemplate);
logger.debug("新建立的对象是:"+ new JsonUtilsHelper().ObjectToJsonString(model));
logger.debug("create(Model) - end"); //$NON-NLS-1$
return "properties/new";
}
- 前台jsp通过数据绑定来显示,并且不需要为了传回值,使用input hidden的方法来设置不需要显示的内容。
<form id="new_form" onsubmit="javascript:fun_save_create_property()" action="${property_template.id}" method="POST">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">属性名称</span> <input type="text" class="form-control" id="name" name="name" value="${property_template.name}" placeholder="属性名称" required autofocus aria-describedby="basic-addon1">
</div>
<br />
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">描述</span>
<textarea class="form-control" rows="3" name="description" id="description" placeholder="描述">${property_template.description}</textarea>
</div>
<div class="operate-btn-group">
<button type="submit" class="btn btn-success" id="save-btn">
<span class="glyphicon glyphicon-saved" aria-hidden="true"></span> 保存
</button>
<a type="button" class="btn btn-danger" id="cancel-btn" onClick="fun_create_cancel()"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> 放弃
</a>
</div>
</form>
- 在controller里面通过(@ModelAttribute(“property_template”) PropertyTemplate property来获取传回的对象
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String update(@ModelAttribute("property_template") PropertyTemplate property) throws Exception {
logger.debug("update(PropertyTemplate) - start"); //$NON-NLS-1$
logger.debug("上传上来的准备更新的对象是 "+ new JsonUtilsHelper().ObjectToJsonString(property));
property.setUpdatedBy("-99999");
property.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
propertyService.update(property);
logger.debug("update(PropertyTemplate) - end"); //$NON-NLS-1$
return "properties/index";
}