您所在的位置: 首页>>开发>>架构&设计>>

Spring架构增强MultiActionController(1)

http://developer.51cto.com  2006-12-14 16:45  小蜜峰  IT168  我要评论(0)
  • 摘要:Spring借鉴Struts的DispatchAction提供了类似功能的MultiActionController。可以实现不同的请求路径对应MultiActionController中的不同方法,这样就可以把相关的操作都在一个类的相关方法中完成。具体内容包括:MultiActionController的使用、缺点、构想和它的增强实现。   
  • 标签:Java/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;
}
}

1.2 web-context配置

<!-- 把sampleMultiMethodController所有的请求映射到SimpleUrlHandlerMapping -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="defaultHandler" ref=" sampleMultiMethodController "/>
</bean>

<!-- 集增,删,改,查操作到一个类的controller -->
<bean id="sampleMultiMethodController"
class="com.prs.application.ehld.sample.web.controller.SampleMultiMethodController">
<property name="methodNameResolver">
<bean
class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/ehld.sample.getuserinfolist.do">listUser</prop>
<prop key="/ehld.sample.edituserinfo.do">edtiUser</prop>
<prop key="/ehld.sample.saveuserinfo.do">saveUser</prop>
</props>
</property>
</bean>
</property>
<property name="sampleAction"
ref="com.prs.application.ehld.sample.biz.action.sampleAction"></property>
</bean>


共4页: 1 [2] [3] [4] 下一页
【内容导航】
深入Vista应用程序开发
走向银光 —— 一步一步学Silverlight2
让你的代码“炫”起来——WPF开发教程
WebSphere 实现SOA的利器
初探敏捷开发
 
 验证码: (点击刷新验证码)   匿名发表
  • Visual C++ 完全自学宝典

  • 作者:强锋科技,朱洪波
  • Visual C++ 6.0是微软公司为程序人员提供的Visual Studio 6.0工具套件中的重要组成部分。本书由浅入深地介绍使用Visual C++ 6.0..
Copyright©2005-2008 51CTO.COM 版权所有