在上一篇学习笔记的实例中,每个控制器只能对应页面中的一个按钮,这样对于一个大型项目来说,就会产生很多的控制器,不利于管理,Spring提供了一个多动作控制器,开发人员可以把处理一个业务逻辑类似的动作,比如对用户进行增、删、改等,放在这个控制器中,从而减少控制器的数量。MultiActionController的实现方式有两种,一种是继承MultiActionController,另一种是在配置文档中定义一个代理Bean,由它来定义哪个控制器是多动作的。这里通过一个实例来说明前一种方式是如何实现的。
project的文件夹结构和上一篇文章中提到的一样。
(1)修改input.jsp,增加两个button。输出页面show.jsp不用改变
<body>
<form name="HelloWorld" action="/myApp/helloWorld.do" method="post">
欢迎语
<input name="msg" type="text" />
<br />
<input type="submit" name="method" value="insert" />
<input type="submit" name="method" value="update"/>
<input type="submit" name="method" value="delete"/>
</form>
</body>
(2)修改配置文档,增加两个bean
<!-- 定义控制器 -->
<bean id="helloWorldAction" class="com.jp.action.MultiActionControllerAction">
<property name="methodNameResolver">
<ref bean="paraMethodResolver" />
</property>
<property name="viewPage">
<value>show</value>
</property>
</bean>
<!-- 定义多动作参数 -->
<bean id="paraMethodResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName">
<value>method</value>
</property>
</bean>
(3)修改控制器代码,使其继承MultiActionController。MultiActionControllerAction.java代码如下:
package com.jp.action;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class MultiActionControllerAction extends MultiActionController {
private Logger logger = Logger.getLogger(this.getClass().getName());
private String viewPage;
// insert动作
public ModelAndView insert(HttpServletRequest req, HttpServletResponse res)
throws ServletRequestBindingException, UnsupportedEncodingException {
String yourWords = RequestUtils.getRequiredStringParameter(req, "msg");
return getModelAndView("insert", yourWords);
}
// update动作
public ModelAndView update(HttpServletRequest req, HttpServletResponse res)
throws ServletRequestBindingException, UnsupportedEncodingException {
String yourWords = RequestUtils.getRequiredStringParameter(req, "msg");
return getModelAndView("update", yourWords);
}
// delete动作
public ModelAndView delete(HttpServletRequest req, HttpServletResponse res)
throws ServletRequestBindingException, UnsupportedEncodingException {
String yourWords = RequestUtils.getRequiredStringParameter(req, "msg");
return getModelAndView("delete", yourWords);
}
private ModelAndView getModelAndView(String operationType, String yourWords)
throws UnsupportedEncodingException {
Map model = new HashMap();
model.put("yourWords", operationType + ":"
+ new String(yourWords.getBytes("iso-8859-1"), "utf-8"));
return new ModelAndView(getViewPage(), model);
}
public String getViewPage() {
return viewPage;
}
public void setViewPage(String viewPage) {
this.viewPage = viewPage;
}
}