Spring架构设计-增强MultiActionController
2007-09-14 22:52
323 查看
Spring架构设计-增强MultiActionController
在使用Spring提供的控制器时,AbstractController和SimpleFormController是应用得最多的。 AbstractController是最基本的Controller,可以给予用户最大的灵活性。SimpleFormController则用于典型 的表单编辑和提交。在一个需要增,删,改,查的需求中,增加和修改扩展SimpleFormController完成,删除和查询则扩展 AbstractController完成。
但是像上面那样完成某一业务对象的增,删,改,查,都属于一类相关的业务。把一类相关的操作分布到不同的类去完成,违返“高内聚”的设计原则。这样四个业务操作需要四个类来完成,造成太多的类文件,难以维护和配置。
所以Spring借鉴Struts的DispatchAction提供了类似功能的MultiActionController。可以实现不同的 请求路径对应MultiActionController中的不同方法,这样就可以把相关的操作都在一个类的相关方法中完成。这样使得这个类具有“高内 聚”,也利于系统的维护,还避免了重复代码。增加和修改操作的数据验证逻辑是很相似的,使用MultiActionController后就可以让增加和 修改操作共用一段数据验证逻辑代码。
1. 使用MultiActionController
MultiActionController会使不同的请求映射为不同方法,这里是一个实现用户信息增删改查的例子:
1.1 SampleMultiMethodController实现
代码
public class SampleMultiMethodController extends MultiActionController{
// 用户信息列表view
private static final String userInfoListView = "ehld.sample.getuserinfolist";
//用户信息编辑view
private static final String userFormView = "ehld.sample.userForm";
//提交成功后显示的view
private static final String userSuccessView ="redirect:ehld.sample.getuserinfolist.do";
// 用户信息列表key值
private static final String userInfoListKey = "userInfoList";
// userid
private final String userIdParam = "id";
// 定义业务对象
private SampleAction sampleAction;
public SampleAction getSampleAction() {
return sampleAction;
}
public void setSampleAction(SampleAction sampleAction) {
this.sampleAction = sampleAction;
}
/**
* 功能:获得所有的用户信息<br>
*/
public ModelAndView listUser(HttpServletRequest request,
HttpServletResponse response) throws Exception {
List userInfoList = this.sampleAction.getUserInfoList();
ModelAndView mav = new ModelAndView(userInfoListView);
mav.addObject(this.userInfoListKey,userInfoList);
return mav;
}
/**
* 功能:编辑用户信息<br>
*/
public ModelAndView edtiUser(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String uid = RequestUtils.getStringParameter(request, userIdParam);
UserInfoDTO userInfo = null;
if (!"".equals(uid)) {
userInfo = this.sampleAction.getUserInfo(uid);
}
if (userInfo == null) {
userInfo = new UserInfoDTO();
}
ModelAndView mav = new ModelAndView(this.userFormView, this
.getCommandName(null), userInfo);
return mav;
}
/**
* 功能:保存修改或新增的用户信息<br>
*检查从页面bind的对象,如果userId或userName为空则返回原来的form页面;否则进行保存用户信息操作,返回
*成功页面
*/
public ModelAndView saveUser(HttpServletRequest request,
HttpServletResponse response, UserInfoDTO command) throws Exception {
UserInfoDTO user = (UserInfoDTO) command;
ServletRequestDataBinder binder = new ServletRequestDataBinder(command,
getCommandName(command));
BindException errors = binder.getErrors();
ModelAndView mav = null;
if (user.getUserID() == null || "".equals(user.getUserID())) {
errors.rejectValue("userID", "userIdNull", "用户id不能为空");
}
if (user.getUserName() == null || "".equals(user.getUserName())) {
errors.reject("userNameNull", "用户名不能为空");
}
if (errors.hasErrors()) {
mav = new ModelAndView(this.userFormView, errors.getModel());
} else {
this.sampleAction.saveUserInfo(user);// 保存用户信息
mav = new ModelAndView(this.userSuccessView);
}
return mav;
}
/**
* 功能:删除用户信息<br>
*/
public ModelAndView deleteUser(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String uid = RequestUtils.getStringParameter(request, userIdParam);
UserInfoDTO user = new UserInfoDTO();
user.setUserID(uid);
this.sampleAction.deleteUserInfo(user);
ModelAndView mav = new ModelAndView(this.userSuccessView);
return mav;
}
}
相关文章推荐
- Spring架构设计-增强MultiActionController
- Spring架构设计-增强MultiActionController
- Spring架构设计-增强MultiActionController
- Spring架构增强MultiActionController(3)
- Spring架构增强MultiActionController
- Spring架构增强MultiActionController(4)
- Spring架构增强MultiActionController(一)
- Spring架构增强MultiActionController(二)
- Spring架构增强MultiActionController(上)
- Spring架构增强MultiActionController(下)
- Spring架构增强MultiActionController(1)
- Spring架构增强MultiActionController
- Spring架构增强MultiActionController(2)
- spring中MultiActionController的数据绑定
- Spring MVC MultiActionController annotation example
- spring MultiActionController 简单配置
- springMVC3学习(五)--MultiActionController
- Spring MVC完成指南(一)Spring MVC MultiActionController
- springMVC3学习(五)--MultiActionController
- 初学spring3,关于MultiActionController的使用和配置