在使用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; } /**//** * 功能:获得所有的用户信息
*/ 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; } /**//** * 功能:编辑用户信息
*/ 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; } /**//** * 功能:保存修改或新增的用户信息
*检查从页面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; } /**//** * 功能:删除用户信息
*/ 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; } } |
1.2 web-context配置
<!-- 把sampleMultiMethodController所有的请求映射到SimpleUrlHandlerMapping --> <bean id="handlerMapping">class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><br><property name="defaultHandler" ref=" sampleMultiMethodController "></property><br></bean> <!-- 集增,删,改,查操作到一个类的controller --> <bean id="sampleMultiMethodController">class="com.prs.application.ehld.sample.web.controller.SampleMultiMethodController"><br><property name="methodNameResolver"><br><bean>class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"><br><property name="mappings"><br><props><br><prop key="/ehld.sample.getuserinfolist.do">listUser</prop><br><prop key="/ehld.sample.edituserinfo.do">edtiUser</prop><br><prop key="/ehld.sample.saveuserinfo.do">saveUser</prop><br></props><br></property><br></bean><br></property><br><property name="sampleAction">ref="com.prs.application.ehld.sample.biz.action.sampleAction"></property><br></bean> |